selectionSetFor

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 a SelectionSet<Bar> with a selections for id, foo { id }, __typename, because these are the merged selections for the Foo.bar field under Node.

  • selectionSetFor(Bar.foo) will return a SelectionSet<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.