28 lines
1.1 KiB
JavaScript
28 lines
1.1 KiB
JavaScript
import { valueFromASTUntyped, } from 'graphql';
|
|
export function getFieldsWithDirectives(documentNode, options = {}) {
|
|
const result = {};
|
|
let selected = ['ObjectTypeDefinition', 'ObjectTypeExtension'];
|
|
if (options.includeInputTypes) {
|
|
selected = [...selected, 'InputObjectTypeDefinition', 'InputObjectTypeExtension'];
|
|
}
|
|
const allTypes = documentNode.definitions.filter(obj => selected.includes(obj.kind));
|
|
for (const type of allTypes) {
|
|
const typeName = type.name.value;
|
|
if (type.fields == null) {
|
|
continue;
|
|
}
|
|
for (const field of type.fields) {
|
|
if (field.directives && field.directives.length > 0) {
|
|
const fieldName = field.name.value;
|
|
const key = `${typeName}.${fieldName}`;
|
|
const directives = field.directives.map(d => ({
|
|
name: d.name.value,
|
|
args: (d.arguments || []).reduce((prev, arg) => ({ ...prev, [arg.name.value]: valueFromASTUntyped(arg.value) }), {}),
|
|
}));
|
|
result[key] = directives;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|