Field Collection

Before execution, the selection set is converted to a grouped field set by callingCollectFields(). Each entry in the grouped field set is a list of fields that share a response key. This ensures all fields with the same response key (alias or field name) included via referenced fragments are executed at the same time.

As an example, collecting the fields of this selection set would collect two instances of the fieldaand one of fieldb:

{
  a {
    subfield1
  }
  ...ExampleFragment
}

fragment ExampleFragment on Query {
  a {
    subfield2
  }
  b
}

The depth‐first‐search order of the field groups produced byCollectFields()is maintained through execution, ensuring that fields appear in the executed response in a stable and predictable order.

CollectFields

(

objectType

,

selectionSet

,

variableValues

,

visitedFragments

)

  1. If visitedFragments if not provided, initialize it to the empty set.
  2. Initialize groupedFields to an empty ordered map of lists.
  3. For each selection in selectionSet :
    1. If selection provides the directive @skip , let skipDirective be that directive.
      1. If skipDirective ‘s if argument is true or is a variable in variableValues with the value true , continue with the next selection in selectionSet .
    2. If selection provides the directive @include , let includeDirective be that directive.
      1. If includeDirective ‘s if argument is not true and is not a variable in variableValues with the value true , continue with the next selection in selectionSet .
    3. If selection is a Field :
      1. Let responseKey be the response key of selection .
      2. Let groupForResponseKey be the list in groupedFields for responseKey ; if no such list exists, create it as an empty list.
      3. Append selection to the groupForResponseKey .
    4. If selection is a FragmentSpread :
      1. Let fragmentSpreadName be the name of selection .
      2. If fragmentSpreadName is in visitedFragments , continue with the next selection in selectionSet .
      3. Add fragmentSpreadName to visitedFragments .
      4. Let fragment be the Fragment in the current Document whose name is fragmentSpreadName .
      5. If no such fragment exists, continue with the next selection in selectionSet .
      6. Let fragmentType be the type condition on fragment .
      7. If DoesFragmentTypeApply ( objectType , fragmentType ) is false, continue with the next selection in selectionSet .
      8. Let fragmentSelectionSet be the top‐level selection set of fragment .
      9. Let fragmentGroupedFieldSet be the result of calling CollectFields ( objectType , fragmentSelectionSet , visitedFragments ) .
      10. For each fragmentGroup in fragmentGroupedFieldSet :
        1. Let responseKey be the response key shared by all fields in fragmentGroup
        2. Let groupForResponseKey be the list in groupedFields for responseKey ; if no such list exists, create it as an empty list.
        3. Append all items in fragmentGroup to groupForResponseKey .
    5. If selection is an InlineFragment :
      1. Let fragmentType be the type condition on selection .
      2. If fragmentType is not null and DoesFragmentTypeApply ( objectType , fragmentType ) is false, continue with the next selection in selectionSet .
      3. Let fragmentSelectionSet be the top‐level selection set of selection .
      4. Let fragmentGroupedFieldSet be the result of calling CollectFields ( objectType , fragmentSelectionSet , variableValues , visitedFragments ) .
      5. For each fragmentGroup in fragmentGroupedFieldSet :
        1. Let responseKey be the response key shared by all fields in fragmentGroup
        2. Let groupForResponseKey be the list in groupedFields for responseKey ; if no such list exists, create it as an empty list.
        3. Append all items in fragmentGroup to groupForResponseKey .
  4. Return groupedFields .

DoesFragmentTypeApply

(

objectType

,

fragmentType

)

  1. If fragmentType is an Object Type:
    1. if objectType and fragmentType are the same type, return true , otherwise return false .
  2. If fragmentType is an Interface Type:
    1. if objectType is an implementation of fragmentType , return true otherwise return false .
  3. If fragmentType is a Union:
    1. if objectType is a possible type of fragmentType , return true otherwise return false .

results matching ""

    No results matching ""