1 | import { Kind, } from 'graphql';
|
2 | function parseDirectiveValue(value) {
|
3 | switch (value.kind) {
|
4 | case Kind.INT:
|
5 | return parseInt(value.value);
|
6 | case Kind.FLOAT:
|
7 | return parseFloat(value.value);
|
8 | case Kind.BOOLEAN:
|
9 | return Boolean(value.value);
|
10 | case Kind.STRING:
|
11 | case Kind.ENUM:
|
12 | return value.value;
|
13 | case Kind.LIST:
|
14 | return value.values.map(v => parseDirectiveValue(v));
|
15 | case Kind.OBJECT:
|
16 | return value.fields.reduce((prev, v) => ({ ...prev, [v.name.value]: parseDirectiveValue(v.value) }), {});
|
17 | case Kind.NULL:
|
18 | return null;
|
19 | default:
|
20 | return null;
|
21 | }
|
22 | }
|
23 | export function getFieldsWithDirectives(documentNode, options = {}) {
|
24 | const result = {};
|
25 | let selected = ['ObjectTypeDefinition', 'ObjectTypeExtension'];
|
26 | if (options.includeInputTypes) {
|
27 | selected = [...selected, 'InputObjectTypeDefinition', 'InputObjectTypeExtension'];
|
28 | }
|
29 | const allTypes = documentNode.definitions.filter(obj => selected.includes(obj.kind));
|
30 | for (const type of allTypes) {
|
31 | const typeName = type.name.value;
|
32 | if (type.fields == null) {
|
33 | continue;
|
34 | }
|
35 | for (const field of type.fields) {
|
36 | if (field.directives && field.directives.length > 0) {
|
37 | const fieldName = field.name.value;
|
38 | const key = `${typeName}.${fieldName}`;
|
39 | const directives = field.directives.map(d => ({
|
40 | name: d.name.value,
|
41 | args: (d.arguments || []).reduce((prev, arg) => ({ ...prev, [arg.name.value]: parseDirectiveValue(arg.value) }), {}),
|
42 | }));
|
43 | result[key] = directives;
|
44 | }
|
45 | }
|
46 | }
|
47 | return result;
|
48 | }
|