-
Notifications
You must be signed in to change notification settings - Fork 6
/
graphQLOperationsJSON.js
133 lines (125 loc) · 4.45 KB
/
graphQLOperationsJSON.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// @ts-check
/* eslint-disable @typescript-eslint/no-var-requires */
const { concatAST, Kind } = require('graphql');
const { print } = require('graphql/language/printer');
const { sortBy, uniq } = require('lodash');
/**
*
* @param {import('graphql').SelectionSetNode} selectionSet
* @param {{ [name: string]: import('graphql').FragmentDefinitionNode }} fragmentsByName
* @param {Set<string>} seenFragments
* @returns {string[]}
*/
function getFragmentNames(selectionSet, fragmentsByName, seenFragments) {
return selectionSet.selections.flatMap((selection) => {
if (selection.kind === 'FragmentSpread') {
const fragment = fragmentsByName[selection.name.value];
if (seenFragments.has(selection.name.value)) {
return [selection.name.value];
} else {
seenFragments.add(selection.name.value);
return [selection.name.value, ...getFragmentNames(fragment.selectionSet, fragmentsByName, seenFragments)];
}
} else if (selection.kind === 'Field') {
if (selection.selectionSet) {
return getFragmentNames(selection.selectionSet, fragmentsByName, seenFragments);
} else {
return [];
}
} else if (selection.kind === 'InlineFragment') {
return getFragmentNames(selection.selectionSet, fragmentsByName, seenFragments);
} else {
return [];
}
});
}
/**
*
* @param {import('graphql').SelectionSetNode} selectionSet
* @returns {import('graphql').SelectionSetNode}
*/
function addTypenameToSelectionSet(selectionSet) {
const newSelectionSet = { ...selectionSet };
const hasDefinition = selectionSet.selections.some(
(selection) => selection.kind === 'Field' && selection.name.value === '__typename',
);
if (!hasDefinition) {
/** @type {import('graphql').FieldNode} */
const typenameSelection = {
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: '__typename',
},
};
newSelectionSet.selections = [...selectionSet.selections, typenameSelection];
}
newSelectionSet.selections = newSelectionSet.selections.map((selection) => {
if (selection.kind === Kind.FIELD && selection.selectionSet) {
return { ...selection, selectionSet: addTypenameToSelectionSet(selection.selectionSet) };
} else if (selection.kind === Kind.INLINE_FRAGMENT) {
return { ...selection, selectionSet: addTypenameToSelectionSet(selection.selectionSet) };
} else {
return selection;
}
});
return newSelectionSet;
}
/**
* @template {import('graphql').OperationDefinitionNode | import('graphql').FragmentDefinitionNode} T
* @param {T} definition
* @returns {T}
*/
function addTypenameToDefinition(definition) {
return {
...definition,
selectionSet: addTypenameToSelectionSet(definition.selectionSet),
};
}
/**
*
* @param {import('graphql').GraphQLSchema} _schema
* @param {{location: string, document: import('graphql').DocumentNode}[]} documents
* @returns
*/
const GraphQLJSONOperationsPlugin = (_schema, documents) => {
const sortedDocuments = sortBy(documents, (d) => d.location);
const allAST = concatAST(sortedDocuments.map((d) => d.document));
/** @type {{ [name: string] : import('graphql').OperationDefinitionNode }} */
const operationsByName = allAST.definitions.reduce((acc, definition) => {
if (definition.kind === 'OperationDefinition') {
const name = definition.name?.value;
if (name) {
acc[name] = addTypenameToDefinition(definition);
}
}
return acc;
}, {});
/** @type {{ [name: string] : import('graphql').FragmentDefinitionNode }} */
const fragmentsByName = allAST.definitions.reduce((acc, definition) => {
if (definition.kind === 'FragmentDefinition') {
const name = definition.name?.value;
if (name) {
acc[name] = addTypenameToDefinition(definition);
}
}
return acc;
}, {});
const result = Object.entries(operationsByName).reduce((acc, [name, definition]) => {
const fragmentNames = uniq(getFragmentNames(definition.selectionSet, fragmentsByName, new Set()));
const parts = [print(definition), ...fragmentNames.map((fragmentName) => print(fragmentsByName[fragmentName]))];
const document = parts.join('\n');
acc[name] = {
document,
ast: {
kind: 'Document',
definitions: [definition, ...fragmentNames.map((fragmentName) => fragmentsByName[fragmentName])],
},
};
return acc;
}, {});
return JSON.stringify(result);
};
module.exports = {
plugin: GraphQLJSONOperationsPlugin,
};