123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- import objectValues from "../polyfills/objectValues.mjs";
- import inspect from "../jsutils/inspect.mjs";
- import devAssert from "../jsutils/devAssert.mjs";
- import keyValMap from "../jsutils/keyValMap.mjs";
- import isObjectLike from "../jsutils/isObjectLike.mjs";
- import { parseValue } from "../language/parser.mjs";
- import { GraphQLSchema } from "../type/schema.mjs";
- import { GraphQLDirective } from "../type/directives.mjs";
- import { specifiedScalarTypes } from "../type/scalars.mjs";
- import { introspectionTypes, TypeKind } from "../type/introspection.mjs";
- import { isInputType, isOutputType, GraphQLList, GraphQLNonNull, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, assertNullableType, assertObjectType, assertInterfaceType } from "../type/definition.mjs";
- import { valueFromAST } from "./valueFromAST.mjs";
- /**
- * Build a GraphQLSchema for use by client tools.
- *
- * Given the result of a client running the introspection query, creates and
- * returns a GraphQLSchema instance which can be then used with all graphql-js
- * tools, but cannot be used to execute a query, as introspection does not
- * represent the "resolver", "parse" or "serialize" functions or any other
- * server-internal mechanisms.
- *
- * This function expects a complete introspection result. Don't forget to check
- * the "errors" field of a server response before calling this function.
- */
- export function buildClientSchema(introspection, options) {
- isObjectLike(introspection) && isObjectLike(introspection.__schema) || devAssert(0, "Invalid or incomplete introspection result. Ensure that you are passing \"data\" property of introspection response and no \"errors\" was returned alongside: ".concat(inspect(introspection), ".")); // Get the schema from the introspection result.
- var schemaIntrospection = introspection.__schema; // Iterate through all types, getting the type definition for each.
- var typeMap = keyValMap(schemaIntrospection.types, function (typeIntrospection) {
- return typeIntrospection.name;
- }, function (typeIntrospection) {
- return buildType(typeIntrospection);
- }); // Include standard types only if they are used.
- for (var _i2 = 0, _ref2 = [].concat(specifiedScalarTypes, introspectionTypes); _i2 < _ref2.length; _i2++) {
- var stdType = _ref2[_i2];
- if (typeMap[stdType.name]) {
- typeMap[stdType.name] = stdType;
- }
- } // Get the root Query, Mutation, and Subscription types.
- var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null;
- var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null;
- var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null; // Get the directives supported by Introspection, assuming empty-set if
- // directives were not queried for.
- var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; // Then produce and return a Schema with these types.
- return new GraphQLSchema({
- description: schemaIntrospection.description,
- query: queryType,
- mutation: mutationType,
- subscription: subscriptionType,
- types: objectValues(typeMap),
- directives: directives,
- assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
- }); // Given a type reference in introspection, return the GraphQLType instance.
- // preferring cached instances before building new instances.
- function getType(typeRef) {
- if (typeRef.kind === TypeKind.LIST) {
- var itemRef = typeRef.ofType;
- if (!itemRef) {
- throw new Error('Decorated type deeper than introspection query.');
- }
- return new GraphQLList(getType(itemRef));
- }
- if (typeRef.kind === TypeKind.NON_NULL) {
- var nullableRef = typeRef.ofType;
- if (!nullableRef) {
- throw new Error('Decorated type deeper than introspection query.');
- }
- var nullableType = getType(nullableRef);
- return new GraphQLNonNull(assertNullableType(nullableType));
- }
- return getNamedType(typeRef);
- }
- function getNamedType(typeRef) {
- var typeName = typeRef.name;
- if (!typeName) {
- throw new Error("Unknown type reference: ".concat(inspect(typeRef), "."));
- }
- var type = typeMap[typeName];
- if (!type) {
- throw new Error("Invalid or incomplete schema, unknown type: ".concat(typeName, ". Ensure that a full introspection query is used in order to build a client schema."));
- }
- return type;
- }
- function getObjectType(typeRef) {
- return assertObjectType(getNamedType(typeRef));
- }
- function getInterfaceType(typeRef) {
- return assertInterfaceType(getNamedType(typeRef));
- } // Given a type's introspection result, construct the correct
- // GraphQLType instance.
- function buildType(type) {
- if (type != null && type.name != null && type.kind != null) {
- switch (type.kind) {
- case TypeKind.SCALAR:
- return buildScalarDef(type);
- case TypeKind.OBJECT:
- return buildObjectDef(type);
- case TypeKind.INTERFACE:
- return buildInterfaceDef(type);
- case TypeKind.UNION:
- return buildUnionDef(type);
- case TypeKind.ENUM:
- return buildEnumDef(type);
- case TypeKind.INPUT_OBJECT:
- return buildInputObjectDef(type);
- }
- }
- var typeStr = inspect(type);
- throw new Error("Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ".concat(typeStr, "."));
- }
- function buildScalarDef(scalarIntrospection) {
- return new GraphQLScalarType({
- name: scalarIntrospection.name,
- description: scalarIntrospection.description,
- specifiedByUrl: scalarIntrospection.specifiedByUrl
- });
- }
- function buildImplementationsList(implementingIntrospection) {
- // TODO: Temporary workaround until GraphQL ecosystem will fully support
- // 'interfaces' on interface types.
- if (implementingIntrospection.interfaces === null && implementingIntrospection.kind === TypeKind.INTERFACE) {
- return [];
- }
- if (!implementingIntrospection.interfaces) {
- var implementingIntrospectionStr = inspect(implementingIntrospection);
- throw new Error("Introspection result missing interfaces: ".concat(implementingIntrospectionStr, "."));
- }
- return implementingIntrospection.interfaces.map(getInterfaceType);
- }
- function buildObjectDef(objectIntrospection) {
- return new GraphQLObjectType({
- name: objectIntrospection.name,
- description: objectIntrospection.description,
- interfaces: function interfaces() {
- return buildImplementationsList(objectIntrospection);
- },
- fields: function fields() {
- return buildFieldDefMap(objectIntrospection);
- }
- });
- }
- function buildInterfaceDef(interfaceIntrospection) {
- return new GraphQLInterfaceType({
- name: interfaceIntrospection.name,
- description: interfaceIntrospection.description,
- interfaces: function interfaces() {
- return buildImplementationsList(interfaceIntrospection);
- },
- fields: function fields() {
- return buildFieldDefMap(interfaceIntrospection);
- }
- });
- }
- function buildUnionDef(unionIntrospection) {
- if (!unionIntrospection.possibleTypes) {
- var unionIntrospectionStr = inspect(unionIntrospection);
- throw new Error("Introspection result missing possibleTypes: ".concat(unionIntrospectionStr, "."));
- }
- return new GraphQLUnionType({
- name: unionIntrospection.name,
- description: unionIntrospection.description,
- types: function types() {
- return unionIntrospection.possibleTypes.map(getObjectType);
- }
- });
- }
- function buildEnumDef(enumIntrospection) {
- if (!enumIntrospection.enumValues) {
- var enumIntrospectionStr = inspect(enumIntrospection);
- throw new Error("Introspection result missing enumValues: ".concat(enumIntrospectionStr, "."));
- }
- return new GraphQLEnumType({
- name: enumIntrospection.name,
- description: enumIntrospection.description,
- values: keyValMap(enumIntrospection.enumValues, function (valueIntrospection) {
- return valueIntrospection.name;
- }, function (valueIntrospection) {
- return {
- description: valueIntrospection.description,
- deprecationReason: valueIntrospection.deprecationReason
- };
- })
- });
- }
- function buildInputObjectDef(inputObjectIntrospection) {
- if (!inputObjectIntrospection.inputFields) {
- var inputObjectIntrospectionStr = inspect(inputObjectIntrospection);
- throw new Error("Introspection result missing inputFields: ".concat(inputObjectIntrospectionStr, "."));
- }
- return new GraphQLInputObjectType({
- name: inputObjectIntrospection.name,
- description: inputObjectIntrospection.description,
- fields: function fields() {
- return buildInputValueDefMap(inputObjectIntrospection.inputFields);
- }
- });
- }
- function buildFieldDefMap(typeIntrospection) {
- if (!typeIntrospection.fields) {
- throw new Error("Introspection result missing fields: ".concat(inspect(typeIntrospection), "."));
- }
- return keyValMap(typeIntrospection.fields, function (fieldIntrospection) {
- return fieldIntrospection.name;
- }, buildField);
- }
- function buildField(fieldIntrospection) {
- var type = getType(fieldIntrospection.type);
- if (!isOutputType(type)) {
- var typeStr = inspect(type);
- throw new Error("Introspection must provide output type for fields, but received: ".concat(typeStr, "."));
- }
- if (!fieldIntrospection.args) {
- var fieldIntrospectionStr = inspect(fieldIntrospection);
- throw new Error("Introspection result missing field args: ".concat(fieldIntrospectionStr, "."));
- }
- return {
- description: fieldIntrospection.description,
- deprecationReason: fieldIntrospection.deprecationReason,
- type: type,
- args: buildInputValueDefMap(fieldIntrospection.args)
- };
- }
- function buildInputValueDefMap(inputValueIntrospections) {
- return keyValMap(inputValueIntrospections, function (inputValue) {
- return inputValue.name;
- }, buildInputValue);
- }
- function buildInputValue(inputValueIntrospection) {
- var type = getType(inputValueIntrospection.type);
- if (!isInputType(type)) {
- var typeStr = inspect(type);
- throw new Error("Introspection must provide input type for arguments, but received: ".concat(typeStr, "."));
- }
- var defaultValue = inputValueIntrospection.defaultValue != null ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) : undefined;
- return {
- description: inputValueIntrospection.description,
- type: type,
- defaultValue: defaultValue,
- deprecationReason: inputValueIntrospection.deprecationReason
- };
- }
- function buildDirective(directiveIntrospection) {
- if (!directiveIntrospection.args) {
- var directiveIntrospectionStr = inspect(directiveIntrospection);
- throw new Error("Introspection result missing directive args: ".concat(directiveIntrospectionStr, "."));
- }
- if (!directiveIntrospection.locations) {
- var _directiveIntrospectionStr = inspect(directiveIntrospection);
- throw new Error("Introspection result missing directive locations: ".concat(_directiveIntrospectionStr, "."));
- }
- return new GraphQLDirective({
- name: directiveIntrospection.name,
- description: directiveIntrospection.description,
- isRepeatable: directiveIntrospection.isRepeatable,
- locations: directiveIntrospection.locations.slice(),
- args: buildInputValueDefMap(directiveIntrospection.args)
- });
- }
- }
|