输入条件
TypeCondition
  onNamedType

Fragments must specify the type they apply to. In this example,friendFieldscan 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
  }
}

Theprofilesroot field returns a list where each element could be aPageor aUser. When the object in theprofilesresult is aUser,friendswill be present andlikerswill not. Conversely when the result is aPage,likerswill be present andfriendswill not.

query FragmentTyping {
  profiles(handles: ["zuck", "cocacola"]) {
    handle
    ...userFragment
    ...pageFragment
  }
}

fragment userFragment on User {
  friends {
    count
  }
}

fragment pageFragment on Page {
  likers {
    count
  }
}

results matching ""

    No results matching ""