UNPKG

3.03 kBJavaScriptView Raw
1"use strict";
2// addTypes uses toConfig to create a new schema with a new or replaced
3// type or directive. Rewiring is employed so that the replaced type can be
4// reconnected with the existing types.
5//
6// Rewiring is employed even for new types or directives as a convenience, so
7// that type references within the new type or directive do not have to be to
8// the identical objects within the original schema.
9//
10// In fact, the type references could even be stub types with entirely different
11// fields, as long as the type references share the same name as the desired
12// type within the original schema's type map.
13//
14// This makes it easy to perform simple schema operations (e.g. adding a new
15// type with a fiew fields removed from an existing type) that could normally be
16// performed by using toConfig directly, but is blocked if any intervening
17// more advanced schema operations have caused the types to be recreated via
18// rewiring.
19//
20// Type recreation happens, for example, with every use of mapSchema, as the
21// types are always rewired. If fields are selected and removed using
22// mapSchema, adding those fields to a new type can no longer be simply done
23// by toConfig, as the types are not the identical JavaScript objects, and
24// schema creation will fail with errors referencing multiple types with the
25// same names.
26//
27// enhanceSchema can fill this gap by adding an additional round of rewiring.
28//
29Object.defineProperty(exports, "__esModule", { value: true });
30exports.addTypes = void 0;
31const graphql_1 = require("graphql");
32const getObjectTypeFromTypeMap_js_1 = require("./getObjectTypeFromTypeMap.js");
33const rewire_js_1 = require("./rewire.js");
34function addTypes(schema, newTypesOrDirectives) {
35 const config = schema.toConfig();
36 const originalTypeMap = {};
37 for (const type of config.types) {
38 originalTypeMap[type.name] = type;
39 }
40 const originalDirectiveMap = {};
41 for (const directive of config.directives) {
42 originalDirectiveMap[directive.name] = directive;
43 }
44 for (const newTypeOrDirective of newTypesOrDirectives) {
45 if ((0, graphql_1.isNamedType)(newTypeOrDirective)) {
46 originalTypeMap[newTypeOrDirective.name] = newTypeOrDirective;
47 }
48 else if ((0, graphql_1.isDirective)(newTypeOrDirective)) {
49 originalDirectiveMap[newTypeOrDirective.name] = newTypeOrDirective;
50 }
51 }
52 const { typeMap, directives } = (0, rewire_js_1.rewireTypes)(originalTypeMap, Object.values(originalDirectiveMap));
53 return new graphql_1.GraphQLSchema({
54 ...config,
55 query: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getQueryType()),
56 mutation: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getMutationType()),
57 subscription: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getSubscriptionType()),
58 types: Object.values(typeMap),
59 directives,
60 });
61}
62exports.addTypes = addTypes;