UNPKG

@pothos/plugin-directives

Version:

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

316 lines (315 loc) 12.6 kB
import './global-types.js'; import { GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, Kind, astFromValue, 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_extensions; 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 _type_extensions1; type.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 _type_extensions2; type.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 _type_extensions3; type.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 _type_extensions4; type.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 _type_extensions5; type.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(directives, deprecationReason, schema) { if (!directives) { return []; } 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) { directiveList.unshift({ name: "deprecated", args: { reason: deprecationReason } }); } 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_extensions; const field = fields[fieldName]; var _field_deprecationReason; 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_extensions; const field = fields[fieldName]; const defaultValueNode = astFromValue(field.defaultValue, field.type); var _field_deprecationReason; 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_extensions; const defaultValueNode = astFromValue(arg.defaultValue, arg.type); var _arg_deprecationReason; 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_extensions; var _value_deprecationReason; 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