@graphql-tools/schema 8.4.0 → 8.4.1-alpha-1f8de7bd.0

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.
Files changed (3) hide show
  1. package/index.js +98 -91
  2. package/index.mjs +100 -93
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -108,94 +108,12 @@ function extendResolversFromInterfaces(schema, resolvers) {
108
108
  return extendedResolvers;
109
109
  }
110
110
 
111
- function addResolversToSchema(schemaOrOptions, legacyInputResolvers, legacyInputValidationOptions) {
112
- const options = graphql.isSchema(schemaOrOptions)
113
- ? {
114
- schema: schemaOrOptions,
115
- resolvers: legacyInputResolvers !== null && legacyInputResolvers !== void 0 ? legacyInputResolvers : {},
116
- resolverValidationOptions: legacyInputValidationOptions,
117
- }
118
- : schemaOrOptions;
119
- let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options;
120
- const { requireResolversToMatchSchema = 'error', requireResolversForResolveType } = resolverValidationOptions;
121
- const resolvers = inheritResolversFromInterfaces
122
- ? extendResolversFromInterfaces(schema, inputResolvers)
123
- : inputResolvers;
124
- for (const typeName in resolvers) {
125
- const resolverValue = resolvers[typeName];
126
- const resolverType = typeof resolverValue;
127
- if (resolverType !== 'object') {
128
- throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`);
129
- }
130
- const type = schema.getType(typeName);
131
- if (type == null) {
132
- if (requireResolversToMatchSchema === 'ignore') {
133
- continue;
134
- }
135
- throw new Error(`"${typeName}" defined in resolvers, but not in schema`);
136
- }
137
- else if (graphql.isSpecifiedScalarType(type)) {
138
- // allow -- without recommending -- overriding of specified scalar types
139
- for (const fieldName in resolverValue) {
140
- if (fieldName.startsWith('__')) {
141
- type[fieldName.substring(2)] = resolverValue[fieldName];
142
- }
143
- else {
144
- type[fieldName] = resolverValue[fieldName];
145
- }
146
- }
147
- }
148
- else if (graphql.isEnumType(type)) {
149
- const values = type.getValues();
150
- for (const fieldName in resolverValue) {
151
- if (!fieldName.startsWith('__') &&
152
- !values.some(value => value.name === fieldName) &&
153
- requireResolversToMatchSchema &&
154
- requireResolversToMatchSchema !== 'ignore') {
155
- throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`);
156
- }
157
- }
158
- }
159
- else if (graphql.isUnionType(type)) {
160
- for (const fieldName in resolverValue) {
161
- if (!fieldName.startsWith('__') &&
162
- requireResolversToMatchSchema &&
163
- requireResolversToMatchSchema !== 'ignore') {
164
- throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`);
165
- }
166
- }
167
- }
168
- else if (graphql.isObjectType(type) || graphql.isInterfaceType(type)) {
169
- for (const fieldName in resolverValue) {
170
- if (!fieldName.startsWith('__')) {
171
- const fields = type.getFields();
172
- const field = fields[fieldName];
173
- if (field == null) {
174
- // Field present in resolver but not in schema
175
- if (requireResolversToMatchSchema && requireResolversToMatchSchema !== 'ignore') {
176
- throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`);
177
- }
178
- }
179
- else {
180
- // Field present in both the resolver and schema
181
- const fieldResolve = resolverValue[fieldName];
182
- if (typeof fieldResolve !== 'function' && typeof fieldResolve !== 'object') {
183
- throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`);
184
- }
185
- }
186
- }
187
- }
188
- }
189
- }
190
- schema = updateResolversInPlace
191
- ? addResolversToExistingSchema(schema, resolvers, defaultFieldResolver)
192
- : createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver);
193
- if (requireResolversForResolveType && requireResolversForResolveType !== 'ignore') {
194
- checkForResolveTypeResolver(schema, requireResolversForResolveType);
111
+ function setFieldProperties(field, propertiesObj) {
112
+ for (const propertyName in propertiesObj) {
113
+ field[propertyName] = propertiesObj[propertyName];
195
114
  }
196
- return schema;
197
115
  }
198
- function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
116
+ function addResolversToExistingSchema(schema, resolvers) {
199
117
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
200
118
  const typeMap = schema.getTypeMap();
201
119
  for (const typeName in resolvers) {
@@ -252,7 +170,7 @@ function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
252
170
  enumValueConfigMap[fieldName].value = resolverValue[fieldName];
253
171
  }
254
172
  }
255
- typeMap[typeName] = new graphql.GraphQLEnumType(config);
173
+ Object.assign(typeMap[typeName], new graphql.GraphQLEnumType(config));
256
174
  }
257
175
  else if (graphql.isUnionType(type)) {
258
176
  for (const fieldName in resolverValue) {
@@ -283,10 +201,99 @@ function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
283
201
  }
284
202
  }
285
203
  }
204
+ }
205
+
206
+ function addResolversToSchema(schemaOrOptions, legacyInputResolvers, legacyInputValidationOptions) {
207
+ const options = graphql.isSchema(schemaOrOptions)
208
+ ? {
209
+ schema: schemaOrOptions,
210
+ resolvers: legacyInputResolvers !== null && legacyInputResolvers !== void 0 ? legacyInputResolvers : {},
211
+ resolverValidationOptions: legacyInputValidationOptions,
212
+ }
213
+ : schemaOrOptions;
214
+ let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options;
215
+ const { requireResolversToMatchSchema = 'error', requireResolversForResolveType } = resolverValidationOptions;
216
+ const resolvers = inheritResolversFromInterfaces
217
+ ? extendResolversFromInterfaces(schema, inputResolvers)
218
+ : inputResolvers;
219
+ for (const typeName in resolvers) {
220
+ const resolverValue = resolvers[typeName];
221
+ const resolverType = typeof resolverValue;
222
+ if (resolverType !== 'object') {
223
+ throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`);
224
+ }
225
+ const type = schema.getType(typeName);
226
+ if (type == null) {
227
+ if (requireResolversToMatchSchema === 'ignore') {
228
+ continue;
229
+ }
230
+ throw new Error(`"${typeName}" defined in resolvers, but not in schema`);
231
+ }
232
+ else if (graphql.isSpecifiedScalarType(type)) {
233
+ // allow -- without recommending -- overriding of specified scalar types
234
+ for (const fieldName in resolverValue) {
235
+ if (fieldName.startsWith('__')) {
236
+ type[fieldName.substring(2)] = resolverValue[fieldName];
237
+ }
238
+ else {
239
+ type[fieldName] = resolverValue[fieldName];
240
+ }
241
+ }
242
+ }
243
+ else if (graphql.isEnumType(type)) {
244
+ const values = type.getValues();
245
+ for (const fieldName in resolverValue) {
246
+ if (!fieldName.startsWith('__') &&
247
+ !values.some(value => value.name === fieldName) &&
248
+ requireResolversToMatchSchema &&
249
+ requireResolversToMatchSchema !== 'ignore') {
250
+ throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`);
251
+ }
252
+ }
253
+ }
254
+ else if (graphql.isUnionType(type)) {
255
+ for (const fieldName in resolverValue) {
256
+ if (!fieldName.startsWith('__') &&
257
+ requireResolversToMatchSchema &&
258
+ requireResolversToMatchSchema !== 'ignore') {
259
+ throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`);
260
+ }
261
+ }
262
+ }
263
+ else if (graphql.isObjectType(type) || graphql.isInterfaceType(type)) {
264
+ for (const fieldName in resolverValue) {
265
+ if (!fieldName.startsWith('__')) {
266
+ const fields = type.getFields();
267
+ const field = fields[fieldName];
268
+ if (field == null) {
269
+ // Field present in resolver but not in schema
270
+ if (requireResolversToMatchSchema && requireResolversToMatchSchema !== 'ignore') {
271
+ throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`);
272
+ }
273
+ }
274
+ else {
275
+ // Field present in both the resolver and schema
276
+ const fieldResolve = resolverValue[fieldName];
277
+ if (typeof fieldResolve !== 'function' && typeof fieldResolve !== 'object') {
278
+ throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`);
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ schema = updateResolversInPlace
286
+ ? addResolversToExistingSchemaWithHealing(schema, resolvers, defaultFieldResolver)
287
+ : createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver);
288
+ if (requireResolversForResolveType && requireResolversForResolveType !== 'ignore') {
289
+ checkForResolveTypeResolver(schema, requireResolversForResolveType);
290
+ }
291
+ return schema;
292
+ }
293
+ function addResolversToExistingSchemaWithHealing(schema, resolvers, defaultFieldResolver) {
294
+ addResolversToExistingSchema(schema, resolvers);
286
295
  // serialize all default values prior to healing fields with new scalar/enum types.
287
296
  utils.forEachDefaultValue(schema, utils.serializeInputValue);
288
- // schema may have new scalar/enum types that require healing
289
- utils.healSchema(schema);
290
297
  // reparse all default values with new parsing functions.
291
298
  utils.forEachDefaultValue(schema, utils.parseInputValue);
292
299
  if (defaultFieldResolver != null) {
@@ -404,7 +411,7 @@ function createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver) {
404
411
  newFieldConfig.resolve = fieldResolve.bind(resolverValue);
405
412
  }
406
413
  else {
407
- setFieldProperties(newFieldConfig, fieldResolve);
414
+ setFieldProperties$1(newFieldConfig, fieldResolve);
408
415
  }
409
416
  return newFieldConfig;
410
417
  }
@@ -421,7 +428,7 @@ function createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver) {
421
428
  }
422
429
  return schema;
423
430
  }
