# selectionSetFor

abstract fun <U : T, R : CompositeOutput> selectionSetFor(field: CompositeField<U, R>): SelectionSet<R>(source)

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 }

Content copied to clipboard

And these selections on Node:

    ... on Foo {
      bar {
        id
        foo { id }
      }
    }
    ... on Foo {
      bar { __typename }
    }

Content copied to clipboard

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.

abstract fun <U : T> selectionSetFor(type: Type<U>): SelectionSet<U>(source)

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! }

Content copied to clipboard

And these selections on Node:

    id
    ... on Foo { name }

Content copied to clipboard

Then `selectionSetFor` will have the following behavior:

- `selectionSetFor(Node)` returns the current SelectionSet because the provided type is the same as the current type

- `selectionSetFor(Foo)` returns a SelectionSet with selections `id name`, because the projection for type Foo includes the selections on the parent interface Node and the inline fragment selections on Foo

- `selectionSetFor(Bar)` returns a SelectionSet with selections on `id`, because the projection for type Bar includes the selections on the parent interface Node