123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.makeDirectiveNodes = exports.makeDirectiveNode = exports.makeDeprecatedDirective = exports.astFromEnumValue = exports.astFromInputField = exports.astFromField = exports.astFromScalarType = exports.astFromEnumType = exports.astFromInputObjectType = exports.astFromUnionType = exports.astFromInterfaceType = exports.astFromObjectType = exports.astFromArg = exports.getDeprecatableDirectiveNodes = exports.getDirectiveNodes = exports.astFromDirective = exports.astFromSchema = exports.printSchemaWithDirectives = exports.getDocumentNodeFromSchema = void 0;
- const graphql_1 = require("graphql");
- const astFromType_js_1 = require("./astFromType.js");
- const get_directives_js_1 = require("./get-directives.js");
- const astFromValueUntyped_js_1 = require("./astFromValueUntyped.js");
- const helpers_js_1 = require("./helpers.js");
- const rootTypes_js_1 = require("./rootTypes.js");
- function getDocumentNodeFromSchema(schema, options = {}) {
- const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
- const typesMap = schema.getTypeMap();
- const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
- const definitions = schemaNode != null ? [schemaNode] : [];
- const directives = schema.getDirectives();
- for (const directive of directives) {
- if ((0, graphql_1.isSpecifiedDirective)(directive)) {
- continue;
- }
- definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
- }
- for (const typeName in typesMap) {
- const type = typesMap[typeName];
- const isPredefinedScalar = (0, graphql_1.isSpecifiedScalarType)(type);
- const isIntrospection = (0, graphql_1.isIntrospectionType)(type);
- if (isPredefinedScalar || isIntrospection) {
- continue;
- }
- if ((0, graphql_1.isObjectType)(type)) {
- definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
- }
- else if ((0, graphql_1.isInterfaceType)(type)) {
- definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
- }
- else if ((0, graphql_1.isUnionType)(type)) {
- definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
- }
- else if ((0, graphql_1.isInputObjectType)(type)) {
- definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
- }
- else if ((0, graphql_1.isEnumType)(type)) {
- definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
- }
- else if ((0, graphql_1.isScalarType)(type)) {
- definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
- }
- else {
- throw new Error(`Unknown type ${type}.`);
- }
- }
- return {
- kind: graphql_1.Kind.DOCUMENT,
- definitions,
- };
- }
- exports.getDocumentNodeFromSchema = getDocumentNodeFromSchema;
- // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
- // currently does not allow customization of printSchema options having to do with comments.
- function printSchemaWithDirectives(schema, options = {}) {
- const documentNode = getDocumentNodeFromSchema(schema, options);
- return (0, graphql_1.print)(documentNode);
- }
- exports.printSchemaWithDirectives = printSchemaWithDirectives;
- function astFromSchema(schema, pathToDirectivesInExtensions) {
- var _a, _b;
- const operationTypeMap = new Map([
- ['query', undefined],
- ['mutation', undefined],
- ['subscription', undefined],
- ]);
- const nodes = [];
- if (schema.astNode != null) {
- nodes.push(schema.astNode);
- }
- if (schema.extensionASTNodes != null) {
- for (const extensionASTNode of schema.extensionASTNodes) {
- nodes.push(extensionASTNode);
- }
- }
- for (const node of nodes) {
- if (node.operationTypes) {
- for (const operationTypeDefinitionNode of node.operationTypes) {
- operationTypeMap.set(operationTypeDefinitionNode.operation, operationTypeDefinitionNode);
- }
- }
- }
- const rootTypeMap = (0, rootTypes_js_1.getRootTypeMap)(schema);
- for (const [operationTypeNode, operationTypeDefinitionNode] of operationTypeMap) {
- const rootType = rootTypeMap.get(operationTypeNode);
- if (rootType != null) {
- const rootTypeAST = (0, astFromType_js_1.astFromType)(rootType);
- if (operationTypeDefinitionNode != null) {
- operationTypeDefinitionNode.type = rootTypeAST;
- }
- else {
- operationTypeMap.set(operationTypeNode, {
- kind: graphql_1.Kind.OPERATION_TYPE_DEFINITION,
- operation: operationTypeNode,
- type: rootTypeAST,
- });
- }
- }
- }
- const operationTypes = [...operationTypeMap.values()].filter(helpers_js_1.isSome);
- const directives = getDirectiveNodes(schema, schema, pathToDirectivesInExtensions);
- if (!operationTypes.length && !directives.length) {
- return null;
- }
- const schemaNode = {
- kind: operationTypes != null ? graphql_1.Kind.SCHEMA_DEFINITION : graphql_1.Kind.SCHEMA_EXTENSION,
- operationTypes,
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: directives,
- };
- // This code is so weird because it needs to support GraphQL.js 14
- // In GraphQL.js 14 there is no `description` value on schemaNode
- schemaNode.description =
- ((_b = (_a = schema.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : schema.description != null)
- ? {
- kind: graphql_1.Kind.STRING,
- value: schema.description,
- block: true,
- }
- : undefined;
- return schemaNode;
- }
- exports.astFromSchema = astFromSchema;
- function astFromDirective(directive, schema, pathToDirectivesInExtensions) {
- var _a, _b, _c, _d;
- return {
- kind: graphql_1.Kind.DIRECTIVE_DEFINITION,
- description: (_b = (_a = directive.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (directive.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: directive.description,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: directive.name,
- },
- arguments: (_c = directive.args) === null || _c === void 0 ? void 0 : _c.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)),
- repeatable: directive.isRepeatable,
- locations: ((_d = directive.locations) === null || _d === void 0 ? void 0 : _d.map(location => ({
- kind: graphql_1.Kind.NAME,
- value: location,
- }))) || [],
- };
- }
- exports.astFromDirective = astFromDirective;
- function getDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
- const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(entity, pathToDirectivesInExtensions);
- let nodes = [];
- if (entity.astNode != null) {
- nodes.push(entity.astNode);
- }
- if ('extensionASTNodes' in entity && entity.extensionASTNodes != null) {
- nodes = nodes.concat(entity.extensionASTNodes);
- }
- let directives;
- if (directivesInExtensions != null) {
- directives = makeDirectiveNodes(schema, directivesInExtensions);
- }
- else {
- directives = [];
- for (const node of nodes) {
- if (node.directives) {
- directives.push(...node.directives);
- }
- }
- }
- return directives;
- }
- exports.getDirectiveNodes = getDirectiveNodes;
- function getDeprecatableDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- let directiveNodesBesidesDeprecated = [];
- let deprecatedDirectiveNode = null;
- const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(entity, pathToDirectivesInExtensions);
- let directives;
- if (directivesInExtensions != null) {
- directives = makeDirectiveNodes(schema, directivesInExtensions);
- }
- else {
- directives = (_a = entity.astNode) === null || _a === void 0 ? void 0 : _a.directives;
- }
- if (directives != null) {
- directiveNodesBesidesDeprecated = directives.filter(directive => directive.name.value !== 'deprecated');
- if (entity.deprecationReason != null) {
- deprecatedDirectiveNode = (_b = directives.filter(directive => directive.name.value === 'deprecated')) === null || _b === void 0 ? void 0 : _b[0];
- }
- }
- if (entity.deprecationReason != null &&
- deprecatedDirectiveNode == null) {
- deprecatedDirectiveNode = makeDeprecatedDirective(entity.deprecationReason);
- }
- return deprecatedDirectiveNode == null
- ? directiveNodesBesidesDeprecated
- : [deprecatedDirectiveNode].concat(directiveNodesBesidesDeprecated);
- }
- exports.getDeprecatableDirectiveNodes = getDeprecatableDirectiveNodes;
- function astFromArg(arg, schema, pathToDirectivesInExtensions) {
- var _a, _b, _c;
- return {
- kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
- description: (_b = (_a = arg.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (arg.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: arg.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: arg.name,
- },
- type: (0, astFromType_js_1.astFromType)(arg.type),
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- defaultValue: arg.defaultValue !== undefined ? (_c = (0, graphql_1.astFromValue)(arg.defaultValue, arg.type)) !== null && _c !== void 0 ? _c : undefined : undefined,
- directives: getDeprecatableDirectiveNodes(arg, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromArg = astFromArg;
- function astFromObjectType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.OBJECT_TYPE_DEFINITION,
- description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)),
- interfaces: Object.values(type.getInterfaces()).map(iFace => (0, astFromType_js_1.astFromType)(iFace)),
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromObjectType = astFromObjectType;
- function astFromInterfaceType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- const node = {
- kind: graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
- description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)),
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
- };
- if ('getInterfaces' in type) {
- node.interfaces = Object.values(type.getInterfaces()).map(iFace => (0, astFromType_js_1.astFromType)(iFace));
- }
- return node;
- }
- exports.astFromInterfaceType = astFromInterfaceType;
- function astFromUnionType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.UNION_TYPE_DEFINITION,
- description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
- types: type.getTypes().map(type => (0, astFromType_js_1.astFromType)(type)),
- };
- }
- exports.astFromUnionType = astFromUnionType;
- function astFromInputObjectType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
- description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- fields: Object.values(type.getFields()).map(field => astFromInputField(field, schema, pathToDirectivesInExtensions)),
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromInputObjectType = astFromInputObjectType;
- function astFromEnumType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.ENUM_TYPE_DEFINITION,
- description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- values: Object.values(type.getValues()).map(value => astFromEnumValue(value, schema, pathToDirectivesInExtensions)),
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromEnumType = astFromEnumType;
- function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
- var _a, _b, _c;
- const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(type, pathToDirectivesInExtensions);
- const directives = directivesInExtensions
- ? makeDirectiveNodes(schema, directivesInExtensions)
- : ((_a = type.astNode) === null || _a === void 0 ? void 0 : _a.directives) || [];
- const specifiedByValue = (type['specifiedByUrl'] || type['specifiedByURL']);
- if (specifiedByValue && !directives.some(directiveNode => directiveNode.name.value === 'specifiedBy')) {
- const specifiedByArgs = {
- url: specifiedByValue,
- };
- directives.push(makeDirectiveNode('specifiedBy', specifiedByArgs));
- }
- return {
- kind: graphql_1.Kind.SCALAR_TYPE_DEFINITION,
- description: (_c = (_b = type.astNode) === null || _b === void 0 ? void 0 : _b.description) !== null && _c !== void 0 ? _c : (type.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: type.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: type.name,
- },
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: directives,
- };
- }
- exports.astFromScalarType = astFromScalarType;
- function astFromField(field, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.FIELD_DEFINITION,
- description: (_b = (_a = field.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (field.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: field.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: field.name,
- },
- arguments: field.args.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)),
- type: (0, astFromType_js_1.astFromType)(field.type),
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromField = astFromField;
- function astFromInputField(field, schema, pathToDirectivesInExtensions) {
- var _a, _b, _c;
- return {
- kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
- description: (_b = (_a = field.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (field.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: field.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: field.name,
- },
- type: (0, astFromType_js_1.astFromType)(field.type),
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
- defaultValue: (_c = (0, graphql_1.astFromValue)(field.defaultValue, field.type)) !== null && _c !== void 0 ? _c : undefined,
- };
- }
- exports.astFromInputField = astFromInputField;
- function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
- var _a, _b;
- return {
- kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
- description: (_b = (_a = value.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (value.description
- ? {
- kind: graphql_1.Kind.STRING,
- value: value.description,
- block: true,
- }
- : undefined),
- name: {
- kind: graphql_1.Kind.NAME,
- value: value.name,
- },
- // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
- directives: getDeprecatableDirectiveNodes(value, schema, pathToDirectivesInExtensions),
- };
- }
- exports.astFromEnumValue = astFromEnumValue;
- function makeDeprecatedDirective(deprecationReason) {
- return makeDirectiveNode('deprecated', { reason: deprecationReason }, graphql_1.GraphQLDeprecatedDirective);
- }
- exports.makeDeprecatedDirective = makeDeprecatedDirective;
- function makeDirectiveNode(name, args, directive) {
- const directiveArguments = [];
- if (directive != null) {
- for (const arg of directive.args) {
- const argName = arg.name;
- const argValue = args[argName];
- if (argValue !== undefined) {
- const value = (0, graphql_1.astFromValue)(argValue, arg.type);
- if (value) {
- directiveArguments.push({
- kind: graphql_1.Kind.ARGUMENT,
- name: {
- kind: graphql_1.Kind.NAME,
- value: argName,
- },
- value,
- });
- }
- }
- }
- }
- else {
- for (const argName in args) {
- const argValue = args[argName];
- const value = (0, astFromValueUntyped_js_1.astFromValueUntyped)(argValue);
- if (value) {
- directiveArguments.push({
- kind: graphql_1.Kind.ARGUMENT,
- name: {
- kind: graphql_1.Kind.NAME,
- value: argName,
- },
- value,
- });
- }
- }
- }
- return {
- kind: graphql_1.Kind.DIRECTIVE,
- name: {
- kind: graphql_1.Kind.NAME,
- value: name,
- },
- arguments: directiveArguments,
- };
- }
- exports.makeDirectiveNode = makeDirectiveNode;
- function makeDirectiveNodes(schema, directiveValues) {
- const directiveNodes = [];
- for (const directiveName in directiveValues) {
- const arrayOrSingleValue = directiveValues[directiveName];
- const directive = schema === null || schema === void 0 ? void 0 : schema.getDirective(directiveName);
- if (Array.isArray(arrayOrSingleValue)) {
- for (const value of arrayOrSingleValue) {
- directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
- }
- }
- else {
- directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
- }
- }
- return directiveNodes;
- }
- exports.makeDirectiveNodes = makeDirectiveNodes;
|