selection Set For
Return a SelectionSet describing the selections at the provided field. If the current SelectionSet does not include the provided field, then an empty SelectionSet will be returned.
This method can be used to extract selections for a field belonging to any subtype of the current type. This allows deriving subselections of a field from a union member if the current selections are on a union type, or deriving subselections of an interface implementation if the current selections are on an interface type.
Examples
Given these type definitions:
interface Node { id: ID! }
type Foo implements Node { id: ID!, bar: Bar }
type Bar implements Node { id: ID!, foo: Foo }And these selections on Node:
... on Foo {
bar {
id
foo { id }
}
}
... on Foo {
bar { __typename }
}Then selectionSetFor will have the following behavior:
selectionSetFor(Foo.bar)will return aSelectionSet<Bar>with a selections forid, foo { id }, __typename, because these are the merged selections for the Foo.bar field under Node.selectionSetFor(Bar.foo)will return aSelectionSet<Foo>that is empty, because even though Bar.foo is selected somewhere in the SelectionSet, it is not included in the immediate children of the current selections.
Return a SelectionSet that is a projection of the selections on the provided type. The returned SelectionSet may include fields that are selected on a parent interface or union -- if you need to know if a projected SelectionSet contains non-inherited selections for a type, see requestsType
Examples
Given these type definitions:
interface Node { id: ID! }
type Foo implements Node { id: ID!, name: String }
type Bar implements Node { id: ID! }And these selections on Node:
id
... on Foo { name }Then selectionSetFor will have the following behavior:
selectionSetFor(Node)returns the current SelectionSet because the provided type is the same as the current typeselectionSetFor(Foo)returns a SelectionSetwith selections id name, because the projection for type Foo includes the selections on the parent interface Node and the inline fragment selections on FooselectionSetFor(Bar)returns a SelectionSetwith selections on id, because the projection for type Bar includes the selections on the parent interface Node