@graphql-tools/schema
Advanced tools
Comparing version 6.0.0-alpha-66debc3.0 to 6.0.0-alpha-843b53d.0
113
index.cjs.js
@@ -344,3 +344,3 @@ 'use strict'; | ||
: schemaOrOptions; | ||
let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, } = options; | ||
let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options; | ||
const { allowResolversNotInSchema = false, requireResolversForResolveType } = resolverValidationOptions; | ||
@@ -371,3 +371,2 @@ const resolvers = inheritResolversFromInterfaces | ||
// allow -- without recommending -- overriding of specified scalar types | ||
const resolverValue = resolvers[typeName]; | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
@@ -384,2 +383,111 @@ if (fieldName.startsWith('__')) { | ||
}); | ||
schema = updateResolversInPlace | ||
? addResolversToExistingSchema({ | ||
schema, | ||
resolvers, | ||
defaultFieldResolver, | ||
allowResolversNotInSchema, | ||
}) | ||
: createNewSchemaWithResolvers({ | ||
schema, | ||
resolvers, | ||
defaultFieldResolver, | ||
allowResolversNotInSchema, | ||
}); | ||
checkForResolveTypeResolver(schema, requireResolversForResolveType); | ||
return schema; | ||
} | ||
function addResolversToExistingSchema({ schema, resolvers, defaultFieldResolver, allowResolversNotInSchema, }) { | ||
const typeMap = schema.getTypeMap(); | ||
Object.keys(resolvers).forEach(typeName => { | ||
if (typeName !== '__schema') { | ||
const type = schema.getType(typeName); | ||
const resolverValue = resolvers[typeName]; | ||
if (graphql.isScalarType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else { | ||
type[fieldName] = resolverValue[fieldName]; | ||
} | ||
}); | ||
} | ||
else if (graphql.isEnumType(type)) { | ||
const config = type.toConfig(); | ||
const enumValueConfigMap = config.values; | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
config[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else if (!enumValueConfigMap[fieldName]) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`); | ||
} | ||
else { | ||
enumValueConfigMap[fieldName].value = resolverValue[fieldName]; | ||
} | ||
}); | ||
typeMap[typeName] = new graphql.GraphQLEnumType(config); | ||
} | ||
else if (graphql.isUnionType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
return; | ||
} | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`); | ||
}); | ||
} | ||
else if (graphql.isObjectType(type) || graphql.isInterfaceType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
// this is for isTypeOf and resolveType and all the other stuff. | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
return; | ||
} | ||
const fields = type.getFields(); | ||
const field = fields[fieldName]; | ||
if (field == null) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`); | ||
} | ||
const fieldResolve = resolverValue[fieldName]; | ||
if (typeof fieldResolve === 'function') { | ||
// for convenience. Allows shorter syntax in resolver definition file | ||
field.resolve = fieldResolve; | ||
} | ||
else { | ||
if (typeof fieldResolve !== 'object') { | ||
throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`); | ||
} | ||
setFieldProperties(field, fieldResolve); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
// serialize all default values prior to healing fields with new scalar/enum types. | ||
utils.forEachDefaultValue(schema, utils.serializeInputValue); | ||
// schema may have new scalar/enum types that require healing | ||
utils.healSchema(schema); | ||
// reparse all default values with new parsing functions. | ||
utils.forEachDefaultValue(schema, utils.parseInputValue); | ||
if (defaultFieldResolver != null) { | ||
utils.forEachField(schema, field => { | ||
if (!field.resolve) { | ||
field.resolve = defaultFieldResolver; | ||
} | ||
}); | ||
} | ||
return schema; | ||
} | ||
function createNewSchemaWithResolvers({ schema, resolvers, defaultFieldResolver, allowResolversNotInSchema, }) { | ||
schema = utils.mapSchema(schema, { | ||
@@ -504,3 +612,2 @@ [utils.MapperKind.SCALAR_TYPE]: type => { | ||
}); | ||
checkForResolveTypeResolver(schema, requireResolversForResolveType); | ||
if (defaultFieldResolver != null) { | ||
@@ -507,0 +614,0 @@ schema = utils.mapSchema(schema, { |
117
index.esm.js
@@ -1,3 +0,3 @@ | ||
import { defaultFieldResolver, isScalarType, getNamedType, Kind, print, buildASTSchema, extendSchema, parse, isAbstractType, isSchema, isSpecifiedScalarType, GraphQLScalarType, GraphQLEnumType, GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType } from 'graphql'; | ||
import { mapSchema, MapperKind, forEachField, getDirectives, mergeDeep, SchemaDirectiveVisitor } from '@graphql-tools/utils'; | ||
import { defaultFieldResolver, isScalarType, getNamedType, Kind, print, buildASTSchema, extendSchema, parse, isAbstractType, isSchema, isSpecifiedScalarType, isEnumType, GraphQLEnumType, isUnionType, isObjectType, isInterfaceType, GraphQLScalarType, GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType } from 'graphql'; | ||
import { mapSchema, MapperKind, forEachField, getDirectives, forEachDefaultValue, serializeInputValue, healSchema, parseInputValue, mergeDeep, SchemaDirectiveVisitor } from '@graphql-tools/utils'; | ||
@@ -340,3 +340,3 @@ // wraps all resolvers of query, mutation or subscription fields | ||
: schemaOrOptions; | ||
let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, } = options; | ||
let { schema, resolvers: inputResolvers, defaultFieldResolver, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, } = options; | ||
const { allowResolversNotInSchema = false, requireResolversForResolveType } = resolverValidationOptions; | ||
@@ -367,3 +367,2 @@ const resolvers = inheritResolversFromInterfaces | ||
// allow -- without recommending -- overriding of specified scalar types | ||
const resolverValue = resolvers[typeName]; | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
@@ -380,2 +379,111 @@ if (fieldName.startsWith('__')) { | ||
}); | ||
schema = updateResolversInPlace | ||
? addResolversToExistingSchema({ | ||
schema, | ||
resolvers, | ||
defaultFieldResolver, | ||
allowResolversNotInSchema, | ||
}) | ||
: createNewSchemaWithResolvers({ | ||
schema, | ||
resolvers, | ||
defaultFieldResolver, | ||
allowResolversNotInSchema, | ||
}); | ||
checkForResolveTypeResolver(schema, requireResolversForResolveType); | ||
return schema; | ||
} | ||
function addResolversToExistingSchema({ schema, resolvers, defaultFieldResolver, allowResolversNotInSchema, }) { | ||
const typeMap = schema.getTypeMap(); | ||
Object.keys(resolvers).forEach(typeName => { | ||
if (typeName !== '__schema') { | ||
const type = schema.getType(typeName); | ||
const resolverValue = resolvers[typeName]; | ||
if (isScalarType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else { | ||
type[fieldName] = resolverValue[fieldName]; | ||
} | ||
}); | ||
} | ||
else if (isEnumType(type)) { | ||
const config = type.toConfig(); | ||
const enumValueConfigMap = config.values; | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
config[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else if (!enumValueConfigMap[fieldName]) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${type.name}.${fieldName} was defined in resolvers, but not present within ${type.name}`); | ||
} | ||
else { | ||
enumValueConfigMap[fieldName].value = resolverValue[fieldName]; | ||
} | ||
}); | ||
typeMap[typeName] = new GraphQLEnumType(config); | ||
} | ||
else if (isUnionType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
return; | ||
} | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${type.name}.${fieldName} was defined in resolvers, but ${type.name} is not an object or interface type`); | ||
}); | ||
} | ||
else if (isObjectType(type) || isInterfaceType(type)) { | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (fieldName.startsWith('__')) { | ||
// this is for isTypeOf and resolveType and all the other stuff. | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
return; | ||
} | ||
const fields = type.getFields(); | ||
const field = fields[fieldName]; | ||
if (field == null) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${typeName}.${fieldName} defined in resolvers, but not in schema`); | ||
} | ||
const fieldResolve = resolverValue[fieldName]; | ||
if (typeof fieldResolve === 'function') { | ||
// for convenience. Allows shorter syntax in resolver definition file | ||
field.resolve = fieldResolve; | ||
} | ||
else { | ||
if (typeof fieldResolve !== 'object') { | ||
throw new Error(`Resolver ${typeName}.${fieldName} must be object or function`); | ||
} | ||
setFieldProperties(field, fieldResolve); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
// serialize all default values prior to healing fields with new scalar/enum types. | ||
forEachDefaultValue(schema, serializeInputValue); | ||
// schema may have new scalar/enum types that require healing | ||
healSchema(schema); | ||
// reparse all default values with new parsing functions. | ||
forEachDefaultValue(schema, parseInputValue); | ||
if (defaultFieldResolver != null) { | ||
forEachField(schema, field => { | ||
if (!field.resolve) { | ||
field.resolve = defaultFieldResolver; | ||
} | ||
}); | ||
} | ||
return schema; | ||
} | ||
function createNewSchemaWithResolvers({ schema, resolvers, defaultFieldResolver, allowResolversNotInSchema, }) { | ||
schema = mapSchema(schema, { | ||
@@ -500,3 +608,2 @@ [MapperKind.SCALAR_TYPE]: type => { | ||
}); | ||
checkForResolveTypeResolver(schema, requireResolversForResolveType); | ||
if (defaultFieldResolver != null) { | ||
@@ -503,0 +610,0 @@ schema = mapSchema(schema, { |
{ | ||
"name": "@graphql-tools/schema", | ||
"version": "6.0.0-alpha-66debc3.0", | ||
"version": "6.0.0-alpha-843b53d.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
@@ -9,3 +10,3 @@ "graphql": "^14.0.0 || ^15.0.0" | ||
"dependencies": { | ||
"@graphql-tools/utils": "6.0.0-alpha-66debc3.0", | ||
"@graphql-tools/utils": "6.0.0-alpha-843b53d.0", | ||
"tslib": "~2.0.0" | ||
@@ -12,0 +13,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
188477
1469
+ Added@graphql-tools/utils@6.0.0-alpha-843b53d.0(transitive)
- Removed@graphql-tools/utils@6.0.0-alpha-66debc3.0(transitive)