@graphql-tools/schema 7.1.0-alpha-796c16fa.0 → 7.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,4 +43,4 @@ import { IExecutableSchemaDefinition } from './types';
43
43
  * })
44
44
  * ```
45
45
  */
46
- export declare function makeExecutableSchema<TContext = any>({ typeDefs, resolvers, logger, allowUndefinedInResolve, resolverValidationOptions, directiveResolvers, schemaDirectives, schemaTransforms, parseOptions, inheritResolversFromInterfaces, pruningOptions, }: IExecutableSchemaDefinition<TContext>): import("graphql").GraphQLSchema;
46
+ export declare function makeExecutableSchema<TContext = any>({ typeDefs, resolvers, logger, allowUndefinedInResolve, resolverValidationOptions, directiveResolvers, schemaDirectives, schemaTransforms: userProvidedSchemaTransforms, parseOptions, inheritResolversFromInterfaces, pruningOptions, updateResolversInPlace, noExtensionExtraction, }: IExecutableSchemaDefinition<TContext>): import("graphql").GraphQLSchema;
package/es5/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@graphql-tools/schema/es5",
3
- "version": "7.1.0-alpha-796c16fa.0",
3
+ "version": "7.1.2",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "graphql": "^14.0.0 || ^15.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-tools/utils": "^7.0.0",
10
+ "@graphql-tools/utils": "^7.1.2",
11
11
  "tslib": "~2.0.1"
12
12
  },
13
13
  "repository": "git@github.com:ardatan/graphql-tools.git",
package/es5/types.d.ts CHANGED
@@ -41,7 +41,7 @@ export interface IExecutableSchemaDefinition<TContext = any> {
41
41
  /**
42
42
  * An array of schema transformation functions
43
43
  */
44
- schemaTransforms?: Array<(originalWrappingSchema: GraphQLSchema) => GraphQLSchema>;
44
+ schemaTransforms?: ExecutableSchemaTransformation[];
45
45
  /**
46
46
  * Additional options for parsing the type definitions if they are provided
47
47
  * as a string
@@ -56,4 +56,13 @@ export interface IExecutableSchemaDefinition<TContext = any> {
56
56
  * Additional options for removing unused types from the schema
57
57
  */
58
58
  pruningOptions?: PruneSchemaOptions;
59
+ /**
60
+ * Do not create a schema again and use the one from `buildASTSchema`
61
+ */
62
+ updateResolversInPlace?: boolean;
63
+ /**
64
+ * Do not extract and apply extensions seperately and leave it to `buildASTSchema`
65
+ */
66
+ noExtensionExtraction?: boolean;
59
67
  }
68
+ export declare type ExecutableSchemaTransformation = (schema: GraphQLSchema) => GraphQLSchema;
@@ -1,10 +1,22 @@
1
1
  import { DocumentNode, DefinitionNode } from 'graphql';
2
- export declare function extractExtensionDefinitions(ast: DocumentNode): {
2
+ export declare function filterAndExtractExtensionDefinitions(ast: DocumentNode): {
3
+ typesAst: {
4
+ definitions: DefinitionNode[];
5
+ kind: "Document";
6
+ loc?: import("graphql").Location;
7
+ };
8
+ extensionsAst: {
9
+ definitions: DefinitionNode[];
10
+ kind: "Document";
11
+ loc?: import("graphql").Location;
12
+ };
13
+ };
14
+ export declare function filterExtensionDefinitions(ast: DocumentNode): {
3
15
  definitions: DefinitionNode[];
4
16
  kind: "Document";
5
17
  loc?: import("graphql").Location;
6
18
  };
7
- export declare function filterExtensionDefinitions(ast: DocumentNode): {
19
+ export declare function extractExtensionDefinitions(ast: DocumentNode): {
8
20
  definitions: DefinitionNode[];
9
21
  kind: "Document";
10
22
  loc?: import("graphql").Location;
package/index.cjs.js CHANGED
@@ -148,73 +148,80 @@ function attachDirectiveResolvers(schema, directiveResolvers) {
148
148
  });
149
149
  }
150
150
 
151
- function extractExtensionDefinitions(ast) {
152
- const extensionDefs = ast.definitions.filter((def) => def.kind === graphql.Kind.OBJECT_TYPE_EXTENSION ||
153
- def.kind === graphql.Kind.INTERFACE_TYPE_EXTENSION ||
154
- def.kind === graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION ||
155
- def.kind === graphql.Kind.UNION_TYPE_EXTENSION ||
156
- def.kind === graphql.Kind.ENUM_TYPE_EXTENSION ||
157
- def.kind === graphql.Kind.SCALAR_TYPE_EXTENSION ||
158
- def.kind === graphql.Kind.SCHEMA_EXTENSION);
151
+ const isExtensionNode = (def) => def.kind === graphql.Kind.OBJECT_TYPE_EXTENSION ||
152
+ def.kind === graphql.Kind.INTERFACE_TYPE_EXTENSION ||
153
+ def.kind === graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION ||
154
+ def.kind === graphql.Kind.UNION_TYPE_EXTENSION ||
155
+ def.kind === graphql.Kind.ENUM_TYPE_EXTENSION ||
156
+ def.kind === graphql.Kind.SCALAR_TYPE_EXTENSION ||
157
+ def.kind === graphql.Kind.SCHEMA_EXTENSION;
158
+ function filterAndExtractExtensionDefinitions(ast) {
159
+ const extensionDefs = [];
160
+ const typesDefs = [];
161
+ ast.definitions.forEach(def => {
162
+ if (isExtensionNode(def)) {
163
+ extensionDefs.push(def);
164
+ }
165
+ else {
166
+ typesDefs.push(def);
167
+ }
168
+ });
159
169
  return {
160
- ...ast,
161
- definitions: extensionDefs,
170
+ typesAst: {
171
+ ...ast,
172
+ definitions: typesDefs,
173
+ },
174
+ extensionsAst: {
175
+ ...ast,
176
+ definitions: extensionDefs,
177
+ },
162
178
  };
163
179
  }
164
180
  function filterExtensionDefinitions(ast) {
165
- const extensionDefs = ast.definitions.filter((def) => def.kind !== graphql.Kind.OBJECT_TYPE_EXTENSION &&
166
- def.kind !== graphql.Kind.INTERFACE_TYPE_EXTENSION &&
167
- def.kind !== graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION &&
168
- def.kind !== graphql.Kind.UNION_TYPE_EXTENSION &&
169
- def.kind !== graphql.Kind.ENUM_TYPE_EXTENSION &&
170
- def.kind !== graphql.Kind.SCALAR_TYPE_EXTENSION &&
171
- def.kind !== graphql.Kind.SCHEMA_EXTENSION);
172
- return {
173
- ...ast,
174
- definitions: extensionDefs,
175
- };
181
+ const { typesAst } = filterAndExtractExtensionDefinitions(ast);
182
+ return typesAst;
183
+ }
184
+ function extractExtensionDefinitions(ast) {
185
+ const { extensionsAst } = filterAndExtractExtensionDefinitions(ast);
186
+ return extensionsAst;
176
187
  }
177
188
 
178
- function concatenateTypeDefs(typeDefinitionsAry, calledFunctionRefs = []) {
179
- let resolvedTypeDefinitions = [];
189
+ function concatenateTypeDefs(typeDefinitionsAry, calledFunctionRefs = new Set()) {
190
+ const resolvedTypeDefinitions = new Set();
180
191
  typeDefinitionsAry.forEach((typeDef) => {
181
192
  if (typeof typeDef === 'function') {
182
- if (calledFunctionRefs.indexOf(typeDef) === -1) {
183
- calledFunctionRefs.push(typeDef);
184
- resolvedTypeDefinitions = resolvedTypeDefinitions.concat(concatenateTypeDefs(typeDef(), calledFunctionRefs));
193
+ if (!calledFunctionRefs.has(typeDef)) {
194
+ calledFunctionRefs.add(typeDef);
195
+ resolvedTypeDefinitions.add(concatenateTypeDefs(typeDef(), calledFunctionRefs));
185
196
  }
186
197
  }
187
198
  else if (typeof typeDef === 'string') {
188
- resolvedTypeDefinitions.push(typeDef.trim());
199
+ resolvedTypeDefinitions.add(typeDef.trim());
189
200
  }
190
201
  else if (typeDef.kind !== undefined) {
191
- resolvedTypeDefinitions.push(graphql.print(typeDef).trim());
202
+ resolvedTypeDefinitions.add(graphql.print(typeDef).trim());
192
203
  }
193
204
  else {
194
205
  const type = typeof typeDef;
195
206
  throw new Error(`typeDef array must contain only strings, documents, or functions, got ${type}`);
196
207
  }
197
208
  });
198
- return uniq(resolvedTypeDefinitions.map(x => x.trim())).join('\n');
199
- }
200
- function uniq(array) {
201
- return array.reduce((accumulator, currentValue) => accumulator.indexOf(currentValue) === -1 ? [...accumulator, currentValue] : accumulator, []);
209
+ return [...resolvedTypeDefinitions].join('\n');
202
210
  }
203
211
 
204
- function buildSchemaFromTypeDefinitions(typeDefinitions, parseOptions) {
212
+ function buildSchemaFromTypeDefinitions(typeDefinitions, parseOptions, noExtensionExtraction) {
205
213
  const document = buildDocumentFromTypeDefinitions(typeDefinitions, parseOptions);
206
- const typesAst = filterExtensionDefinitions(document);
214
+ if (noExtensionExtraction) {
215
+ return graphql.buildASTSchema(document);
216
+ }
217
+ const { typesAst, extensionsAst } = filterAndExtractExtensionDefinitions(document);
207
218
  const backcompatOptions = { commentDescriptions: true };
208
219
  let schema = graphql.buildASTSchema(typesAst, backcompatOptions);
209
- const extensionsAst = extractExtensionDefinitions(document);
210
220
  if (extensionsAst.definitions.length > 0) {
211
221
  schema = graphql.extendSchema(schema, extensionsAst, backcompatOptions);
212
222
  }
213
223
  return schema;
214
224
  }
215
- function isDocumentNode(typeDefinitions) {
216
- return typeDefinitions.kind !== undefined;
217
- }
218
225
  function buildDocumentFromTypeDefinitions(typeDefinitions, parseOptions) {
219
226
  let document;
220
227
  if (typeof typeDefinitions === 'string') {
@@ -223,7 +230,7 @@ function buildDocumentFromTypeDefinitions(typeDefinitions, parseOptions) {
223
230
  else if (Array.isArray(typeDefinitions)) {
224
231
  document = utils.parseGraphQLSDL('', concatenateTypeDefs(typeDefinitions), parseOptions).document;
225
232
  }
226
- else if (isDocumentNode(typeDefinitions)) {
233
+ else if (utils.isDocumentNode(typeDefinitions)) {
227
234
  document = typeDefinitions;
228
235
  }
229
236
  else {
@@ -287,7 +294,7 @@ function decorateWithLogger(fn, logger, hint) {
287
294
  function checkForResolveTypeResolver(schema, requireResolversForResolveType) {
288
295
  utils.mapSchema(schema, {
289
296
  [utils.MapperKind.ABSTRACT_TYPE]: type => {
290
- if (!type.resolveType && requireResolversForResolveType) {
297
+ if (!type.resolveType) {
291
298
  const message = `Type "${type.name}" is missing a "__resolveType" resolver. Pass 'ignore' into ` +
292
299
  '"resolverValidationOptions.requireResolversForResolveType" to disable this error.';
293
300
  if (requireResolversForResolveType === 'error') {
@@ -423,7 +430,9 @@ function addResolversToSchema(schemaOrOptions, legacyInputResolvers, legacyInput
423
430
  schema = updateResolversInPlace
424
431
  ? addResolversToExistingSchema(schema, resolvers, defaultFieldResolver)
425
432
  : createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver);
426
- checkForResolveTypeResolver(schema, requireResolversForResolveType);
433
+ if (requireResolversForResolveType || requireResolversForResolveType !== 'ignore') {
434
+ checkForResolveTypeResolver(schema, requireResolversForResolveType);
435
+ }
427
436
  return schema;
428
437
  }
429
438
  function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
@@ -745,7 +754,7 @@ function addCatchUndefinedToSchema(schema) {
745
754
  * })
746
755
  * ```
