UNPKG

@pothos/plugin-directives

Version:

Directive plugin for Pothos, enables using graphql-tools based directives with Pothos

324 lines (323 loc) 13.6 kB
import './global-types.js'; import { astFromValue, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, Kind, parseValue } from 'graphql'; export default function mockAst(schema) { var _schema_extensions; const types = schema.getTypeMap(); schema.extensionASTNodes = [ { kind: Kind.SCHEMA_EXTENSION, directives: directiveNodes((_schema_extensions = schema.extensions) === null || _schema_extensions === void 0 ? void 0 : _schema_extensions.directives, null, schema), operationTypes: [ { operation: 'query', node: schema.getQueryType() }, { operation: 'mutation', node: schema.getMutationType() }, { operation: 'subscription', node: schema.getSubscriptionType() } ].filter(({ node, operation })=>node && node.name !== `${operation[0].toUpperCase()}${operation.slice(1)}`).map(({ operation, node })=>({ kind: Kind.OPERATION_TYPE_DEFINITION, operation, type: { kind: Kind.NAMED_TYPE, name: { kind: Kind.NAME, value: node.name } } })) } ]; for (const typeName of Object.keys(types)){ const type = types[typeName]; if (type instanceof GraphQLObjectType) { var _type; var _type_extensions; (_type = type).astNode || (_type.astNode = { kind: Kind.OBJECT_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, interfaces: type.getInterfaces().map((iface)=>typeNode(iface)), fields: fieldNodes(type.getFields(), schema), directives: directiveNodes((_type_extensions = type.extensions) === null || _type_extensions === void 0 ? void 0 : _type_extensions.directives, null, schema) }); } else if (type instanceof GraphQLInterfaceType) { var _type1; var _type_extensions1; (_type1 = type).astNode || (_type1.astNode = { kind: Kind.INTERFACE_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, interfaces: type.getInterfaces().map((iface)=>typeNode(iface)), fields: fieldNodes(type.getFields(), schema), directives: directiveNodes((_type_extensions1 = type.extensions) === null || _type_extensions1 === void 0 ? void 0 : _type_extensions1.directives, null, schema) }); } else if (type instanceof GraphQLUnionType) { var _type2; var _type_extensions2; (_type2 = type).astNode || (_type2.astNode = { kind: Kind.UNION_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, types: type.getTypes().map((iface)=>typeNode(iface)), directives: directiveNodes((_type_extensions2 = type.extensions) === null || _type_extensions2 === void 0 ? void 0 : _type_extensions2.directives, null, schema) }); } else if (type instanceof GraphQLEnumType) { var _type3; var _type_extensions3; (_type3 = type).astNode || (_type3.astNode = { kind: Kind.ENUM_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, values: enumValueNodes(type.getValues(), schema), directives: directiveNodes((_type_extensions3 = type.extensions) === null || _type_extensions3 === void 0 ? void 0 : _type_extensions3.directives, null, schema) }); } else if (type instanceof GraphQLScalarType) { var _type4; var _type_extensions4; (_type4 = type).astNode || (_type4.astNode = { kind: Kind.SCALAR_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, directives: directiveNodes((_type_extensions4 = type.extensions) === null || _type_extensions4 === void 0 ? void 0 : _type_extensions4.directives, null, schema) }); } else if (type instanceof GraphQLInputObjectType) { var _type5; var _type_extensions5; (_type5 = type).astNode || (_type5.astNode = { kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, fields: inputFieldNodes(type.getFields(), schema), directives: directiveNodes((_type_extensions5 = type.extensions) === null || _type_extensions5 === void 0 ? void 0 : _type_extensions5.directives, null, schema) }); } } } function typeNode(type) { if (type instanceof GraphQLList) { return { kind: Kind.LIST_TYPE, type: typeNode(type.ofType) }; } if (type instanceof GraphQLNonNull) { return { kind: Kind.NON_NULL_TYPE, type: typeNode(type.ofType) }; } return { kind: Kind.NAMED_TYPE, name: { kind: Kind.NAME, value: type.name } }; } function valueNode(value, arg) { if (value == null) { return { kind: Kind.NULL }; } if (arg) { return astFromValue(value, arg.type); } if (Array.isArray(value)) { return { kind: Kind.LIST, values: value.map((val)=>valueNode(val)) }; } switch(typeof value){ case 'object': return { kind: Kind.OBJECT, fields: Object.keys(value).map((key)=>({ kind: Kind.OBJECT_FIELD, name: { kind: Kind.NAME, value: key }, value: valueNode(value[key]) })) }; default: return parseValue(JSON.stringify(value)); } } function directiveNodes(rawDirectives, deprecationReason, schema) { if (!rawDirectives && !deprecationReason) { return []; } const directives = rawDirectives !== null && rawDirectives !== void 0 ? rawDirectives : []; const directiveList = Array.isArray(directives) ? directives : Object.keys(directives).flatMap((name)=>Array.isArray(directives[name]) ? directives[name].map((args)=>({ name, args })) : { name, args: directives[name] }); if (deprecationReason) { const deprecatedIndex = directiveList.findIndex((directive)=>directive.name === 'deprecated'); const deprecatedDirective = { name: 'deprecated', args: { reason: deprecationReason } }; if (deprecatedIndex === -1) { directiveList.unshift(deprecatedDirective); } else { directiveList[deprecatedIndex] = deprecatedDirective; } } return directiveList.map((directive)=>{ const directiveDef = schema.getDirective(directive.name); directiveDef === null || directiveDef === void 0 ? void 0 : directiveDef.args.find((arg)=>arg.name); return { kind: Kind.DIRECTIVE, name: { kind: Kind.NAME, value: directive.name }, arguments: directive.args && Object.keys(directive.args).map((argName)=>({ kind: Kind.ARGUMENT, name: { kind: Kind.NAME, value: argName }, value: valueNode(directive.args[argName], directiveDef === null || directiveDef === void 0 ? void 0 : directiveDef.args.find((arg)=>arg.name === argName)) })) }; }); } function fieldNodes(fields, schema) { return Object.keys(fields).map((fieldName)=>{ var _field, _field_deprecationReason; var _field_extensions; const field = fields[fieldName]; (_field = field).astNode || (_field.astNode = { kind: Kind.FIELD_DEFINITION, description: field.description ? { kind: Kind.STRING, value: field.description } : undefined, name: { kind: Kind.NAME, value: fieldName }, arguments: argumentNodes(field.args, schema), type: typeNode(field.type), directives: directiveNodes((_field_extensions = field.extensions) === null || _field_extensions === void 0 ? void 0 : _field_extensions.directives, (_field_deprecationReason = field.deprecationReason) !== null && _field_deprecationReason !== void 0 ? _field_deprecationReason : null, schema) }); return field.astNode; }); } function inputFieldNodes(fields, schema) { return Object.keys(fields).map((fieldName)=>{ var _field, _field_deprecationReason; var _field_extensions; const field = fields[fieldName]; const defaultValueNode = astFromValue(field.defaultValue, field.type); (_field = field).astNode || (_field.astNode = { kind: Kind.INPUT_VALUE_DEFINITION, description: field.description ? { kind: Kind.STRING, value: field.description } : undefined, name: { kind: Kind.NAME, value: fieldName }, type: typeNode(field.type), defaultValue: field.defaultValue === undefined ? undefined : defaultValueNode, directives: directiveNodes((_field_extensions = field.extensions) === null || _field_extensions === void 0 ? void 0 : _field_extensions.directives, (_field_deprecationReason = field.deprecationReason) !== null && _field_deprecationReason !== void 0 ? _field_deprecationReason : null, schema) }); return field.astNode; }); } function argumentNodes(args, schema) { return args.map((arg)=>{ var _arg, _arg_deprecationReason; var _arg_extensions; const defaultValueNode = astFromValue(arg.defaultValue, arg.type); (_arg = arg).astNode || (_arg.astNode = { kind: Kind.INPUT_VALUE_DEFINITION, description: arg.description ? { kind: Kind.STRING, value: arg.description } : undefined, name: { kind: Kind.NAME, value: arg.name }, type: typeNode(arg.type), defaultValue: arg.defaultValue === undefined ? undefined : defaultValueNode, directives: directiveNodes((_arg_extensions = arg.extensions) === null || _arg_extensions === void 0 ? void 0 : _arg_extensions.directives, (_arg_deprecationReason = arg.deprecationReason) !== null && _arg_deprecationReason !== void 0 ? _arg_deprecationReason : null, schema) }); return arg.astNode; }); } function enumValueNodes(values, schema) { return values.map((value)=>{ var _value, _value_deprecationReason; var _value_extensions; (_value = value).astNode || (_value.astNode = { kind: Kind.ENUM_VALUE_DEFINITION, description: value.description ? { kind: Kind.STRING, value: value.description } : undefined, name: { kind: Kind.NAME, value: value.name }, directives: directiveNodes((_value_extensions = value.extensions) === null || _value_extensions === void 0 ? void 0 : _value_extensions.directives, (_value_deprecationReason = value.deprecationReason) !== null && _value_deprecationReason !== void 0 ? _value_deprecationReason : null, schema) }); return value.astNode; }); } //# sourceMappingURL=mock-ast.js.map