File size: 1,425 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { type DocumentNode, type FieldNode, Kind } from 'graphql';

import { findOperationDefinition } from 'src/engine/api/graphql/direct-execution/utils/find-operation-definition.util';
import { graphQLBuildFragmentMap } from 'src/engine/api/graphql/direct-execution/utils/graphql-build-fragment-map.util';

export const graphQLExtractTopLevelFields = (
  document: DocumentNode,
  operationName: string | undefined,
): FieldNode[] => {
  const operationDefinition = findOperationDefinition(document, operationName);

  if (!operationDefinition) {
    return [];
  }

  const fragmentMap = graphQLBuildFragmentMap(document);
  const fields: FieldNode[] = [];

  for (const selection of operationDefinition.selectionSet.selections) {
    if (selection.kind === Kind.FIELD) {
      fields.push(selection);
    } else if (selection.kind === Kind.FRAGMENT_SPREAD) {
      const fragment = fragmentMap.get(selection.name.value);

      if (fragment) {
        for (const fragmentSelection of fragment.selectionSet.selections) {
          if (fragmentSelection.kind === Kind.FIELD) {
            fields.push(fragmentSelection);
          }
        }
      }
    } else if (selection.kind === Kind.INLINE_FRAGMENT) {
      for (const inlineSelection of selection.selectionSet.selections) {
        if (inlineSelection.kind === Kind.FIELD) {
          fields.push(inlineSelection);
        }
      }
    }
  }

  return fields;
};