@graphql-tools/wrap
Advanced tools
Comparing version 5.0.1-alpha-d3da75f.0 to 5.0.1-alpha-d4d928c.0
import { GraphQLSchema, GraphQLFieldResolver } from 'graphql'; | ||
import { Transform } from '@graphql-tools/utils'; | ||
import { SubschemaConfig } from '@graphql-tools/delegate'; | ||
export declare function generateProxyingResolvers({ subschemaConfig, transforms, }: { | ||
subschemaConfig: SubschemaConfig; | ||
transforms?: Array<Transform>; | ||
}): Record<string, Record<string, GraphQLFieldResolver<any, any>>>; | ||
export declare function defaultCreateProxyingResolver(schema: GraphQLSchema | SubschemaConfig, transforms: Array<Transform>): GraphQLFieldResolver<any, any>; | ||
import { SubschemaConfig, ICreateProxyingResolverOptions } from '@graphql-tools/delegate'; | ||
export declare function generateProxyingResolvers(subschemaOrSubschemaConfig: GraphQLSchema | SubschemaConfig, transforms: Array<Transform>): Record<string, Record<string, GraphQLFieldResolver<any, any>>>; | ||
export declare function defaultCreateProxyingResolver({ schema, transforms, transformedSchema, }: ICreateProxyingResolverOptions): GraphQLFieldResolver<any, any>; |
211
index.cjs.js
@@ -10,4 +10,22 @@ 'use strict'; | ||
function generateProxyingResolvers({ subschemaConfig, transforms, }) { | ||
const targetSchema = subschemaConfig.schema; | ||
function generateProxyingResolvers(subschemaOrSubschemaConfig, transforms) { | ||
var _a; | ||
let targetSchema; | ||
let schemaTransforms = []; | ||
let createProxyingResolver; | ||
if (delegate.isSubschemaConfig(subschemaOrSubschemaConfig)) { | ||
targetSchema = subschemaOrSubschemaConfig.schema; | ||
createProxyingResolver = (_a = subschemaOrSubschemaConfig.createProxyingResolver) !== null && _a !== void 0 ? _a : defaultCreateProxyingResolver; | ||
if (subschemaOrSubschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaOrSubschemaConfig.transforms); | ||
} | ||
} | ||
else { | ||
targetSchema = subschemaOrSubschemaConfig; | ||
createProxyingResolver = defaultCreateProxyingResolver; | ||
} | ||
if (transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(transforms); | ||
} | ||
const transformedSchema = utils.applySchemaTransforms(targetSchema, schemaTransforms); | ||
const operationTypes = { | ||
@@ -18,5 +36,2 @@ query: targetSchema.getQueryType(), | ||
}; | ||
const createProxyingResolver = subschemaConfig.createProxyingResolver != null | ||
? subschemaConfig.createProxyingResolver | ||
: defaultCreateProxyingResolver; | ||
const resolvers = {}; | ||
@@ -31,4 +46,10 @@ Object.keys(operationTypes).forEach((operation) => { | ||
Object.keys(fields).forEach(fieldName => { | ||
const proxyingResolver = createProxyingResolver(subschemaConfig, transforms, operation, fieldName); | ||
const finalResolver = createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver); | ||
const proxyingResolver = createProxyingResolver({ | ||
schema: subschemaOrSubschemaConfig, | ||
transforms, | ||
transformedSchema, | ||
operation, | ||
fieldName, | ||
}); | ||
const finalResolver = createPossiblyNestedProxyingResolver(subschemaOrSubschemaConfig, proxyingResolver); | ||
resolvers[typeName][fieldName] = { | ||
@@ -42,3 +63,3 @@ [resolveField]: finalResolver, | ||
} | ||
function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) { | ||
function createPossiblyNestedProxyingResolver(subschemaOrSubschemaConfig, proxyingResolver) { | ||
return (parent, args, context, info) => { | ||
@@ -54,3 +75,3 @@ if (parent != null) { | ||
// also nested as a field within a different type. | ||
if (subschemaConfig === subschema) { | ||
if (subschemaOrSubschemaConfig === subschema && parent[responseKey] !== undefined) { | ||
return delegate.handleResult(parent[responseKey], errors, subschema, context, info); | ||
@@ -63,3 +84,3 @@ } | ||
} | ||
function defaultCreateProxyingResolver(schema, transforms) { | ||
function defaultCreateProxyingResolver({ schema, transforms, transformedSchema, }) { | ||
return (_parent, _args, context, info) => delegate.delegateToSchema({ | ||
@@ -70,2 +91,3 @@ schema, | ||
transforms, | ||
transformedSchema, | ||
}); | ||
@@ -75,17 +97,18 @@ } | ||
function wrapSchema(subschemaOrSubschemaConfig, transforms) { | ||
const subschemaConfig = delegate.isSubschemaConfig(subschemaOrSubschemaConfig) | ||
? subschemaOrSubschemaConfig | ||
: { schema: subschemaOrSubschemaConfig }; | ||
const proxyingResolvers = generateProxyingResolvers({ | ||
subschemaConfig, | ||
transforms, | ||
}); | ||
const schema = createWrappingSchema(subschemaConfig.schema, proxyingResolvers); | ||
let targetSchema; | ||
let schemaTransforms = []; | ||
if (subschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaConfig.transforms); | ||
if (delegate.isSubschemaConfig(subschemaOrSubschemaConfig)) { | ||
targetSchema = subschemaOrSubschemaConfig.schema; | ||
if (subschemaOrSubschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaOrSubschemaConfig.transforms); | ||
} | ||
} | ||
else { | ||
targetSchema = subschemaOrSubschemaConfig; | ||
} | ||
if (transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(transforms); | ||
} | ||
const proxyingResolvers = generateProxyingResolvers(subschemaOrSubschemaConfig, transforms); | ||
const schema = createWrappingSchema(targetSchema, proxyingResolvers); | ||
return utils.applySchemaTransforms(schema, schemaTransforms); | ||
@@ -97,5 +120,6 @@ } | ||
const config = type.toConfig(); | ||
Object.keys(config.fields).forEach(fieldName => { | ||
config.fields[fieldName] = { | ||
...config.fields[fieldName], | ||
const fieldConfigMap = config.fields; | ||
Object.keys(fieldConfigMap).forEach(fieldName => { | ||
fieldConfigMap[fieldName] = { | ||
...fieldConfigMap[fieldName], | ||
...proxyingResolvers[type.name][fieldName], | ||
@@ -368,32 +392,28 @@ }; | ||
transformFields(type, fieldTransformer) { | ||
const typeConfig = type.toConfig(); | ||
const fields = type.getFields(); | ||
const newFields = {}; | ||
Object.keys(fields).forEach(fieldName => { | ||
const field = fields[fieldName]; | ||
const transformedField = fieldTransformer(type.name, fieldName, field); | ||
if (typeof transformedField === 'undefined') { | ||
newFields[fieldName] = typeConfig.fields[fieldName]; | ||
const config = type.toConfig(); | ||
const originalFieldConfigMap = config.fields; | ||
const newFieldConfigMap = {}; | ||
Object.keys(originalFieldConfigMap).forEach(fieldName => { | ||
const originalfieldConfig = originalFieldConfigMap[fieldName]; | ||
const transformedField = fieldTransformer(type.name, fieldName, originalfieldConfig); | ||
if (transformedField === undefined) { | ||
newFieldConfigMap[fieldName] = originalfieldConfig; | ||
} | ||
else if (transformedField !== null) { | ||
const newName = transformedField.name; | ||
if (newName) { | ||
newFields[newName] = | ||
transformedField.field != null | ||
? transformedField.field | ||
: typeConfig.fields[fieldName]; | ||
if (newName !== fieldName) { | ||
const typeName = type.name; | ||
if (!(typeName in this.mapping)) { | ||
this.mapping[typeName] = {}; | ||
} | ||
this.mapping[typeName][newName] = fieldName; | ||
else if (Array.isArray(transformedField)) { | ||
const newFieldName = transformedField[0]; | ||
const newFieldConfig = transformedField[1]; | ||
newFieldConfigMap[newFieldName] = newFieldConfig; | ||
if (newFieldName !== fieldName) { | ||
const typeName = type.name; | ||
if (!(typeName in this.mapping)) { | ||
this.mapping[typeName] = {}; | ||
} | ||
this.mapping[typeName][newFieldName] = fieldName; | ||
} | ||
else { | ||
newFields[fieldName] = transformedField; | ||
} | ||
} | ||
else if (transformedField != null) { | ||
newFieldConfigMap[fieldName] = transformedField; | ||
} | ||
}); | ||
if (newFields == null || Object.keys(newFields).length === 0) { | ||
if (!Object.keys(newFieldConfigMap).length) { | ||
return null; | ||
@@ -404,3 +424,3 @@ } | ||
...type.toConfig(), | ||
fields: newFields, | ||
fields: newFieldConfigMap, | ||
}); | ||
@@ -411,3 +431,3 @@ } | ||
...type.toConfig(), | ||
fields: newFields, | ||
fields: newFieldConfigMap, | ||
}); | ||
@@ -482,5 +502,5 @@ } | ||
transformSchema(originalSchema) { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (graphql.isObjectType(originalSchema.getType(typeName))) { | ||
return this.objectFieldTransformer(typeName, fieldName, field); | ||
return this.objectFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -499,5 +519,5 @@ return undefined; | ||
constructor(rootFieldTransformer, fieldNodeTransformer) { | ||
const rootToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (typeName === 'Query' || typeName === 'Mutation' || typeName === 'Subscription') { | ||
return rootFieldTransformer(typeName, fieldName, field); | ||
return rootFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -518,5 +538,3 @@ return undefined; | ||
constructor(renamer) { | ||
this.transformer = new TransformRootFields((operation, fieldName, field) => ({ | ||
name: renamer(operation, fieldName, field), | ||
})); | ||
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => [renamer(operation, fieldName, fieldConfig), fieldConfig]); | ||
} | ||
@@ -533,4 +551,4 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformRootFields((operation, fieldName, field) => { | ||
if (filter(operation, fieldName, field)) { | ||
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => { | ||
if (filter(operation, fieldName, fieldConfig)) { | ||
return undefined; | ||
@@ -548,5 +566,6 @@ } | ||
constructor(renamer) { | ||
this.transformer = new TransformObjectFields((typeName, fieldName, field) => ({ | ||
name: renamer(typeName, fieldName, field), | ||
})); | ||
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => [ | ||
renamer(typeName, fieldName, fieldConfig), | ||
fieldConfig, | ||
]); | ||
} | ||
@@ -563,3 +582,3 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformObjectFields((typeName, fieldName, field) => filter(typeName, fieldName, field) ? undefined : null); | ||
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null); | ||
} | ||
@@ -577,5 +596,5 @@ transformSchema(originalSchema) { | ||
transformSchema(originalSchema) { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (graphql.isInterfaceType(originalSchema.getType(typeName))) { | ||
return this.interfaceFieldTransformer(typeName, fieldName, field); | ||
return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -594,5 +613,6 @@ return undefined; | ||
constructor(renamer) { | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, field) => ({ | ||
name: renamer(typeName, fieldName, field), | ||
})); | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => [ | ||
renamer(typeName, fieldName, fieldConfig), | ||
fieldConfig, | ||
]); | ||
} | ||
@@ -609,3 +629,3 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, field) => filter(typeName, fieldName, field) ? undefined : null); | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null); | ||
} | ||
@@ -702,15 +722,3 @@ transformSchema(originalSchema) { | ||
constructor(fieldNodeTransformerMap) { | ||
this.transformer = new TransformObjectFields((_typeName, _fieldName, field) => ({ | ||
description: field.deprecationReason, | ||
type: field.type, | ||
args: field.args.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name]: curr, | ||
}), {}), | ||
resolve: field.resolve, | ||
subscribe: field.subscribe, | ||
deprecationReason: field.deprecationReason, | ||
extensions: field.extensions, | ||
astNode: field.astNode, | ||
}), (typeName, fieldName, fieldNode, fragments) => { | ||
this.transformer = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => fieldConfig, (typeName, fieldName, fieldNode, fragments) => { | ||
const typeTransformers = fieldNodeTransformerMap[typeName]; | ||
@@ -776,23 +784,25 @@ if (typeTransformers == null) { | ||
transformSchema(schema) { | ||
const typeMap = schema.getTypeMap(); | ||
const targetFields = utils.removeFields(typeMap, this.outerTypeName, !this.fieldNames ? () => true : fieldName => this.fieldNames.includes(fieldName)); | ||
let [newSchema, targetFieldConfigMap] = utils.removeObjectFields(schema, this.outerTypeName, !this.fieldNames ? () => true : fieldName => this.fieldNames.includes(fieldName)); | ||
let wrapIndex = this.numWraps - 1; | ||
const innerMostWrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
utils.appendFields(typeMap, innerMostWrappingTypeName, targetFields); | ||
let wrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
let wrappingFieldName = this.wrappingFieldNames[wrapIndex]; | ||
newSchema = utils.appendObjectFields(newSchema, wrappingTypeName, targetFieldConfigMap); | ||
for (wrapIndex--; wrapIndex > -1; wrapIndex--) { | ||
utils.appendFields(typeMap, this.wrappingTypeNames[wrapIndex], { | ||
[this.wrappingFieldNames[wrapIndex + 1]]: { | ||
type: typeMap[this.wrappingTypeNames[wrapIndex + 1]], | ||
const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
newSchema = utils.appendObjectFields(newSchema, nextWrappingTypeName, { | ||
[wrappingFieldName]: { | ||
type: newSchema.getType(wrappingTypeName), | ||
resolve: delegate.defaultMergedResolver, | ||
}, | ||
}); | ||
wrappingTypeName = nextWrappingTypeName; | ||
wrappingFieldName = this.wrappingFieldNames[wrapIndex]; | ||
} | ||
utils.appendFields(typeMap, this.outerTypeName, { | ||
[this.wrappingFieldNames[0]]: { | ||
type: typeMap[this.wrappingTypeNames[0]], | ||
newSchema = utils.appendObjectFields(newSchema, this.outerTypeName, { | ||
[wrappingFieldName]: { | ||
type: newSchema.getType(wrappingTypeName), | ||
resolve: delegate.createMergedResolver({ dehoist: true }), | ||
}, | ||
}); | ||
utils.healSchema(schema); | ||
return this.transformer.transformSchema(schema); | ||
return this.transformer.transformSchema(newSchema); | ||
} | ||
@@ -830,7 +840,7 @@ transformRequest(originalRequest) { | ||
transformSchema(schema) { | ||
const typeMap = schema.getTypeMap(); | ||
const innerType = this.pathToField.reduce((acc, pathSegment) => graphql.getNullableType(acc.getFields()[pathSegment].type), typeMap[this.typeName]); | ||
const targetField = utils.removeFields(typeMap, innerType.name, fieldName => fieldName === this.oldFieldName)[this.oldFieldName]; | ||
const innerType = this.pathToField.reduce((acc, pathSegment) => graphql.getNullableType(acc.getFields()[pathSegment].type), schema.getType(this.typeName)); | ||
let [newSchema, targetFieldConfigMap] = utils.removeObjectFields(schema, innerType.name, fieldName => fieldName === this.oldFieldName); | ||
const targetField = targetFieldConfigMap[this.oldFieldName]; | ||
const targetType = targetField.type; | ||
utils.appendFields(typeMap, this.typeName, { | ||
newSchema = utils.appendObjectFields(newSchema, this.typeName, { | ||
[this.newFieldName]: { | ||
@@ -841,4 +851,3 @@ type: targetType, | ||
}); | ||
utils.healSchema(schema); | ||
return this.transformer.transformSchema(schema); | ||
return this.transformer.transformSchema(newSchema); | ||
} | ||
@@ -962,3 +971,3 @@ transformRequest(originalRequest) { | ||
schema: targetSchema, | ||
createProxyingResolver: (_schema, _transforms, _operation) => createResolver(executor, subscriber), | ||
createProxyingResolver: () => createResolver(executor, subscriber), | ||
}); | ||
@@ -965,0 +974,0 @@ } |
215
index.esm.js
import { GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, isSpecifiedScalarType, isScalarType, isObjectType, isInterfaceType, isUnionType, isInputObjectType, GraphQLInputObjectType, isEnumType, GraphQLEnumType, GraphQLScalarType, visit, Kind, TypeInfo, visitWithTypeInfo, extendSchema, parse, getNullableType, BREAK, buildSchema, getIntrospectionQuery, buildClientSchema } from 'graphql'; | ||
import { getResponseKeyFromInfo, getErrors, mapSchema, MapperKind, applySchemaTransforms, relocatedError, hoistFieldNodes, removeFields, appendFields, healSchema, wrapFieldNode, renameFieldNode, CombinedError } from '@graphql-tools/utils'; | ||
import { delegateToSchema, getSubschema, handleResult, isSubschemaConfig, defaultMergedResolver, createMergedResolver } from '@graphql-tools/delegate'; | ||
import { applySchemaTransforms, getResponseKeyFromInfo, getErrors, mapSchema, MapperKind, relocatedError, hoistFieldNodes, removeObjectFields, appendObjectFields, wrapFieldNode, renameFieldNode, CombinedError } from '@graphql-tools/utils'; | ||
import { delegateToSchema, isSubschemaConfig, getSubschema, handleResult, defaultMergedResolver, createMergedResolver } from '@graphql-tools/delegate'; | ||
import { addResolversToSchema } from '@graphql-tools/schema'; | ||
function generateProxyingResolvers({ subschemaConfig, transforms, }) { | ||
const targetSchema = subschemaConfig.schema; | ||
function generateProxyingResolvers(subschemaOrSubschemaConfig, transforms) { | ||
var _a; | ||
let targetSchema; | ||
let schemaTransforms = []; | ||
let createProxyingResolver; | ||
if (isSubschemaConfig(subschemaOrSubschemaConfig)) { | ||
targetSchema = subschemaOrSubschemaConfig.schema; | ||
createProxyingResolver = (_a = subschemaOrSubschemaConfig.createProxyingResolver) !== null && _a !== void 0 ? _a : defaultCreateProxyingResolver; | ||
if (subschemaOrSubschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaOrSubschemaConfig.transforms); | ||
} | ||
} | ||
else { | ||
targetSchema = subschemaOrSubschemaConfig; | ||
createProxyingResolver = defaultCreateProxyingResolver; | ||
} | ||
if (transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(transforms); | ||
} | ||
const transformedSchema = applySchemaTransforms(targetSchema, schemaTransforms); | ||
const operationTypes = { | ||
@@ -13,5 +31,2 @@ query: targetSchema.getQueryType(), | ||
}; | ||
const createProxyingResolver = subschemaConfig.createProxyingResolver != null | ||
? subschemaConfig.createProxyingResolver | ||
: defaultCreateProxyingResolver; | ||
const resolvers = {}; | ||
@@ -26,4 +41,10 @@ Object.keys(operationTypes).forEach((operation) => { | ||
Object.keys(fields).forEach(fieldName => { | ||
const proxyingResolver = createProxyingResolver(subschemaConfig, transforms, operation, fieldName); | ||
const finalResolver = createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver); | ||
const proxyingResolver = createProxyingResolver({ | ||
schema: subschemaOrSubschemaConfig, | ||
transforms, | ||
transformedSchema, | ||
operation, | ||
fieldName, | ||
}); | ||
const finalResolver = createPossiblyNestedProxyingResolver(subschemaOrSubschemaConfig, proxyingResolver); | ||
resolvers[typeName][fieldName] = { | ||
@@ -37,3 +58,3 @@ [resolveField]: finalResolver, | ||
} | ||
function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) { | ||
function createPossiblyNestedProxyingResolver(subschemaOrSubschemaConfig, proxyingResolver) { | ||
return (parent, args, context, info) => { | ||
@@ -49,3 +70,3 @@ if (parent != null) { | ||
// also nested as a field within a different type. | ||
if (subschemaConfig === subschema) { | ||
if (subschemaOrSubschemaConfig === subschema && parent[responseKey] !== undefined) { | ||
return handleResult(parent[responseKey], errors, subschema, context, info); | ||
@@ -58,3 +79,3 @@ } | ||
} | ||
function defaultCreateProxyingResolver(schema, transforms) { | ||
function defaultCreateProxyingResolver({ schema, transforms, transformedSchema, }) { | ||
return (_parent, _args, context, info) => delegateToSchema({ | ||
@@ -65,2 +86,3 @@ schema, | ||
transforms, | ||
transformedSchema, | ||
}); | ||
@@ -70,17 +92,18 @@ } | ||
function wrapSchema(subschemaOrSubschemaConfig, transforms) { | ||
const subschemaConfig = isSubschemaConfig(subschemaOrSubschemaConfig) | ||
? subschemaOrSubschemaConfig | ||
: { schema: subschemaOrSubschemaConfig }; | ||
const proxyingResolvers = generateProxyingResolvers({ | ||
subschemaConfig, | ||
transforms, | ||
}); | ||
const schema = createWrappingSchema(subschemaConfig.schema, proxyingResolvers); | ||
let targetSchema; | ||
let schemaTransforms = []; | ||
if (subschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaConfig.transforms); | ||
if (isSubschemaConfig(subschemaOrSubschemaConfig)) { | ||
targetSchema = subschemaOrSubschemaConfig.schema; | ||
if (subschemaOrSubschemaConfig.transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(subschemaOrSubschemaConfig.transforms); | ||
} | ||
} | ||
else { | ||
targetSchema = subschemaOrSubschemaConfig; | ||
} | ||
if (transforms != null) { | ||
schemaTransforms = schemaTransforms.concat(transforms); | ||
} | ||
const proxyingResolvers = generateProxyingResolvers(subschemaOrSubschemaConfig, transforms); | ||
const schema = createWrappingSchema(targetSchema, proxyingResolvers); | ||
return applySchemaTransforms(schema, schemaTransforms); | ||
@@ -92,5 +115,6 @@ } | ||
const config = type.toConfig(); | ||
Object.keys(config.fields).forEach(fieldName => { | ||
config.fields[fieldName] = { | ||
...config.fields[fieldName], | ||
const fieldConfigMap = config.fields; | ||
Object.keys(fieldConfigMap).forEach(fieldName => { | ||
fieldConfigMap[fieldName] = { | ||
...fieldConfigMap[fieldName], | ||
...proxyingResolvers[type.name][fieldName], | ||
@@ -363,32 +387,28 @@ }; | ||
transformFields(type, fieldTransformer) { | ||
const typeConfig = type.toConfig(); | ||
const fields = type.getFields(); | ||
const newFields = {}; | ||
Object.keys(fields).forEach(fieldName => { | ||
const field = fields[fieldName]; | ||
const transformedField = fieldTransformer(type.name, fieldName, field); | ||
if (typeof transformedField === 'undefined') { | ||
newFields[fieldName] = typeConfig.fields[fieldName]; | ||
const config = type.toConfig(); | ||
const originalFieldConfigMap = config.fields; | ||
const newFieldConfigMap = {}; | ||
Object.keys(originalFieldConfigMap).forEach(fieldName => { | ||
const originalfieldConfig = originalFieldConfigMap[fieldName]; | ||
const transformedField = fieldTransformer(type.name, fieldName, originalfieldConfig); | ||
if (transformedField === undefined) { | ||
newFieldConfigMap[fieldName] = originalfieldConfig; | ||
} | ||
else if (transformedField !== null) { | ||
const newName = transformedField.name; | ||
if (newName) { | ||
newFields[newName] = | ||
transformedField.field != null | ||
? transformedField.field | ||
: typeConfig.fields[fieldName]; | ||
if (newName !== fieldName) { | ||
const typeName = type.name; | ||
if (!(typeName in this.mapping)) { | ||
this.mapping[typeName] = {}; | ||
} | ||
this.mapping[typeName][newName] = fieldName; | ||
else if (Array.isArray(transformedField)) { | ||
const newFieldName = transformedField[0]; | ||
const newFieldConfig = transformedField[1]; | ||
newFieldConfigMap[newFieldName] = newFieldConfig; | ||
if (newFieldName !== fieldName) { | ||
const typeName = type.name; | ||
if (!(typeName in this.mapping)) { | ||
this.mapping[typeName] = {}; | ||
} | ||
this.mapping[typeName][newFieldName] = fieldName; | ||
} | ||
else { | ||
newFields[fieldName] = transformedField; | ||
} | ||
} | ||
else if (transformedField != null) { | ||
newFieldConfigMap[fieldName] = transformedField; | ||
} | ||
}); | ||
if (newFields == null || Object.keys(newFields).length === 0) { | ||
if (!Object.keys(newFieldConfigMap).length) { | ||
return null; | ||
@@ -399,3 +419,3 @@ } | ||
...type.toConfig(), | ||
fields: newFields, | ||
fields: newFieldConfigMap, | ||
}); | ||
@@ -406,3 +426,3 @@ } | ||
...type.toConfig(), | ||
fields: newFields, | ||
fields: newFieldConfigMap, | ||
}); | ||
@@ -477,5 +497,5 @@ } | ||
transformSchema(originalSchema) { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (isObjectType(originalSchema.getType(typeName))) { | ||
return this.objectFieldTransformer(typeName, fieldName, field); | ||
return this.objectFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -494,5 +514,5 @@ return undefined; | ||
constructor(rootFieldTransformer, fieldNodeTransformer) { | ||
const rootToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const rootToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (typeName === 'Query' || typeName === 'Mutation' || typeName === 'Subscription') { | ||
return rootFieldTransformer(typeName, fieldName, field); | ||
return rootFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -513,5 +533,3 @@ return undefined; | ||
constructor(renamer) { | ||
this.transformer = new TransformRootFields((operation, fieldName, field) => ({ | ||
name: renamer(operation, fieldName, field), | ||
})); | ||
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => [renamer(operation, fieldName, fieldConfig), fieldConfig]); | ||
} | ||
@@ -528,4 +546,4 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformRootFields((operation, fieldName, field) => { | ||
if (filter(operation, fieldName, field)) { | ||
this.transformer = new TransformRootFields((operation, fieldName, fieldConfig) => { | ||
if (filter(operation, fieldName, fieldConfig)) { | ||
return undefined; | ||
@@ -543,5 +561,6 @@ } | ||
constructor(renamer) { | ||
this.transformer = new TransformObjectFields((typeName, fieldName, field) => ({ | ||
name: renamer(typeName, fieldName, field), | ||
})); | ||
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => [ | ||
renamer(typeName, fieldName, fieldConfig), | ||
fieldConfig, | ||
]); | ||
} | ||
@@ -558,3 +577,3 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformObjectFields((typeName, fieldName, field) => filter(typeName, fieldName, field) ? undefined : null); | ||
this.transformer = new TransformObjectFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null); | ||
} | ||
@@ -572,5 +591,5 @@ transformSchema(originalSchema) { | ||
transformSchema(originalSchema) { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, field) => { | ||
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => { | ||
if (isInterfaceType(originalSchema.getType(typeName))) { | ||
return this.interfaceFieldTransformer(typeName, fieldName, field); | ||
return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig); | ||
} | ||
@@ -589,5 +608,6 @@ return undefined; | ||
constructor(renamer) { | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, field) => ({ | ||
name: renamer(typeName, fieldName, field), | ||
})); | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => [ | ||
renamer(typeName, fieldName, fieldConfig), | ||
fieldConfig, | ||
]); | ||
} | ||
@@ -604,3 +624,3 @@ transformSchema(originalSchema) { | ||
constructor(filter) { | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, field) => filter(typeName, fieldName, field) ? undefined : null); | ||
this.transformer = new TransformInterfaceFields((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null); | ||
} | ||
@@ -697,15 +717,3 @@ transformSchema(originalSchema) { | ||
constructor(fieldNodeTransformerMap) { | ||
this.transformer = new TransformObjectFields((_typeName, _fieldName, field) => ({ | ||
description: field.deprecationReason, | ||
type: field.type, | ||
args: field.args.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name]: curr, | ||
}), {}), | ||
resolve: field.resolve, | ||
subscribe: field.subscribe, | ||
deprecationReason: field.deprecationReason, | ||
extensions: field.extensions, | ||
astNode: field.astNode, | ||
}), (typeName, fieldName, fieldNode, fragments) => { | ||
this.transformer = new TransformObjectFields((_typeName, _fieldName, fieldConfig) => fieldConfig, (typeName, fieldName, fieldNode, fragments) => { | ||
const typeTransformers = fieldNodeTransformerMap[typeName]; | ||
@@ -771,23 +779,25 @@ if (typeTransformers == null) { | ||
transformSchema(schema) { | ||
const typeMap = schema.getTypeMap(); | ||
const targetFields = removeFields(typeMap, this.outerTypeName, !this.fieldNames ? () => true : fieldName => this.fieldNames.includes(fieldName)); | ||
let [newSchema, targetFieldConfigMap] = removeObjectFields(schema, this.outerTypeName, !this.fieldNames ? () => true : fieldName => this.fieldNames.includes(fieldName)); | ||
let wrapIndex = this.numWraps - 1; | ||
const innerMostWrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
appendFields(typeMap, innerMostWrappingTypeName, targetFields); | ||
let wrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
let wrappingFieldName = this.wrappingFieldNames[wrapIndex]; | ||
newSchema = appendObjectFields(newSchema, wrappingTypeName, targetFieldConfigMap); | ||
for (wrapIndex--; wrapIndex > -1; wrapIndex--) { | ||
appendFields(typeMap, this.wrappingTypeNames[wrapIndex], { | ||
[this.wrappingFieldNames[wrapIndex + 1]]: { | ||
type: typeMap[this.wrappingTypeNames[wrapIndex + 1]], | ||
const nextWrappingTypeName = this.wrappingTypeNames[wrapIndex]; | ||
newSchema = appendObjectFields(newSchema, nextWrappingTypeName, { | ||
[wrappingFieldName]: { | ||
type: newSchema.getType(wrappingTypeName), | ||
resolve: defaultMergedResolver, | ||
}, | ||
}); | ||
wrappingTypeName = nextWrappingTypeName; | ||
wrappingFieldName = this.wrappingFieldNames[wrapIndex]; | ||
} | ||
appendFields(typeMap, this.outerTypeName, { | ||
[this.wrappingFieldNames[0]]: { | ||
type: typeMap[this.wrappingTypeNames[0]], | ||
newSchema = appendObjectFields(newSchema, this.outerTypeName, { | ||
[wrappingFieldName]: { | ||
type: newSchema.getType(wrappingTypeName), | ||
resolve: createMergedResolver({ dehoist: true }), | ||
}, | ||
}); | ||
healSchema(schema); | ||
return this.transformer.transformSchema(schema); | ||
return this.transformer.transformSchema(newSchema); | ||
} | ||
@@ -825,7 +835,7 @@ transformRequest(originalRequest) { | ||
transformSchema(schema) { | ||
const typeMap = schema.getTypeMap(); | ||
const innerType = this.pathToField.reduce((acc, pathSegment) => getNullableType(acc.getFields()[pathSegment].type), typeMap[this.typeName]); | ||
const targetField = removeFields(typeMap, innerType.name, fieldName => fieldName === this.oldFieldName)[this.oldFieldName]; | ||
const innerType = this.pathToField.reduce((acc, pathSegment) => getNullableType(acc.getFields()[pathSegment].type), schema.getType(this.typeName)); | ||
let [newSchema, targetFieldConfigMap] = removeObjectFields(schema, innerType.name, fieldName => fieldName === this.oldFieldName); | ||
const targetField = targetFieldConfigMap[this.oldFieldName]; | ||
const targetType = targetField.type; | ||
appendFields(typeMap, this.typeName, { | ||
newSchema = appendObjectFields(newSchema, this.typeName, { | ||
[this.newFieldName]: { | ||
@@ -836,4 +846,3 @@ type: targetType, | ||
}); | ||
healSchema(schema); | ||
return this.transformer.transformSchema(schema); | ||
return this.transformer.transformSchema(newSchema); | ||
} | ||
@@ -957,3 +966,3 @@ transformRequest(originalRequest) { | ||
schema: targetSchema, | ||
createProxyingResolver: (_schema, _transforms, _operation) => createResolver(executor, subscriber), | ||
createProxyingResolver: () => createResolver(executor, subscriber), | ||
}); | ||
@@ -960,0 +969,0 @@ } |
{ | ||
"name": "@graphql-tools/wrap", | ||
"version": "5.0.1-alpha-d3da75f.0", | ||
"version": "5.0.1-alpha-d4d928c.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,5 +9,5 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/delegate": "5.0.1-alpha-d3da75f.0", | ||
"@graphql-tools/schema": "5.0.1-alpha-d3da75f.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-d3da75f.0", | ||
"@graphql-tools/delegate": "5.0.1-alpha-d4d928c.0", | ||
"@graphql-tools/schema": "5.0.1-alpha-d4d928c.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-d4d928c.0", | ||
"tslib": "1.11.1" | ||
@@ -14,0 +14,0 @@ }, |
@@ -1,8 +0,8 @@ | ||
import { GraphQLField, GraphQLSchema } from 'graphql'; | ||
import { GraphQLSchema, GraphQLFieldConfig } from 'graphql'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
export default class RenameInterfaceFields implements Transform { | ||
private readonly transformer; | ||
constructor(renamer: (typeName: string, fieldName: string, field: GraphQLField<any, any>) => string); | ||
constructor(renamer: (typeName: string, fieldName: string, fieldConfig: GraphQLFieldConfig<any, any>) => string); | ||
transformSchema(originalSchema: GraphQLSchema): GraphQLSchema; | ||
transformRequest(originalRequest: Request): Request; | ||
} |
@@ -1,8 +0,8 @@ | ||
import { GraphQLField, GraphQLSchema } from 'graphql'; | ||
import { GraphQLSchema, GraphQLFieldConfig } from 'graphql'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
export default class RenameObjectFields implements Transform { | ||
private readonly transformer; | ||
constructor(renamer: (typeName: string, fieldName: string, field: GraphQLField<any, any>) => string); | ||
constructor(renamer: (typeName: string, fieldName: string, fieldConfig: GraphQLFieldConfig<any, any>) => string); | ||
transformSchema(originalSchema: GraphQLSchema): GraphQLSchema; | ||
transformRequest(originalRequest: Request): Request; | ||
} |
@@ -1,8 +0,8 @@ | ||
import { GraphQLField, GraphQLSchema } from 'graphql'; | ||
import { GraphQLSchema, GraphQLFieldConfig } from 'graphql'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
export default class RenameRootFields implements Transform { | ||
private readonly transformer; | ||
constructor(renamer: (operation: 'Query' | 'Mutation' | 'Subscription', name: string, field: GraphQLField<any, any>) => string); | ||
constructor(renamer: (operation: 'Query' | 'Mutation' | 'Subscription', name: string, fieldConfig: GraphQLFieldConfig<any, any>) => string); | ||
transformSchema(originalSchema: GraphQLSchema): GraphQLSchema; | ||
transformRequest(originalRequest: Request): Request; | ||
} |
import { GraphQLSchema } from 'graphql'; | ||
import { Transform, Request, FieldTransformer, FieldNodeTransformer } from '@graphql-tools/utils'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
import { FieldTransformer, FieldNodeTransformer } from '../types'; | ||
export default class TransformCompositeFields implements Transform { | ||
@@ -4,0 +5,0 @@ private readonly fieldTransformer; |
import { GraphQLSchema } from 'graphql'; | ||
import { Transform, Request, FieldTransformer, FieldNodeTransformer } from '@graphql-tools/utils'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
import { FieldTransformer, FieldNodeTransformer } from '../types'; | ||
export default class TransformInterfaceFields implements Transform { | ||
@@ -4,0 +5,0 @@ private readonly interfaceFieldTransformer; |
import { GraphQLSchema } from 'graphql'; | ||
import { Transform, Request, FieldTransformer, FieldNodeTransformer } from '@graphql-tools/utils'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
import { FieldTransformer, FieldNodeTransformer } from '../types'; | ||
export default class TransformObjectFields implements Transform { | ||
@@ -4,0 +5,0 @@ private readonly objectFieldTransformer; |
import { GraphQLSchema } from 'graphql'; | ||
import { Transform, Request, FieldNodeTransformer, RootFieldTransformer } from '@graphql-tools/utils'; | ||
import { Transform, Request } from '@graphql-tools/utils'; | ||
import { RootFieldTransformer, FieldNodeTransformer } from '../types'; | ||
export default class TransformRootFields implements Transform { | ||
@@ -4,0 +5,0 @@ private readonly transformer; |
@@ -1,2 +0,2 @@ | ||
import { GraphQLSchema, GraphQLFieldResolver, BuildSchemaOptions } from 'graphql'; | ||
import { GraphQLSchema, GraphQLFieldResolver, BuildSchemaOptions, GraphQLFieldConfig, FieldNode, FragmentDefinitionNode, SelectionNode } from 'graphql'; | ||
import { Executor, Subscriber } from '@graphql-tools/delegate'; | ||
@@ -10,1 +10,4 @@ export interface IMakeRemoteExecutableSchemaOptions { | ||
} | ||
export declare type FieldTransformer = (typeName: string, fieldName: string, fieldConfig: GraphQLFieldConfig<any, any>) => GraphQLFieldConfig<any, any> | [string, GraphQLFieldConfig<any, any>] | null | undefined; | ||
export declare type RootFieldTransformer = (operation: 'Query' | 'Mutation' | 'Subscription', fieldName: string, fieldConfig: GraphQLFieldConfig<any, any>) => GraphQLFieldConfig<any, any> | [string, GraphQLFieldConfig<any, any>] | null | undefined; | ||
export declare type FieldNodeTransformer = (typeName: string, fieldName: string, fieldNode: FieldNode, fragments: Record<string, FragmentDefinitionNode>) => SelectionNode | Array<SelectionNode>; |
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
253799
2248
+ Added@graphql-tools/delegate@5.0.1-alpha-d4d928c.0(transitive)
+ Added@graphql-tools/schema@5.0.1-alpha-d4d928c.0(transitive)
+ Added@graphql-tools/utils@5.0.1-alpha-d4d928c.0(transitive)
- Removed@graphql-tools/delegate@5.0.1-alpha-d3da75f.0(transitive)
- Removed@graphql-tools/schema@5.0.1-alpha-d3da75f.0(transitive)
- Removed@graphql-tools/utils@5.0.1-alpha-d3da75f.0(transitive)