@graphql-tools/delegate
Advanced tools
Comparing version 6.0.0-alpha.1 to 6.0.0-alpha-0bfc960.0
import { GraphQLSchema, GraphQLObjectType, OperationTypeNode } from 'graphql'; | ||
import { Request, ICreateRequest } from '@graphql-tools/utils'; | ||
import { ICreateRequestFromInfo } from './types'; | ||
import { Request } from '@graphql-tools/utils'; | ||
import { ICreateRequestFromInfo, ICreateRequest } from './types'; | ||
export declare function getDelegatingOperation(parentType: GraphQLObjectType, schema: GraphQLSchema): OperationTypeNode; | ||
export declare function createRequestFromInfo({ info, operation, fieldName, selectionSet, fieldNodes, }: ICreateRequestFromInfo): Request; | ||
export declare function createRequest({ sourceSchema, sourceParentType, sourceFieldName, fragments, variableDefinitions, variableValues, targetOperation, targetFieldName, selectionSet, fieldNodes, }: ICreateRequest): Request; |
import { GraphQLSchema } from 'graphql'; | ||
import { IDelegateToSchemaOptions, IDelegateRequestOptions } from './types'; | ||
export declare function delegateToSchema(options: IDelegateToSchemaOptions | GraphQLSchema): any; | ||
export declare function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation, fieldName, args, returnType, context, transforms, skipValidation, skipTypeMerging, }: IDelegateRequestOptions): any; | ||
export declare function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation, fieldName, args, returnType, context, transforms, transformedSchema, skipValidation, skipTypeMerging, }: IDelegateRequestOptions): any; |
@@ -171,2 +171,62 @@ 'use strict'; | ||
// For motivation, see https://github.com/ardatan/graphql-tools/issues/751 | ||
class WrapConcreteTypes { | ||
constructor(returnType, targetSchema) { | ||
this.returnType = returnType; | ||
this.targetSchema = targetSchema; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = wrapConcreteTypes(this.returnType, this.targetSchema, originalRequest.document); | ||
return { | ||
...originalRequest, | ||
document, | ||
}; | ||
} | ||
} | ||
function wrapConcreteTypes(returnType, targetSchema, document) { | ||
const namedType = graphql.getNamedType(returnType); | ||
if (!graphql.isObjectType(namedType)) { | ||
return document; | ||
} | ||
const queryRootType = targetSchema.getQueryType(); | ||
const mutationRootType = targetSchema.getMutationType(); | ||
const subscriptionRootType = targetSchema.getSubscriptionType(); | ||
const typeInfo = new graphql.TypeInfo(targetSchema); | ||
const newDocument = graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, { | ||
[graphql.Kind.FIELD](node) { | ||
const maybeType = typeInfo.getParentType(); | ||
if (maybeType == null) { | ||
return false; | ||
} | ||
const parentType = graphql.getNamedType(maybeType); | ||
if (parentType !== queryRootType && parentType !== mutationRootType && parentType !== subscriptionRootType) { | ||
return false; | ||
} | ||
if (!graphql.isAbstractType(graphql.getNamedType(typeInfo.getType()))) { | ||
return false; | ||
} | ||
return { | ||
...node, | ||
selectionSet: { | ||
kind: graphql.Kind.SELECTION_SET, | ||
selections: [ | ||
{ | ||
kind: graphql.Kind.INLINE_FRAGMENT, | ||
typeCondition: { | ||
kind: graphql.Kind.NAMED_TYPE, | ||
name: { | ||
kind: graphql.Kind.NAME, | ||
value: namedType.name, | ||
}, | ||
}, | ||
selectionSet: node.selectionSet, | ||
}, | ||
], | ||
}, | ||
}; | ||
}, | ||
})); | ||
return newDocument; | ||
} | ||
class FilterToSchema { | ||
@@ -228,3 +288,6 @@ constructor(targetSchema) { | ||
const newVariables = usedVariables.reduce((acc, variableName) => { | ||
acc[variableName] = variables[variableName]; | ||
const variableValue = variables[variableName]; | ||
if (variableValue !== undefined) { | ||
acc[variableName] = variableValue; | ||
} | ||
return acc; | ||
@@ -956,3 +1019,6 @@ }, {}); | ||
const varType = graphql.typeFromAST(sourceSchema, def.type); | ||
newVariables[varName] = utils.serializeInputValue(varType, variableValues[varName]); | ||
const serializedValue = utils.serializeInputValue(varType, variableValues[varName]); | ||
if (serializedValue !== undefined) { | ||
newVariables[varName] = serializedValue; | ||
} | ||
}); | ||
@@ -1028,3 +1094,4 @@ } | ||
} | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, skipTypeMerging) { | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, transformedSchema, skipTypeMerging) { | ||
var _a; | ||
let delegationTransforms = [ | ||
@@ -1036,4 +1103,7 @@ new CheckResultAndHandleErrors(info, fieldName, subschemaOrSubschemaConfig, context, returnType, skipTypeMerging), | ||
} | ||
const transformedTargetSchema = info.mergeInfo == null | ||
? transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : targetSchema : (_a = transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : info.mergeInfo.transformedSchemas.get(subschemaOrSubschemaConfig)) !== null && _a !== void 0 ? _a : targetSchema; | ||
delegationTransforms.push(new WrapConcreteTypes(returnType, transformedTargetSchema)); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
delegationTransforms = delegationTransforms.concat(transforms); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, targetSchema)); | ||
if (info.mergeInfo != null) { | ||
@@ -1048,3 +1118,3 @@ delegationTransforms.push(new AddReplacementFragments(targetSchema, info.mergeInfo.replacementFragments)); | ||
} | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, args, returnType = info.returnType, context, transforms = [], skipValidation, skipTypeMerging, }) { | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, args, returnType = info.returnType, context, transforms = [], transformedSchema, skipValidation, skipTypeMerging, }) { | ||
let targetSchema; | ||
@@ -1067,3 +1137,3 @@ let targetRootValue; | ||
} | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), skipTypeMerging); | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const processedRequest = utils.applyRequestTransforms(request, delegationTransforms); | ||
@@ -1070,0 +1140,0 @@ if (!skipValidation) { |
@@ -167,2 +167,62 @@ import { visit, visitWithTypeInfo, Kind, getNamedType, isAbstractType, TypeInfo, isObjectType, isInterfaceType, TypeNameMetaFieldDef, responsePathAsArray, getNullableType, isLeafType, isCompositeType, isListType, typeFromAST, isSchema, validate, execute, subscribe, defaultFieldResolver, parse } from 'graphql'; | ||
// For motivation, see https://github.com/ardatan/graphql-tools/issues/751 | ||
class WrapConcreteTypes { | ||
constructor(returnType, targetSchema) { | ||
this.returnType = returnType; | ||
this.targetSchema = targetSchema; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = wrapConcreteTypes(this.returnType, this.targetSchema, originalRequest.document); | ||
return { | ||
...originalRequest, | ||
document, | ||
}; | ||
} | ||
} | ||
function wrapConcreteTypes(returnType, targetSchema, document) { | ||
const namedType = getNamedType(returnType); | ||
if (!isObjectType(namedType)) { | ||
return document; | ||
} | ||
const queryRootType = targetSchema.getQueryType(); | ||
const mutationRootType = targetSchema.getMutationType(); | ||
const subscriptionRootType = targetSchema.getSubscriptionType(); | ||
const typeInfo = new TypeInfo(targetSchema); | ||
const newDocument = visit(document, visitWithTypeInfo(typeInfo, { | ||
[Kind.FIELD](node) { | ||
const maybeType = typeInfo.getParentType(); | ||
if (maybeType == null) { | ||
return false; | ||
} | ||
const parentType = getNamedType(maybeType); | ||
if (parentType !== queryRootType && parentType !== mutationRootType && parentType !== subscriptionRootType) { | ||
return false; | ||
} | ||
if (!isAbstractType(getNamedType(typeInfo.getType()))) { | ||
return false; | ||
} | ||
return { | ||
...node, | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
selections: [ | ||
{ | ||
kind: Kind.INLINE_FRAGMENT, | ||
typeCondition: { | ||
kind: Kind.NAMED_TYPE, | ||
name: { | ||
kind: Kind.NAME, | ||
value: namedType.name, | ||
}, | ||
}, | ||
selectionSet: node.selectionSet, | ||
}, | ||
], | ||
}, | ||
}; | ||
}, | ||
})); | ||
return newDocument; | ||
} | ||
class FilterToSchema { | ||
@@ -224,3 +284,6 @@ constructor(targetSchema) { | ||
const newVariables = usedVariables.reduce((acc, variableName) => { | ||
acc[variableName] = variables[variableName]; | ||
const variableValue = variables[variableName]; | ||
if (variableValue !== undefined) { | ||
acc[variableName] = variableValue; | ||
} | ||
return acc; | ||
@@ -952,3 +1015,6 @@ }, {}); | ||
const varType = typeFromAST(sourceSchema, def.type); | ||
newVariables[varName] = serializeInputValue(varType, variableValues[varName]); | ||
const serializedValue = serializeInputValue(varType, variableValues[varName]); | ||
if (serializedValue !== undefined) { | ||
newVariables[varName] = serializedValue; | ||
} | ||
}); | ||
@@ -1024,3 +1090,4 @@ } | ||
} | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, skipTypeMerging) { | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, transformedSchema, skipTypeMerging) { | ||
var _a; | ||
let delegationTransforms = [ | ||
@@ -1032,4 +1099,7 @@ new CheckResultAndHandleErrors(info, fieldName, subschemaOrSubschemaConfig, context, returnType, skipTypeMerging), | ||
} | ||
const transformedTargetSchema = info.mergeInfo == null | ||
? transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : targetSchema : (_a = transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : info.mergeInfo.transformedSchemas.get(subschemaOrSubschemaConfig)) !== null && _a !== void 0 ? _a : targetSchema; | ||
delegationTransforms.push(new WrapConcreteTypes(returnType, transformedTargetSchema)); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
delegationTransforms = delegationTransforms.concat(transforms); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, targetSchema)); | ||
if (info.mergeInfo != null) { | ||
@@ -1044,3 +1114,3 @@ delegationTransforms.push(new AddReplacementFragments(targetSchema, info.mergeInfo.replacementFragments)); | ||
} | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, args, returnType = info.returnType, context, transforms = [], skipValidation, skipTypeMerging, }) { | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, args, returnType = info.returnType, context, transforms = [], transformedSchema, skipValidation, skipTypeMerging, }) { | ||
let targetSchema; | ||
@@ -1063,3 +1133,3 @@ let targetRootValue; | ||
} | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), skipTypeMerging); | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const processedRequest = applyRequestTransforms(request, delegationTransforms); | ||
@@ -1066,0 +1136,0 @@ if (!skipValidation) { |
{ | ||
"name": "@graphql-tools/delegate", | ||
"version": "6.0.0-alpha.1", | ||
"version": "6.0.0-alpha-0bfc960.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,4 +9,4 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/schema": "6.0.0-alpha.1", | ||
"@graphql-tools/utils": "6.0.0-alpha.1", | ||
"@graphql-tools/schema": "6.0.0-alpha-0bfc960.0", | ||
"@graphql-tools/utils": "6.0.0-alpha-0bfc960.0", | ||
"tslib": "1.11.1" | ||
@@ -13,0 +13,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
import { GraphQLSchema, GraphQLOutputType, SelectionSetNode, FieldNode, DocumentNode, GraphQLResolveInfo, GraphQLFieldResolver, InlineFragmentNode } from 'graphql'; | ||
import { GraphQLSchema, GraphQLOutputType, SelectionSetNode, FieldNode, DocumentNode, GraphQLResolveInfo, GraphQLFieldResolver, InlineFragmentNode, FragmentDefinitionNode, GraphQLObjectType, VariableDefinitionNode } from 'graphql'; | ||
import { Operation, Transform, Request, TypeMap, ExecutionResult } from '@graphql-tools/utils'; | ||
@@ -15,2 +15,3 @@ export interface IDelegateToSchemaOptions<TContext = Record<string, any>, TArgs = Record<string, any>> { | ||
transforms?: Array<Transform>; | ||
transformedSchema?: GraphQLSchema; | ||
skipValidation?: boolean; | ||
@@ -29,2 +30,14 @@ skipTypeMerging?: boolean; | ||
} | ||
export interface ICreateRequest { | ||
sourceSchema?: GraphQLSchema; | ||
sourceParentType?: GraphQLObjectType; | ||
sourceFieldName?: string; | ||
fragments?: Record<string, FragmentDefinitionNode>; | ||
variableDefinitions?: ReadonlyArray<VariableDefinitionNode>; | ||
variableValues?: Record<string, any>; | ||
targetOperation: Operation; | ||
targetFieldName: string; | ||
selectionSet?: SelectionSetNode; | ||
fieldNodes?: ReadonlyArray<FieldNode>; | ||
} | ||
export interface MergedTypeInfo { | ||
@@ -49,3 +62,10 @@ subschemas: Array<SubschemaConfig>; | ||
export declare type Subscriber = <TReturn = Record<string, any>, TArgs = Record<string, any>, TContext = Record<string, any>>(params: ExecutionParams<TArgs, TContext>) => Promise<AsyncIterator<ExecutionResult<TReturn>> | ExecutionResult<TReturn>>; | ||
export declare type CreateProxyingResolverFn = (schema: GraphQLSchema | SubschemaConfig, transforms: Array<Transform>, operation: Operation, fieldName: string) => GraphQLFieldResolver<any, any>; | ||
export interface ICreateProxyingResolverOptions { | ||
schema: GraphQLSchema | SubschemaConfig; | ||
transforms?: Array<Transform>; | ||
transformedSchema?: GraphQLSchema; | ||
operation?: Operation; | ||
fieldName?: string; | ||
} | ||
export declare type CreateProxyingResolverFn = (options: ICreateProxyingResolverOptions) => GraphQLFieldResolver<any, any>; | ||
export interface SubschemaConfig { | ||
@@ -52,0 +72,0 @@ schema: GraphQLSchema; |
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
356669
30
2822
+ Added@graphql-tools/schema@6.0.0-alpha-0bfc960.0(transitive)
+ Added@graphql-tools/utils@6.0.0-alpha-0bfc960.0(transitive)
- Removed@graphql-tools/schema@6.0.0-alpha.1(transitive)
- Removed@graphql-tools/utils@6.0.0-alpha.1(transitive)