输入条件
TypeCondition
onNamedType
Fragments must specify the type they apply to. In this example,friendFields
can be used in the context of querying aUser
.
Fragments cannot be specified on any input value (scalar, enumeration, or input object).
Fragments can be specified on object types, interfaces, and unions.
Selections within fragments only return values when concrete type of the object it is operating on matches the type of the fragment.
For example in this query on the Facebook data model:
query FragmentTyping {
profiles(handles: ["zuck", "cocacola"]) {
handle
...userFragment
...pageFragment
}
}
fragment userFragment on User {
friends {
count
}
}
fragment pageFragment on Page {
likers {
count
}
}
Theprofiles
root field returns a list where each element could be aPage
or aUser
. When the object in theprofiles
result is aUser
,friends
will be present andlikers
will not. Conversely when the result is aPage
,likers
will be present andfriends
will not.
query FragmentTyping {
profiles(handles: ["zuck", "cocacola"]) {
handle
...userFragment
...pageFragment
}
}
fragment userFragment on User {
friends {
count
}
}
fragment pageFragment on Page {
likers {
count
}
}