1 | import { getDirectiveValues, GraphQLIncludeDirective, GraphQLSkipDirective, isAbstractType, Kind, typeFromAST, } from 'graphql';
|
2 | import { AccumulatorMap } from './AccumulatorMap.js';
|
3 | import { GraphQLDeferDirective } from './directives.js';
|
4 | import { memoize5 } from './memoize.js';
|
5 | function collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, visitedFragmentNames) {
|
6 | for (const selection of selectionSet.selections) {
|
7 | switch (selection.kind) {
|
8 | case Kind.FIELD: {
|
9 | if (!shouldIncludeNode(variableValues, selection)) {
|
10 | continue;
|
11 | }
|
12 | fields.add(getFieldEntryKey(selection), selection);
|
13 | break;
|
14 | }
|
15 | case Kind.INLINE_FRAGMENT: {
|
16 | if (!shouldIncludeNode(variableValues, selection) ||
|
17 | !doesFragmentConditionMatch(schema, selection, runtimeType)) {
|
18 | continue;
|
19 | }
|
20 | const defer = getDeferValues(variableValues, selection);
|
21 | if (defer) {
|
22 | const patchFields = new AccumulatorMap();
|
23 | collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, patchFields, patches, visitedFragmentNames);
|
24 | patches.push({
|
25 | label: defer.label,
|
26 | fields: patchFields,
|
27 | });
|
28 | }
|
29 | else {
|
30 | collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, patches, visitedFragmentNames);
|
31 | }
|
32 | break;
|
33 | }
|
34 | case Kind.FRAGMENT_SPREAD: {
|
35 | const fragName = selection.name.value;
|
36 | if (!shouldIncludeNode(variableValues, selection)) {
|
37 | continue;
|
38 | }
|
39 | const defer = getDeferValues(variableValues, selection);
|
40 | if (visitedFragmentNames.has(fragName) && !defer) {
|
41 | continue;
|
42 | }
|
43 | const fragment = fragments[fragName];
|
44 | if (!fragment || !doesFragmentConditionMatch(schema, fragment, runtimeType)) {
|
45 | continue;
|
46 | }
|
47 | if (!defer) {
|
48 | visitedFragmentNames.add(fragName);
|
49 | }
|
50 | if (defer) {
|
51 | const patchFields = new AccumulatorMap();
|
52 | collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, patchFields, patches, visitedFragmentNames);
|
53 | patches.push({
|
54 | label: defer.label,
|
55 | fields: patchFields,
|
56 | });
|
57 | }
|
58 | else {
|
59 | collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, fields, patches, visitedFragmentNames);
|
60 | }
|
61 | break;
|
62 | }
|
63 | }
|
64 | }
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export function collectFields(schema, fragments, variableValues, runtimeType, selectionSet) {
|
75 | const fields = new AccumulatorMap();
|
76 | const patches = [];
|
77 | collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, new Set());
|
78 | return { fields, patches };
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export function shouldIncludeNode(variableValues, node) {
|
85 | const skip = getDirectiveValues(GraphQLSkipDirective, node, variableValues);
|
86 | if (skip?.['if'] === true) {
|
87 | return false;
|
88 | }
|
89 | const include = getDirectiveValues(GraphQLIncludeDirective, node, variableValues);
|
90 | if (include?.['if'] === false) {
|
91 | return false;
|
92 | }
|
93 | return true;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 | export function doesFragmentConditionMatch(schema, fragment, type) {
|
99 | const typeConditionNode = fragment.typeCondition;
|
100 | if (!typeConditionNode) {
|
101 | return true;
|
102 | }
|
103 | const conditionalType = typeFromAST(schema, typeConditionNode);
|
104 | if (conditionalType === type) {
|
105 | return true;
|
106 | }
|
107 | if (isAbstractType(conditionalType)) {
|
108 | const possibleTypes = schema.getPossibleTypes(conditionalType);
|
109 | return possibleTypes.includes(type);
|
110 | }
|
111 | return false;
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 | export function getFieldEntryKey(node) {
|
117 | return node.alias ? node.alias.value : node.name.value;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export function getDeferValues(variableValues, node) {
|
125 | const defer = getDirectiveValues(GraphQLDeferDirective, node, variableValues);
|
126 | if (!defer) {
|
127 | return;
|
128 | }
|
129 | if (defer['if'] === false) {
|
130 | return;
|
131 | }
|
132 | return {
|
133 | label: typeof defer['label'] === 'string' ? defer['label'] : undefined,
|
134 | };
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | export const collectSubFields = memoize5(function collectSubfields(schema, fragments, variableValues, returnType, fieldNodes) {
|
146 | const subFieldNodes = new AccumulatorMap();
|
147 | const visitedFragmentNames = new Set();
|
148 | const subPatches = [];
|
149 | const subFieldsAndPatches = {
|
150 | fields: subFieldNodes,
|
151 | patches: subPatches,
|
152 | };
|
153 | for (const node of fieldNodes) {
|
154 | if (node.selectionSet) {
|
155 | collectFieldsImpl(schema, fragments, variableValues, returnType, node.selectionSet, subFieldNodes, subPatches, visitedFragmentNames);
|
156 | }
|
157 | }
|
158 | return subFieldsAndPatches;
|
159 | });
|