requestsType

Returns true if the provided type is requested in this SelectionSet.

Requested means that this SelectionSet contains an explicit field selection on a type that is the same or narrower than the supplied type, or that this SelectionSet includes an inline fragment or fragment spread with a type condition that is the same or narrower than the supplied type.

If a type is requested, it is safe to conclude that the GraphQL client that created this SelectionSet knows how to handle the supplied type.

GraphQL Object Type Examples

Given this type definition:

type Foo { id: ID! }

Then requestsType(Foo) will return these values for the provided selections

  • for selections id, returns true because the selection set contains a field defined by Foo

  • for selections ... { id }, returns true because the selection set contains an inline fragment with an implied type condition on the surrounding type, Foo

  • for selections id @skip(if:true), returns true because the selection set itself describes an inline fragment on type Foo, even though the selections are empty.

GraphQL Union Type Examples

Given these definitions:

type Foo { id: ID! }
type Bar { id: ID! }
union FooOrBar = Foo | Bar

And given these selections on FooOrBar:

... on Foo { id }

Then this method will have the following behavior:

  • requestsType(FooOrBar) returns true because the selections include an inline fragment on Foo, which is narrower than FooOrBar.

  • requestsType(Bar) returns false because there are no selections with a type condition on Bar

While the above example is based on a union type and its members, the rules apply equally well to an interface and its implementations.