424
- function setFieldProperties(field, propertiesObj) {
431
+ function setFieldProperties$1(field, propertiesObj) {
425
432
  for (const propertyName in propertiesObj) {
426
433
  field[propertyName] = propertiesObj[propertyName];
427
434
  }
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { isScalarType, getNamedType, defaultFieldResolver, isSchema, isSpecifiedScalarType, isEnumType, isUnionType, isObjectType, isInterfaceType, GraphQLEnumType, GraphQLScalarType, GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType, buildSchema, buildASTSchema } from 'graphql';
2
- import { forEachField, mapSchema, MapperKind, forEachDefaultValue, serializeInputValue, healSchema, parseInputValue, pruneSchema, asArray, getResolversFromSchema } from '@graphql-tools/utils';
1
+ import { isScalarType, getNamedType, defaultFieldResolver, isEnumType, GraphQLEnumType, isUnionType, isObjectType, isInterfaceType, isSchema, isSpecifiedScalarType, GraphQLScalarType, GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType, buildSchema, buildASTSchema } from 'graphql';
2
+ import { forEachField, mapSchema, MapperKind, forEachDefaultValue, serializeInputValue, parseInputValue, pruneSchema, asArray, getResolversFromSchema } from '@graphql-tools/utils';
3
3
  import { mergeTypeDefs, mergeResolvers, mergeExtensions, applyExtensions, extractExtensionsFromSchema } from '@graphql-tools/merge';
4
4
 
5
5
  function assertResolversPresent(schema, resolverValidationOptions = {}) {
@@ -104,94 +104,12 @@ function extendResolversFromInterfaces(schema, resolvers) {
104
104
  return extendedResolvers;
105
105
  }
106
106
 
107
- function addResolversToSchema(schemaOrOptions, legacyInputResolvers, legacyInputValidationOptions) {
108
- const options = isSchema(schemaOrOptions)
109
- ? {
110
- schema: schemaOrOptions,
111
- resolvers: legacyInputResolvers !== null && legacyInputResolvers !== void 0 ? legacyInputResolvers : {},
112
- resolverValidationOptions: legacyInputValidationOptions,
113
- }
114
- : schemaOrOptions;
115
- let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options;
116
- const { requireResolversToMatchSchema = 'error', requireResolversForResolveType } = resolverValidationOptions;
117
- const resolvers = inheritResolversFromInterfaces
118
- ? extendResolversFromInterfaces(schema, inputResolvers)
119
- : inputResolvers;
120
- for (const typeName in resolvers) {
121
- const resolverValue = resolvers[typeName];
122
- const resolverType = typeof resolverValue;
123
- if (resolverType !== 'object') {
124
- throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`);
125
- }
126
- const type = schema.getType(typeName);
127
- if (type == null) {
128
- if (requireResolversToMatchSchema === 'ignore') {
129
- continue;
130
- }
131
- throw new Error(`"${typeName}" defined in resolvers, but not in schema`);
132
- }
133
- else if (isSpecifiedScalarType(type)) {
134
- // allow -- without recommending -- overriding of specified scalar types
135
- for (const fieldName in resolverValue) {
136
- if (fieldName.startsWith('__')) {
137
- type[fieldName.substring(2)] = resolverValue[fieldName];
138
- }
139
- else {
140
- type[fieldName] = resolverValue[fieldName];
141
- }
142
- }
143
- }
144
- else if (isEnumType(type)) {
145
- const values = type.getValues();
146
- for (const fieldName in resolverValue) {
147
- if (!fieldName.startsWith('__') &&
148
- !values.some(value => value.name === fieldName) &&
149
- requireResolversToMatchSchema &&
150
- requireResolversToMatchSchema !== 'ignore') {
151
- throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`);
152
- }
153
- }
154
- }
155
- else if (isUnionType(type)) {
156
- for (const fieldName in resolverValue) {
157
- if (!fieldName.startsWith('__') &&
158
- requireResolversToMatchSchema &&
159
- requireResolversToMatchSchema !== 'ignore') {
160
- throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`);
161
- }
162
- }
163
- }
164
- else if (isObjectType(type) || isInterfaceType(type)) {
165
- for (const fieldName in resolverValue) {
166
- if (!fieldName.startsWith('__')) {
167
- const fields = type.getFields();
168
- const field = fields[fieldName];
169
- if (field == null) {
170
- // Field present in resolver but not in schema
171
- if (requireResolversToMatchSchema && requireResolversToMatchSchema !== 'ignore') {
172
- throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`);
173
- }
174
- }
175
- else {
176
- // Field present in both the resolver and schema
177
- const fieldResolve = resolverValue[fieldName];
178
- if (typeof fieldResolve !== 'function' && typeof fieldResolve !== 'object') {
179
- throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`);
180
- }
181
- }
182
- }
183
- }
184
- }
185
- }
186
- schema = updateResolversInPlace
187
- ? addResolversToExistingSchema(schema, resolvers, defaultFieldResolver)
188
- : createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver);
189
- if (requireResolversForResolveType && requireResolversForResolveType !== 'ignore') {
190
- checkForResolveTypeResolver(schema, requireResolversForResolveType);
107
+ function setFieldProperties(field, propertiesObj) {
108
+ for (const propertyName in propertiesObj) {
109
+ field[propertyName] = propertiesObj[propertyName];
191
110
  }
192
- return schema;
193
111
  }
194
- function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
112
+ function addResolversToExistingSchema(schema, resolvers) {
195
113
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
196
114
  const typeMap = schema.getTypeMap();
197
115
  for (const typeName in resolvers) {
@@ -248,7 +166,7 @@ function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
248
166
  enumValueConfigMap[fieldName].value = resolverValue[fieldName];
249
167
  }
250
168
  }
251
- typeMap[typeName] = new GraphQLEnumType(config);
169
+ Object.assign(typeMap[typeName], new GraphQLEnumType(config));
252
170
  }
253
171
  else if (isUnionType(type)) {
254
172
  for (const fieldName in resolverValue) {
@@ -279,10 +197,99 @@ function addResolversToExistingSchema(schema, resolvers, defaultFieldResolver) {
279
197
  }
280
198
  }
281
199
  }
200
+ }
201
+
202
+ function addResolversToSchema(schemaOrOptions, legacyInputResolvers, legacyInputValidationOptions) {
203
+ const options = isSchema(schemaOrOptions)
204
+ ? {
205
+ schema: schemaOrOptions,
206
+ resolvers: legacyInputResolvers !== null && legacyInputResolvers !== void 0 ? legacyInputResolvers : {},
207
+ resolverValidationOptions: legacyInputValidationOptions,
208
+ }
209
+ : schemaOrOptions;
210
+ let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options;
211
+ const { requireResolversToMatchSchema = 'error', requireResolversForResolveType } = resolverValidationOptions;
212
+ const resolvers = inheritResolversFromInterfaces
213
+ ? extendResolversFromInterfaces(schema, inputResolvers)
214
+ : inputResolvers;
215
+ for (const typeName in resolvers) {
216
+ const resolverValue = resolvers[typeName];
217
+ const resolverType = typeof resolverValue;
218
+ if (resolverType !== 'object') {
219
+ throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`);
220
+ }
221
+ const type = schema.getType(typeName);
222
+ if (type == null) {
223
+ if (requireResolversToMatchSchema === 'ignore') {
224
+ continue;
225
+ }
226
+ throw new Error(`"${typeName}" defined in resolvers, but not in schema`);
227
+ }
228
+ else if (isSpecifiedScalarType(type)) {
229
+ // allow -- without recommending -- overriding of specified scalar types
230
+ for (const fieldName in resolverValue) {
231
+ if (fieldName.startsWith('__')) {
232
+ type[fieldName.substring(2)] = resolverValue[fieldName];
233
+ }
234
+ else {
235
+ type[fieldName] = resolverValue[fieldName];
236
+ }
237
+ }
238
+ }
239
+ else if (isEnumType(type)) {
240
+ const values = type.getValues();
241
+ for (const fieldName in resolverValue) {
242
+ if (!fieldName.startsWith('__') &&
243
+ !values.some(value => value.name === fieldName) &&
244
+ requireResolversToMatchSchema &&
245
+ requireResolversToMatchSchema !== 'ignore') {
246
+ throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`);
247
+ }
248
+ }
249
+ }
250
+ else if (isUnionType(type)) {
251
+ for (const fieldName in resolverValue) {
252
+ if (!fieldName.startsWith('__') &&
253
+ requireResolversToMatchSchema &&
254
+ requireResolversToMatchSchema !== 'ignore') {
255
+ throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`);
256
+ }
257
+ }
258
+ }
259
+ else if (isObjectType(type) || isInterfaceType(type)) {
260
+ for (const fieldName in resolverValue) {
261
+ if (!fieldName.startsWith('__')) {
262
+ const fields = type.getFields();
263
+ const field = fields[fieldName];
264
+ if (field == null) {
265
+ // Field present in resolver but not in schema
266
+ if (requireResolversToMatchSchema && requireResolversToMatchSchema !== 'ignore') {
267
+ throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`);
268
+ }
269
+ }
270
+ else {
271
+ // Field present in both the resolver and schema
272
+ const fieldResolve = resolverValue[fieldName];
273
+ if (typeof fieldResolve !== 'function' && typeof fieldResolve !== 'object') {
274
+ throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`);
275
+ }
276
+ }
277
+ }
278
+ }
279
+ }
280
+ }
281
+ schema = updateResolversInPlace
282
+ ? addResolversToExistingSchemaWithHealing(schema, resolvers, defaultFieldResolver)
283
+ : createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver);
284
+ if (requireResolversForResolveType && requireResolversForResolveType !== 'ignore') {
285
+ checkForResolveTypeResolver(schema, requireResolversForResolveType);
286
+ }
287
+ return schema;
288
+ }
289
+ function addResolversToExistingSchemaWithHealing(schema, resolvers, defaultFieldResolver) {
290
+ addResolversToExistingSchema(schema, resolvers);
282
291
  // serialize all default values prior to healing fields with new scalar/enum types.
283
292
  forEachDefaultValue(schema, serializeInputValue);
284
- // schema may have new scalar/enum types that require healing
285
- healSchema(schema);
286
293
  // reparse all default values with new parsing functions.
287
294
  forEachDefaultValue(schema, parseInputValue);
288
295
  if (defaultFieldResolver != null) {
@@ -400,7 +407,7 @@ function createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver) {
400
407
  newFieldConfig.resolve = fieldResolve.bind(resolverValue);
401
408
  }
402
409
  else {
403
- setFieldProperties(newFieldConfig, fieldResolve);
410
+ setFieldProperties$1(newFieldConfig, fieldResolve);
404
411
  }
405
412
  return newFieldConfig;
406
413
  }
@@ -417,7 +424,7 @@ function createNewSchemaWithResolvers(schema, resolvers, defaultFieldResolver) {
417
424
  }
418
425
  return schema;
419
426
  }
420
- function setFieldProperties(field, propertiesObj) {
427
+ function setFieldProperties$1(field, propertiesObj) {
421
428
  for (const propertyName in propertiesObj) {
422
429
  field[propertyName] = propertiesObj[propertyName];
423
430
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/schema",
3
- "version": "8.4.0",
3
+ "version": "8.4.1-alpha-1f8de7bd.0",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {