UNPKG

1.15 kBJavaScriptView Raw
1import { valueFromASTUntyped, } from 'graphql';
2export function getFieldsWithDirectives(documentNode, options = {}) {
3 const result = {};
4 let selected = ['ObjectTypeDefinition', 'ObjectTypeExtension'];
5 if (options.includeInputTypes) {
6 selected = [...selected, 'InputObjectTypeDefinition', 'InputObjectTypeExtension'];
7 }
8 const allTypes = documentNode.definitions.filter(obj => selected.includes(obj.kind));
9 for (const type of allTypes) {
10 const typeName = type.name.value;
11 if (type.fields == null) {
12 continue;
13 }
14 for (const field of type.fields) {
15 if (field.directives && field.directives.length > 0) {
16 const fieldName = field.name.value;
17 const key = `${typeName}.${fieldName}`;
18 const directives = field.directives.map(d => ({
19 name: d.name.value,
20 args: (d.arguments || []).reduce((prev, arg) => ({ ...prev, [arg.name.value]: valueFromASTUntyped(arg.value) }), {}),
21 }));
22 result[key] = directives;
23 }
24 }
25 }
26 return result;
27}