@graphql-tools/schema
Advanced tools
Comparing version 5.0.1-alpha-f753a84.0 to 6.0.0-alpha.1
import { GraphQLSchema } from 'graphql'; | ||
import { IResolvers } from '@graphql-tools/utils'; | ||
export declare function extendResolversFromInterfaces(schema: GraphQLSchema, resolvers: IResolvers): Record<string, import("graphql").GraphQLScalarType | (() => any) | Record<string, string | number> | import("../../utils/src").IResolverObject<any, any, any> | import("../../utils/src").IResolverOptions<any, any, any>>; | ||
export declare function extendResolversFromInterfaces(schema: GraphQLSchema, resolvers: IResolvers): {}; |
@@ -303,10 +303,27 @@ 'use strict'; | ||
typeNames.forEach(typeName => { | ||
const typeResolvers = resolvers[typeName]; | ||
const type = schema.getType(typeName); | ||
if ('getInterfaces' in type) { | ||
const interfaceResolvers = type.getInterfaces().map(iFace => resolvers[iFace.name]); | ||
extendedResolvers[typeName] = Object.assign({}, ...interfaceResolvers, typeResolvers); | ||
const allInterfaceResolvers = type | ||
.getInterfaces() | ||
.map(iFace => resolvers[iFace.name]) | ||
.filter(interfaceResolvers => interfaceResolvers != null); | ||
extendedResolvers[typeName] = {}; | ||
allInterfaceResolvers.forEach(interfaceResolvers => { | ||
Object.keys(interfaceResolvers).forEach(fieldName => { | ||
if (fieldName === '__isTypeOf' || !fieldName.startsWith('__')) { | ||
extendedResolvers[typeName][fieldName] = interfaceResolvers[fieldName]; | ||
} | ||
}); | ||
}); | ||
const typeResolvers = resolvers[typeName]; | ||
extendedResolvers[typeName] = { | ||
...extendedResolvers[typeName], | ||
...typeResolvers, | ||
}; | ||
} | ||
else if (typeResolvers != null) { | ||
extendedResolvers[typeName] = typeResolvers; | ||
else { | ||
const typeResolvers = resolvers[typeName]; | ||
if (typeResolvers != null) { | ||
extendedResolvers[typeName] = typeResolvers; | ||
} | ||
} | ||
@@ -334,5 +351,12 @@ }); | ||
const resolverType = typeof resolverValue; | ||
if (resolverType !== 'object' && resolverType !== 'function') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A resolver's value must be of type object or function.`); | ||
if (typeName === '__schema') { | ||
if (resolverType !== 'function') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A schema resolver's value must be of type object or function.`); | ||
} | ||
} | ||
else { | ||
if (resolverType !== 'object') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`); | ||
} | ||
} | ||
const type = schema.getType(typeName); | ||
@@ -357,11 +381,11 @@ if (!type && typeName !== '__schema') { | ||
else if (graphql.isEnumType(type)) { | ||
// We've encountered an enum resolver that is being used to provide an | ||
// internal enum value. | ||
// Reference: https://www.apollographql.com/docs/graphql-tools/scalars.html#internal-values | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (!type.getValue(fieldName)) { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else if (!type.getValue(fieldName)) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but enum is not in schema`); | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but not present within ${typeName}`); | ||
} | ||
@@ -396,3 +420,3 @@ }); | ||
} | ||
throw new Error(`${typeName} was defined in resolvers, but it's not an object`); | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but ${typeName} is not an object or interface type`); | ||
}); | ||
@@ -490,5 +514,3 @@ } | ||
// We allow passing in an array of resolver maps, in which case we merge them | ||
const resolverMap = Array.isArray(resolvers) | ||
? resolvers.filter(resolverObj => typeof resolverObj === 'object').reduce(utils.mergeDeep, {}) | ||
: resolvers; | ||
const resolverMap = Array.isArray(resolvers) ? resolvers.reduce(utils.mergeDeep, {}) : resolvers; | ||
// Arguments are now validated and cleaned up | ||
@@ -495,0 +517,0 @@ const schema = buildSchemaFromTypeDefinitions(typeDefs, parseOptions); |
@@ -299,10 +299,27 @@ import { defaultFieldResolver, isScalarType, getNamedType, Kind, print, buildASTSchema, extendSchema, parse, isAbstractType, isSchema, isEnumType, GraphQLEnumType, isUnionType, isObjectType, isInterfaceType } from 'graphql'; | ||
typeNames.forEach(typeName => { | ||
const typeResolvers = resolvers[typeName]; | ||
const type = schema.getType(typeName); | ||
if ('getInterfaces' in type) { | ||
const interfaceResolvers = type.getInterfaces().map(iFace => resolvers[iFace.name]); | ||
extendedResolvers[typeName] = Object.assign({}, ...interfaceResolvers, typeResolvers); | ||
const allInterfaceResolvers = type | ||
.getInterfaces() | ||
.map(iFace => resolvers[iFace.name]) | ||
.filter(interfaceResolvers => interfaceResolvers != null); | ||
extendedResolvers[typeName] = {}; | ||
allInterfaceResolvers.forEach(interfaceResolvers => { | ||
Object.keys(interfaceResolvers).forEach(fieldName => { | ||
if (fieldName === '__isTypeOf' || !fieldName.startsWith('__')) { | ||
extendedResolvers[typeName][fieldName] = interfaceResolvers[fieldName]; | ||
} | ||
}); | ||
}); | ||
const typeResolvers = resolvers[typeName]; | ||
extendedResolvers[typeName] = { | ||
...extendedResolvers[typeName], | ||
...typeResolvers, | ||
}; | ||
} | ||
else if (typeResolvers != null) { | ||
extendedResolvers[typeName] = typeResolvers; | ||
else { | ||
const typeResolvers = resolvers[typeName]; | ||
if (typeResolvers != null) { | ||
extendedResolvers[typeName] = typeResolvers; | ||
} | ||
} | ||
@@ -330,5 +347,12 @@ }); | ||
const resolverType = typeof resolverValue; | ||
if (resolverType !== 'object' && resolverType !== 'function') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A resolver's value must be of type object or function.`); | ||
if (typeName === '__schema') { | ||
if (resolverType !== 'function') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A schema resolver's value must be of type object or function.`); | ||
} | ||
} | ||
else { | ||
if (resolverType !== 'object') { | ||
throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`); | ||
} | ||
} | ||
const type = schema.getType(typeName); | ||
@@ -353,11 +377,11 @@ if (!type && typeName !== '__schema') { | ||
else if (isEnumType(type)) { | ||
// We've encountered an enum resolver that is being used to provide an | ||
// internal enum value. | ||
// Reference: https://www.apollographql.com/docs/graphql-tools/scalars.html#internal-values | ||
Object.keys(resolverValue).forEach(fieldName => { | ||
if (!type.getValue(fieldName)) { | ||
if (fieldName.startsWith('__')) { | ||
type[fieldName.substring(2)] = resolverValue[fieldName]; | ||
} | ||
else if (!type.getValue(fieldName)) { | ||
if (allowResolversNotInSchema) { | ||
return; | ||
} | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but enum is not in schema`); | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but not present within ${typeName}`); | ||
} | ||
@@ -392,3 +416,3 @@ }); | ||
} | ||
throw new Error(`${typeName} was defined in resolvers, but it's not an object`); | ||
throw new Error(`${typeName}.${fieldName} was defined in resolvers, but ${typeName} is not an object or interface type`); | ||
}); | ||
@@ -486,5 +510,3 @@ } | ||
// We allow passing in an array of resolver maps, in which case we merge them | ||
const resolverMap = Array.isArray(resolvers) | ||
? resolvers.filter(resolverObj => typeof resolverObj === 'object').reduce(mergeDeep, {}) | ||
: resolvers; | ||
const resolverMap = Array.isArray(resolvers) ? resolvers.reduce(mergeDeep, {}) : resolvers; | ||
// Arguments are now validated and cleaned up | ||
@@ -491,0 +513,0 @@ const schema = buildSchemaFromTypeDefinitions(typeDefs, parseOptions); |
{ | ||
"name": "@graphql-tools/schema", | ||
"version": "5.0.1-alpha-f753a84.0", | ||
"version": "6.0.0-alpha.1", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,3 +9,3 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/utils": "5.0.1-alpha-f753a84.0", | ||
"@graphql-tools/utils": "6.0.0-alpha.1", | ||
"tslib": "1.11.1" | ||
@@ -12,0 +12,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
146045
1141
+ Added@graphql-tools/utils@6.0.0-alpha.1(transitive)
- Removed@graphql-tools/utils@5.0.1-alpha-f753a84.0(transitive)