UNPKG

@pothos/plugin-directives

Version:

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

321 lines (320 loc) 13.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return mockAst; } }); require("./global-types"); const _graphql = require("graphql"); function mockAst(schema) { var _schema_extensions; const types = schema.getTypeMap(); schema.extensionASTNodes = [ { kind: _graphql.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: _graphql.Kind.OPERATION_TYPE_DEFINITION, operation, type: { kind: _graphql.Kind.NAMED_TYPE, name: { kind: _graphql.Kind.NAME, value: node.name } } })) } ]; for (const typeName of Object.keys(types)){ const type = types[typeName]; if (type instanceof _graphql.GraphQLObjectType) { var _type_extensions; type.astNode = { kind: _graphql.Kind.OBJECT_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLInterfaceType) { var _type_extensions1; type.astNode = { kind: _graphql.Kind.INTERFACE_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLUnionType) { var _type_extensions2; type.astNode = { kind: _graphql.Kind.UNION_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLEnumType) { var _type_extensions3; type.astNode = { kind: _graphql.Kind.ENUM_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLScalarType) { var _type_extensions4; type.astNode = { kind: _graphql.Kind.SCALAR_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLInputObjectType) { var _type_extensions5; type.astNode = { kind: _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION, name: { kind: _graphql.Kind.NAME, value: typeName }, description: type.description ? { kind: _graphql.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 _graphql.GraphQLList) { return { kind: _graphql.Kind.LIST_TYPE, type: typeNode(type.ofType) }; } if (type instanceof _graphql.GraphQLNonNull) { return { kind: _graphql.Kind.NON_NULL_TYPE, type: typeNode(type.ofType) }; } return { kind: _graphql.Kind.NAMED_TYPE, name: { kind: _graphql.Kind.NAME, value: type.name } }; } function valueNode(value, arg) { if (value == null) { return { kind: _graphql.Kind.NULL }; } if (arg) { return (0, _graphql.astFromValue)(value, arg.type); } if (Array.isArray(value)) { return { kind: _graphql.Kind.LIST, values: value.map((val)=>valueNode(val)) }; } switch(typeof value){ case 'object': return { kind: _graphql.Kind.OBJECT, fields: Object.keys(value).map((key)=>({ kind: _graphql.Kind.OBJECT_FIELD, name: { kind: _graphql.Kind.NAME, value: key }, value: valueNode(value[key]) })) }; default: return (0, _graphql.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: _graphql.Kind.DIRECTIVE, name: { kind: _graphql.Kind.NAME, value: directive.name }, arguments: directive.args && Object.keys(directive.args).map((argName)=>({ kind: _graphql.Kind.ARGUMENT, name: { kind: _graphql.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: _graphql.Kind.FIELD_DEFINITION, description: field.description ? { kind: _graphql.Kind.STRING, value: field.description } : undefined, name: { kind: _graphql.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 = (0, _graphql.astFromValue)(field.defaultValue, field.type); var _field_deprecationReason; field.astNode = { kind: _graphql.Kind.INPUT_VALUE_DEFINITION, description: field.description ? { kind: _graphql.Kind.STRING, value: field.description } : undefined, name: { kind: _graphql.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 = (0, _graphql.astFromValue)(arg.defaultValue, arg.type); var _arg_deprecationReason; arg.astNode = { kind: _graphql.Kind.INPUT_VALUE_DEFINITION, description: arg.description ? { kind: _graphql.Kind.STRING, value: arg.description } : undefined, name: { kind: _graphql.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: _graphql.Kind.ENUM_VALUE_DEFINITION, description: value.description ? { kind: _graphql.Kind.STRING, value: value.description } : undefined, name: { kind: _graphql.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