747
756
  */
748
- function makeExecutableSchema({ typeDefs, resolvers = {}, logger, allowUndefinedInResolve = true, resolverValidationOptions = {}, directiveResolvers, schemaDirectives, schemaTransforms = [], parseOptions = {}, inheritResolversFromInterfaces = false, pruningOptions, }) {
757
+ function makeExecutableSchema({ typeDefs, resolvers = {}, logger, allowUndefinedInResolve = true, resolverValidationOptions = {}, directiveResolvers, schemaDirectives, schemaTransforms: userProvidedSchemaTransforms, parseOptions = {}, inheritResolversFromInterfaces = false, pruningOptions, updateResolversInPlace = false, noExtensionExtraction = false, }) {
749
758
  // Validate and clean up arguments
750
759
  if (typeof resolverValidationOptions !== 'object') {
751
760
  throw new Error('Expected `resolverValidationOptions` to be an object');
@@ -753,42 +762,54 @@ function makeExecutableSchema({ typeDefs, resolvers = {}, logger, allowUndefined
753
762
  if (!typeDefs) {
754
763
  throw new Error('Must provide typeDefs');
755
764
  }
756
- // We allow passing in an array of resolver maps, in which case we merge them
757
- const resolverMap = Array.isArray(resolvers) ? resolvers.reduce(utils.mergeDeep, {}) : resolvers;
758
765
  // Arguments are now validated and cleaned up
759
- let schema = buildSchemaFromTypeDefinitions(typeDefs, parseOptions);
760
- schema = addResolversToSchema({
761
- schema,
762
- resolvers: resolverMap,
763
- resolverValidationOptions,
764
- inheritResolversFromInterfaces,
765
- });
766
- if (Object.keys(resolverValidationOptions).length > 0) {
767
- assertResolversPresent(schema, resolverValidationOptions);
768
- }
766
+ const schemaTransforms = [
767
+ schema => {
768
+ // We allow passing in an array of resolver maps, in which case we merge them
769
+ const resolverMap = Array.isArray(resolvers) ? resolvers.reduce(utils.mergeDeep, {}) : resolvers;
770
+ const schemaWithResolvers = addResolversToSchema({
771
+ schema,
772
+ resolvers: resolverMap,
773
+ resolverValidationOptions,
774
+ inheritResolversFromInterfaces,
775
+ updateResolversInPlace,
776
+ });
777
+ if (Object.keys(resolverValidationOptions).length > 0) {
778
+ assertResolversPresent(schemaWithResolvers, resolverValidationOptions);
779
+ }
780
+ return schemaWithResolvers;
781
+ },
782
+ ];
769
783
  if (!allowUndefinedInResolve) {
770
- schema = addCatchUndefinedToSchema(schema);
784
+ schemaTransforms.push(addCatchUndefinedToSchema);
771
785
  }
772
786
  if (logger != null) {
773
- schema = addErrorLoggingToSchema(schema, logger);
787
+ schemaTransforms.push(schema => addErrorLoggingToSchema(schema, logger));
774
788
  }
775
789
  if (typeof resolvers['__schema'] === 'function') {
776
790
  // TODO a bit of a hack now, better rewrite generateSchema to attach it there.
777
791
  // not doing that now, because I'd have to rewrite a lot of tests.
778
- schema = addSchemaLevelResolver(schema, resolvers['__schema']);
792
+ schemaTransforms.push(schema => addSchemaLevelResolver(schema, resolvers['__schema']));
793
+ }
794
+ if (userProvidedSchemaTransforms) {
795
+ schemaTransforms.push(schema => userProvidedSchemaTransforms.reduce((s, schemaTransform) => schemaTransform(s), schema));
779
796
  }
780
- schemaTransforms.forEach(schemaTransform => {
781
- schema = schemaTransform(schema);
782
- });
783
797
  // directive resolvers are implemented using SchemaDirectiveVisitor.visitSchemaDirectives
784
798
  // schema visiting modifies the schema in place
785
799
  if (directiveResolvers != null) {
786
- schema = attachDirectiveResolvers(schema, directiveResolvers);
800
+ schemaTransforms.push(schema => attachDirectiveResolvers(schema, directiveResolvers));
787
801
  }
788
802
  if (schemaDirectives != null) {
789
- utils.SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives);
803
+ schemaTransforms.push(schema => {
804
+ utils.SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives);
805
+ return schema;
806
+ });
807
+ }
808
+ if (pruningOptions) {
809
+ schemaTransforms.push(utils.pruneSchema);
790
810
  }
791
- return pruningOptions ? utils.pruneSchema(schema, pruningOptions) : schema;
811
+ const schemaFromTypeDefs = buildSchemaFromTypeDefinitions(typeDefs, parseOptions, noExtensionExtraction);
812
+ return schemaTransforms.reduce((schema, schemaTransform) => schemaTransform(schema), schemaFromTypeDefs);
792
813
  }
793
814
 
794
815
  exports.addCatchUndefinedToSchema = addCatchUndefinedToSchema;
@@ -805,6 +826,7 @@ exports.concatenateTypeDefs = concatenateTypeDefs;
805
826
  exports.decorateWithLogger = decorateWithLogger;
806
827
  exports.extendResolversFromInterfaces = extendResolversFromInterfaces;
807
828
  exports.extractExtensionDefinitions = extractExtensionDefinitions;
829
+ exports.filterAndExtractExtensionDefinitions = filterAndExtractExtensionDefinitions;
808
830
  exports.filterExtensionDefinitions = filterExtensionDefinitions;
809
831
  exports.makeExecutableSchema = makeExecutableSchema;
810
832
  //# sourceMappingURL=index.cjs.js.map