@graphql-tools/delegate
Advanced tools
Comparing version 6.0.0-alpha-ece9edc.0 to 6.0.0-alpha-f92a671.0
169
index.cjs.js
@@ -427,9 +427,9 @@ 'use strict'; | ||
class AddReplacementSelectionSets { | ||
constructor(schema, mapping) { | ||
this.schema = schema; | ||
class AddFragmentsByField { | ||
constructor(targetSchema, mapping) { | ||
this.targetSchema = targetSchema; | ||
this.mapping = mapping; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithSelectionSet(this.schema, originalRequest.document, this.mapping); | ||
const document = addFragmentsByField(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -441,4 +441,4 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithSelectionSet(schema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(schema); | ||
function addFragmentsByField(targetSchema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(targetSchema); | ||
return graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, { | ||
@@ -454,5 +454,5 @@ [graphql.Kind.SELECTION_SET](node) { | ||
const name = selection.name.value; | ||
const selectionSet = mapping[parentTypeName][name]; | ||
if (selectionSet != null) { | ||
selections = selections.concat(selectionSet.selections); | ||
const fragment = mapping[parentTypeName][name]; | ||
if (fragment != null) { | ||
selections = selections.concat(fragment); | ||
} | ||
@@ -473,9 +473,9 @@ } | ||
class AddReplacementFragments { | ||
constructor(targetSchema, mapping) { | ||
this.targetSchema = targetSchema; | ||
class AddSelectionSetsByField { | ||
constructor(schema, mapping) { | ||
this.schema = schema; | ||
this.mapping = mapping; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithFragments(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = addSelectionSetsByField(this.schema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -487,4 +487,4 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithFragments(targetSchema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(targetSchema); | ||
function addSelectionSetsByField(schema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(schema); | ||
return graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, { | ||
@@ -500,5 +500,5 @@ [graphql.Kind.SELECTION_SET](node) { | ||
const name = selection.name.value; | ||
const fragment = mapping[parentTypeName][name]; | ||
if (fragment != null) { | ||
selections = selections.concat(fragment); | ||
const selectionSet = mapping[parentTypeName][name]; | ||
if (selectionSet != null) { | ||
selections = selections.concat(selectionSet.selections); | ||
} | ||
@@ -519,3 +519,3 @@ } | ||
class AddMergedTypeFragments { | ||
class AddSelectionSetsByType { | ||
constructor(targetSchema, mapping) { | ||
@@ -526,3 +526,3 @@ this.targetSchema = targetSchema; | ||
transformRequest(originalRequest) { | ||
const document = addMergedTypeSelectionSets(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = addSelectionSetsByType(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -534,3 +534,3 @@ ...originalRequest, | ||
} | ||
function addMergedTypeSelectionSets(targetSchema, document, mapping) { | ||
function addSelectionSetsByType(targetSchema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(targetSchema); | ||
@@ -544,3 +544,3 @@ return graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, { | ||
if (parentTypeName in mapping) { | ||
const selectionSet = mapping[parentTypeName].selectionSet; | ||
const selectionSet = mapping[parentTypeName]; | ||
if (selectionSet != null) { | ||
@@ -598,3 +598,3 @@ selections = selections.concat(selectionSet.selections); | ||
function handleNull(fieldNodes, path, errors) { | ||
function handleNull(errors) { | ||
if (errors.length) { | ||
@@ -613,3 +613,3 @@ if (errors.some(error => !error.path || error.path.length < 2)) { | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result[pathSegment] = handleNull(fieldNodes, [...path, pathSegment], childErrors[pathSegment]); | ||
result[pathSegment] = handleNull(childErrors[pathSegment]); | ||
}); | ||
@@ -621,3 +621,3 @@ return result; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result.push(handleNull(fieldNodes, [...path, parseInt(pathSegment, 10)], childErrors[pathSegment])); | ||
result.push(handleNull(childErrors[pathSegment])); | ||
}); | ||
@@ -640,3 +640,3 @@ return result; | ||
function unwrapResult(parent, info, path) { | ||
function unwrapResult(parent, path) { | ||
let newParent = parent; | ||
@@ -650,3 +650,3 @@ const pathLength = path.length; | ||
if (object == null) { | ||
return handleNull(info.fieldNodes, graphql.responsePathAsArray(info.path), errors); | ||
return handleNull(errors); | ||
} | ||
@@ -796,9 +796,11 @@ utils.setErrors(object, errors.map(error => utils.relocatedError(error, error.path != null ? error.path.slice(1) : undefined))); | ||
function handleObject(type, object, errors, subschema, context, info, skipTypeMerging) { | ||
var _a; | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
utils.setErrors(object, errors.map(error => utils.slicedError(error))); | ||
setObjectSubschema(object, subschema); | ||
if (skipTypeMerging || !info.mergeInfo) { | ||
if (skipTypeMerging || !stitchingInfo) { | ||
return object; | ||
} | ||
const typeName = graphql.isAbstractType(type) ? info.schema.getTypeMap()[object.__typename].name : type.name; | ||
const mergedTypeInfo = info.mergeInfo.mergedTypes[typeName]; | ||
const mergedTypeInfo = stitchingInfo.mergedTypes[typeName]; | ||
let targetSubschemas; | ||
@@ -847,7 +849,7 @@ if (mergedTypeInfo != null) { | ||
const childErrors = utils.getErrorsByPathSegment(errors); | ||
return list.map((listMember, index) => handleListMember(graphql.getNullableType(type.ofType), listMember, index, index in childErrors ? childErrors[index] : [], subschema, context, info, skipTypeMerging)); | ||
return list.map((listMember, index) => handleListMember(graphql.getNullableType(type.ofType), listMember, index in childErrors ? childErrors[index] : [], subschema, context, info, skipTypeMerging)); | ||
} | ||
function handleListMember(type, listMember, index, errors, subschema, context, info, skipTypeMerging) { | ||
function handleListMember(type, listMember, errors, subschema, context, info, skipTypeMerging) { | ||
if (listMember == null) { | ||
return handleNull(info.fieldNodes, [...graphql.responsePathAsArray(info.path), index], errors); | ||
return handleNull(errors); | ||
} | ||
@@ -868,3 +870,3 @@ if (graphql.isLeafType(type)) { | ||
if (result == null) { | ||
return handleNull(info.fieldNodes, graphql.responsePathAsArray(info.path), errors); | ||
return handleNull(errors); | ||
} | ||
@@ -995,3 +997,3 @@ if (graphql.isLeafType(type)) { | ||
} | ||
function createRequestFromInfo({ info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes, }) { | ||
function createRequestFromInfo({ info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes = info.fieldNodes, }) { | ||
return createRequest({ | ||
@@ -1007,9 +1009,10 @@ sourceSchema: info.schema, | ||
selectionSet, | ||
fieldNodes: selectionSet != null ? undefined : fieldNodes != null ? fieldNodes : info.fieldNodes, | ||
fieldNodes, | ||
}); | ||
} | ||
function createRequest({ sourceSchema, sourceParentType, sourceFieldName, fragments, variableDefinitions, variableValues, targetOperation, targetFieldName, selectionSet, fieldNodes, }) { | ||
var _a; | ||
let newSelectionSet = selectionSet; | ||
let argumentNodeMap; | ||
if (selectionSet != null && fieldNodes == null) { | ||
if (fieldNodes == null) { | ||
argumentNodeMap = Object.create(null); | ||
@@ -1025,6 +1028,10 @@ } | ||
: undefined; | ||
argumentNodeMap = fieldNodes[0].arguments.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name.value]: curr, | ||
}), {}); | ||
argumentNodeMap = {}; | ||
const args = (_a = fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments; | ||
if (args) { | ||
argumentNodeMap = args.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name.value]: curr, | ||
}), argumentNodeMap); | ||
} | ||
} | ||
@@ -1112,17 +1119,36 @@ const newVariables = Object.create(null); | ||
} | ||
function getDelegationReturnType(info, targetSchema, operation, fieldName) { | ||
if (info != null) { | ||
return info.returnType; | ||
} | ||
let rootType; | ||
if (operation === 'query') { | ||
rootType = targetSchema.getQueryType(); | ||
} | ||
else if (operation === 'mutation') { | ||
rootType = targetSchema.getMutationType(); | ||
} | ||
else { | ||
rootType = targetSchema.getSubscriptionType(); | ||
} | ||
return rootType.getFields()[fieldName].type; | ||
} | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, transformedSchema, skipTypeMerging) { | ||
var _a; | ||
var _a, _b; | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
let delegationTransforms = [ | ||
new CheckResultAndHandleErrors(info, fieldName, subschemaOrSubschemaConfig, context, returnType, skipTypeMerging), | ||
]; | ||
if (info.mergeInfo != null) { | ||
delegationTransforms.push(new AddReplacementSelectionSets(info.schema, info.mergeInfo.replacementSelectionSets), new AddMergedTypeFragments(info.schema, info.mergeInfo.mergedTypes)); | ||
if (stitchingInfo != null) { | ||
delegationTransforms.push(new AddSelectionSetsByField(info.schema, stitchingInfo.selectionSetsByField), new AddSelectionSetsByType(info.schema, stitchingInfo.selectionSetsByType)); | ||
} | ||
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; | ||
const transformedTargetSchema = stitchingInfo == null | ||
? transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : targetSchema : (_b = transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : stitchingInfo.transformedSchemas.get(subschemaOrSubschemaConfig)) !== null && _b !== void 0 ? _b : targetSchema; | ||
delegationTransforms.push(new WrapConcreteTypes(returnType, transformedTargetSchema)); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
if (info != null) { | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
} | ||
delegationTransforms = delegationTransforms.concat(transforms); | ||
if (info.mergeInfo != null) { | ||
delegationTransforms.push(new AddReplacementFragments(targetSchema, info.mergeInfo.replacementFragments)); | ||
if (stitchingInfo != null) { | ||
delegationTransforms.push(new AddFragmentsByField(targetSchema, stitchingInfo.fragmentsByField)); | ||
} | ||
@@ -1135,3 +1161,21 @@ if (args != null) { | ||
} | ||
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, }) { | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation, fieldName, args, returnType, context, transforms = [], transformedSchema, skipValidation, skipTypeMerging, }) { | ||
var _a; | ||
let operationDefinition; | ||
let targetOperation; | ||
let targetFieldName; | ||
if (operation == null) { | ||
operationDefinition = graphql.getOperationAST(request.document, undefined); | ||
targetOperation = operationDefinition.operation; | ||
} | ||
else { | ||
targetOperation = operation; | ||
} | ||
if (fieldName == null) { | ||
operationDefinition = operationDefinition !== null && operationDefinition !== void 0 ? operationDefinition : graphql.getOperationAST(request.document, undefined); | ||
targetFieldName = operationDefinition.selectionSet.selections[0].name.value; | ||
} | ||
else { | ||
targetFieldName = fieldName; | ||
} | ||
let targetSchema; | ||
@@ -1144,4 +1188,3 @@ let targetRootValue; | ||
targetSchema = subschemaConfig.schema; | ||
targetRootValue = | ||
rootValue != null ? rootValue : subschemaConfig.rootValue != null ? subschemaConfig.rootValue : info.rootValue; | ||
targetRootValue = (_a = rootValue !== null && rootValue !== void 0 ? rootValue : subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) !== null && _a !== void 0 ? _a : info === null || info === void 0 ? void 0 : info.rootValue; | ||
if (subschemaConfig.transforms != null) { | ||
@@ -1153,5 +1196,5 @@ requestTransforms = requestTransforms.concat(subschemaConfig.transforms); | ||
targetSchema = subschemaOrSubschemaConfig; | ||
targetRootValue = rootValue != null ? rootValue : info.rootValue; | ||
targetRootValue = rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue; | ||
} | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, targetFieldName, args, returnType !== null && returnType !== void 0 ? returnType : getDelegationReturnType(info, targetSchema, targetOperation, targetFieldName), requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const processedRequest = utils.applyRequestTransforms(request, delegationTransforms); | ||
@@ -1169,3 +1212,3 @@ if (!skipValidation) { | ||
} | ||
if (operation === 'query' || operation === 'mutation') { | ||
if (targetOperation === 'query' || targetOperation === 'mutation') { | ||
const executor = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.executor) || createDefaultExecutor(targetSchema, (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) || targetRootValue); | ||
@@ -1197,3 +1240,3 @@ const executionResult = executor({ | ||
return { | ||
[info.fieldName]: transformedResult, | ||
[targetFieldName]: transformedResult, | ||
}; | ||
@@ -1206,6 +1249,6 @@ }); | ||
function createDefaultExecutor(schema, rootValue) { | ||
return ({ document, context, variables, info }) => graphql.execute(schema, document, rootValue || info.rootValue, context, variables); | ||
return ({ document, context, variables, info }) => graphql.execute(schema, document, rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue, context, variables); | ||
} | ||
function createDefaultSubscriber(schema, rootValue) { | ||
return ({ document, context, variables, info }) => graphql.subscribe(schema, document, rootValue || info.rootValue, context, variables); | ||
return ({ document, context, variables, info }) => graphql.subscribe(schema, document, rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue, context, variables); | ||
} | ||
@@ -1238,3 +1281,3 @@ | ||
const unwrappingResolver = fromPath != null | ||
? (parent, args, context, info) => parentErrorResolver(unwrapResult(parent, info, fromPath), args, context, info) | ||
? (parent, args, context, info) => parentErrorResolver(unwrapResult(parent, fromPath), args, context, info) | ||
: parentErrorResolver; | ||
@@ -1268,3 +1311,3 @@ const dehoistingResolver = dehoist | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithFragments$1(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = replaceFieldsWithFragments(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -1276,3 +1319,3 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithFragments$1(targetSchema, document, mapping) { | ||
function replaceFieldsWithFragments(targetSchema, document, mapping) { | ||
const typeInfo = new graphql.TypeInfo(targetSchema); | ||
@@ -1330,5 +1373,5 @@ return graphql.visit(document, graphql.visitWithTypeInfo(typeInfo, { | ||
exports.AddArgumentsAsVariables = AddArgumentsAsVariables; | ||
exports.AddMergedTypeSelectionSets = AddMergedTypeFragments; | ||
exports.AddReplacementFragments = AddReplacementFragments; | ||
exports.AddReplacementSelectionSets = AddReplacementSelectionSets; | ||
exports.AddFragmentsByField = AddFragmentsByField; | ||
exports.AddMergedTypeSelectionSets = AddSelectionSetsByType; | ||
exports.AddSelectionSetsByField = AddSelectionSetsByField; | ||
exports.AddTypenameToAbstract = AddTypenameToAbstract; | ||
@@ -1335,0 +1378,0 @@ exports.CheckResultAndHandleErrors = CheckResultAndHandleErrors; |
167
index.esm.js
@@ -1,2 +0,2 @@ | ||
import { visit, visitWithTypeInfo, Kind, getNamedType, isAbstractType, TypeInfo, isObjectType, isInterfaceType, TypeNameMetaFieldDef, responsePathAsArray, getNullableType, isLeafType, isCompositeType, isListType, typeFromAST, isSchema, validate, execute, subscribe, defaultFieldResolver, parse } from 'graphql'; | ||
import { visit, visitWithTypeInfo, Kind, getNamedType, isAbstractType, TypeInfo, isObjectType, isInterfaceType, TypeNameMetaFieldDef, getNullableType, isLeafType, isCompositeType, isListType, typeFromAST, isSchema, getOperationAST, validate, execute, subscribe, defaultFieldResolver, parse } from 'graphql'; | ||
import { implementsAbstractType, CombinedError, getErrorsByPathSegment, ERROR_SYMBOL, relocatedError, mergeDeep, getErrors, setErrors, slicedError, collectFields, getResponseKeyFromInfo, updateArgument, serializeInputValue, applyRequestTransforms, applyResultTransforms, mapAsyncIterator, concatInlineFragments } from '@graphql-tools/utils'; | ||
@@ -423,9 +423,9 @@ | ||
class AddReplacementSelectionSets { | ||
constructor(schema, mapping) { | ||
this.schema = schema; | ||
class AddFragmentsByField { | ||
constructor(targetSchema, mapping) { | ||
this.targetSchema = targetSchema; | ||
this.mapping = mapping; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithSelectionSet(this.schema, originalRequest.document, this.mapping); | ||
const document = addFragmentsByField(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -437,4 +437,4 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithSelectionSet(schema, document, mapping) { | ||
const typeInfo = new TypeInfo(schema); | ||
function addFragmentsByField(targetSchema, document, mapping) { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
return visit(document, visitWithTypeInfo(typeInfo, { | ||
@@ -450,5 +450,5 @@ [Kind.SELECTION_SET](node) { | ||
const name = selection.name.value; | ||
const selectionSet = mapping[parentTypeName][name]; | ||
if (selectionSet != null) { | ||
selections = selections.concat(selectionSet.selections); | ||
const fragment = mapping[parentTypeName][name]; | ||
if (fragment != null) { | ||
selections = selections.concat(fragment); | ||
} | ||
@@ -469,9 +469,9 @@ } | ||
class AddReplacementFragments { | ||
constructor(targetSchema, mapping) { | ||
this.targetSchema = targetSchema; | ||
class AddSelectionSetsByField { | ||
constructor(schema, mapping) { | ||
this.schema = schema; | ||
this.mapping = mapping; | ||
} | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithFragments(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = addSelectionSetsByField(this.schema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -483,4 +483,4 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithFragments(targetSchema, document, mapping) { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
function addSelectionSetsByField(schema, document, mapping) { | ||
const typeInfo = new TypeInfo(schema); | ||
return visit(document, visitWithTypeInfo(typeInfo, { | ||
@@ -496,5 +496,5 @@ [Kind.SELECTION_SET](node) { | ||
const name = selection.name.value; | ||
const fragment = mapping[parentTypeName][name]; | ||
if (fragment != null) { | ||
selections = selections.concat(fragment); | ||
const selectionSet = mapping[parentTypeName][name]; | ||
if (selectionSet != null) { | ||
selections = selections.concat(selectionSet.selections); | ||
} | ||
@@ -515,3 +515,3 @@ } | ||
class AddMergedTypeFragments { | ||
class AddSelectionSetsByType { | ||
constructor(targetSchema, mapping) { | ||
@@ -522,3 +522,3 @@ this.targetSchema = targetSchema; | ||
transformRequest(originalRequest) { | ||
const document = addMergedTypeSelectionSets(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = addSelectionSetsByType(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -530,3 +530,3 @@ ...originalRequest, | ||
} | ||
function addMergedTypeSelectionSets(targetSchema, document, mapping) { | ||
function addSelectionSetsByType(targetSchema, document, mapping) { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
@@ -540,3 +540,3 @@ return visit(document, visitWithTypeInfo(typeInfo, { | ||
if (parentTypeName in mapping) { | ||
const selectionSet = mapping[parentTypeName].selectionSet; | ||
const selectionSet = mapping[parentTypeName]; | ||
if (selectionSet != null) { | ||
@@ -594,3 +594,3 @@ selections = selections.concat(selectionSet.selections); | ||
function handleNull(fieldNodes, path, errors) { | ||
function handleNull(errors) { | ||
if (errors.length) { | ||
@@ -609,3 +609,3 @@ if (errors.some(error => !error.path || error.path.length < 2)) { | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result[pathSegment] = handleNull(fieldNodes, [...path, pathSegment], childErrors[pathSegment]); | ||
result[pathSegment] = handleNull(childErrors[pathSegment]); | ||
}); | ||
@@ -617,3 +617,3 @@ return result; | ||
Object.keys(childErrors).forEach(pathSegment => { | ||
result.push(handleNull(fieldNodes, [...path, parseInt(pathSegment, 10)], childErrors[pathSegment])); | ||
result.push(handleNull(childErrors[pathSegment])); | ||
}); | ||
@@ -636,3 +636,3 @@ return result; | ||
function unwrapResult(parent, info, path) { | ||
function unwrapResult(parent, path) { | ||
let newParent = parent; | ||
@@ -646,3 +646,3 @@ const pathLength = path.length; | ||
if (object == null) { | ||
return handleNull(info.fieldNodes, responsePathAsArray(info.path), errors); | ||
return handleNull(errors); | ||
} | ||
@@ -792,9 +792,11 @@ setErrors(object, errors.map(error => relocatedError(error, error.path != null ? error.path.slice(1) : undefined))); | ||
function handleObject(type, object, errors, subschema, context, info, skipTypeMerging) { | ||
var _a; | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
setErrors(object, errors.map(error => slicedError(error))); | ||
setObjectSubschema(object, subschema); | ||
if (skipTypeMerging || !info.mergeInfo) { | ||
if (skipTypeMerging || !stitchingInfo) { | ||
return object; | ||
} | ||
const typeName = isAbstractType(type) ? info.schema.getTypeMap()[object.__typename].name : type.name; | ||
const mergedTypeInfo = info.mergeInfo.mergedTypes[typeName]; | ||
const mergedTypeInfo = stitchingInfo.mergedTypes[typeName]; | ||
let targetSubschemas; | ||
@@ -843,7 +845,7 @@ if (mergedTypeInfo != null) { | ||
const childErrors = getErrorsByPathSegment(errors); | ||
return list.map((listMember, index) => handleListMember(getNullableType(type.ofType), listMember, index, index in childErrors ? childErrors[index] : [], subschema, context, info, skipTypeMerging)); | ||
return list.map((listMember, index) => handleListMember(getNullableType(type.ofType), listMember, index in childErrors ? childErrors[index] : [], subschema, context, info, skipTypeMerging)); | ||
} | ||
function handleListMember(type, listMember, index, errors, subschema, context, info, skipTypeMerging) { | ||
function handleListMember(type, listMember, errors, subschema, context, info, skipTypeMerging) { | ||
if (listMember == null) { | ||
return handleNull(info.fieldNodes, [...responsePathAsArray(info.path), index], errors); | ||
return handleNull(errors); | ||
} | ||
@@ -864,3 +866,3 @@ if (isLeafType(type)) { | ||
if (result == null) { | ||
return handleNull(info.fieldNodes, responsePathAsArray(info.path), errors); | ||
return handleNull(errors); | ||
} | ||
@@ -991,3 +993,3 @@ if (isLeafType(type)) { | ||
} | ||
function createRequestFromInfo({ info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes, }) { | ||
function createRequestFromInfo({ info, operation = getDelegatingOperation(info.parentType, info.schema), fieldName = info.fieldName, selectionSet, fieldNodes = info.fieldNodes, }) { | ||
return createRequest({ | ||
@@ -1003,9 +1005,10 @@ sourceSchema: info.schema, | ||
selectionSet, | ||
fieldNodes: selectionSet != null ? undefined : fieldNodes != null ? fieldNodes : info.fieldNodes, | ||
fieldNodes, | ||
}); | ||
} | ||
function createRequest({ sourceSchema, sourceParentType, sourceFieldName, fragments, variableDefinitions, variableValues, targetOperation, targetFieldName, selectionSet, fieldNodes, }) { | ||
var _a; | ||
let newSelectionSet = selectionSet; | ||
let argumentNodeMap; | ||
if (selectionSet != null && fieldNodes == null) { | ||
if (fieldNodes == null) { | ||
argumentNodeMap = Object.create(null); | ||
@@ -1021,6 +1024,10 @@ } | ||
: undefined; | ||
argumentNodeMap = fieldNodes[0].arguments.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name.value]: curr, | ||
}), {}); | ||
argumentNodeMap = {}; | ||
const args = (_a = fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments; | ||
if (args) { | ||
argumentNodeMap = args.reduce((prev, curr) => ({ | ||
...prev, | ||
[curr.name.value]: curr, | ||
}), argumentNodeMap); | ||
} | ||
} | ||
@@ -1108,17 +1115,36 @@ const newVariables = Object.create(null); | ||
} | ||
function getDelegationReturnType(info, targetSchema, operation, fieldName) { | ||
if (info != null) { | ||
return info.returnType; | ||
} | ||
let rootType; | ||
if (operation === 'query') { | ||
rootType = targetSchema.getQueryType(); | ||
} | ||
else if (operation === 'mutation') { | ||
rootType = targetSchema.getMutationType(); | ||
} | ||
else { | ||
rootType = targetSchema.getSubscriptionType(); | ||
} | ||
return rootType.getFields()[fieldName].type; | ||
} | ||
function buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, transforms, transformedSchema, skipTypeMerging) { | ||
var _a; | ||
var _a, _b; | ||
const stitchingInfo = (_a = info === null || info === void 0 ? void 0 : info.schema.extensions) === null || _a === void 0 ? void 0 : _a.stitchingInfo; | ||
let delegationTransforms = [ | ||
new CheckResultAndHandleErrors(info, fieldName, subschemaOrSubschemaConfig, context, returnType, skipTypeMerging), | ||
]; | ||
if (info.mergeInfo != null) { | ||
delegationTransforms.push(new AddReplacementSelectionSets(info.schema, info.mergeInfo.replacementSelectionSets), new AddMergedTypeFragments(info.schema, info.mergeInfo.mergedTypes)); | ||
if (stitchingInfo != null) { | ||
delegationTransforms.push(new AddSelectionSetsByField(info.schema, stitchingInfo.selectionSetsByField), new AddSelectionSetsByType(info.schema, stitchingInfo.selectionSetsByType)); | ||
} | ||
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; | ||
const transformedTargetSchema = stitchingInfo == null | ||
? transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : targetSchema : (_b = transformedSchema !== null && transformedSchema !== void 0 ? transformedSchema : stitchingInfo.transformedSchemas.get(subschemaOrSubschemaConfig)) !== null && _b !== void 0 ? _b : targetSchema; | ||
delegationTransforms.push(new WrapConcreteTypes(returnType, transformedTargetSchema)); | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
if (info != null) { | ||
delegationTransforms.push(new ExpandAbstractTypes(info.schema, transformedTargetSchema)); | ||
} | ||
delegationTransforms = delegationTransforms.concat(transforms); | ||
if (info.mergeInfo != null) { | ||
delegationTransforms.push(new AddReplacementFragments(targetSchema, info.mergeInfo.replacementFragments)); | ||
if (stitchingInfo != null) { | ||
delegationTransforms.push(new AddFragmentsByField(targetSchema, stitchingInfo.fragmentsByField)); | ||
} | ||
@@ -1131,3 +1157,21 @@ if (args != null) { | ||
} | ||
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, }) { | ||
function delegateRequest({ request, schema: subschemaOrSubschemaConfig, rootValue, info, operation, fieldName, args, returnType, context, transforms = [], transformedSchema, skipValidation, skipTypeMerging, }) { | ||
var _a; | ||
let operationDefinition; | ||
let targetOperation; | ||
let targetFieldName; | ||
if (operation == null) { | ||
operationDefinition = getOperationAST(request.document, undefined); | ||
targetOperation = operationDefinition.operation; | ||
} | ||
else { | ||
targetOperation = operation; | ||
} | ||
if (fieldName == null) { | ||
operationDefinition = operationDefinition !== null && operationDefinition !== void 0 ? operationDefinition : getOperationAST(request.document, undefined); | ||
targetFieldName = operationDefinition.selectionSet.selections[0].name.value; | ||
} | ||
else { | ||
targetFieldName = fieldName; | ||
} | ||
let targetSchema; | ||
@@ -1140,4 +1184,3 @@ let targetRootValue; | ||
targetSchema = subschemaConfig.schema; | ||
targetRootValue = | ||
rootValue != null ? rootValue : subschemaConfig.rootValue != null ? subschemaConfig.rootValue : info.rootValue; | ||
targetRootValue = (_a = rootValue !== null && rootValue !== void 0 ? rootValue : subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) !== null && _a !== void 0 ? _a : info === null || info === void 0 ? void 0 : info.rootValue; | ||
if (subschemaConfig.transforms != null) { | ||
@@ -1149,5 +1192,5 @@ requestTransforms = requestTransforms.concat(subschemaConfig.transforms); | ||
targetSchema = subschemaOrSubschemaConfig; | ||
targetRootValue = rootValue != null ? rootValue : info.rootValue; | ||
targetRootValue = rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue; | ||
} | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, fieldName, args, returnType, requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const delegationTransforms = buildDelegationTransforms(subschemaOrSubschemaConfig, info, context, targetSchema, targetFieldName, args, returnType !== null && returnType !== void 0 ? returnType : getDelegationReturnType(info, targetSchema, targetOperation, targetFieldName), requestTransforms.reverse(), transformedSchema, skipTypeMerging); | ||
const processedRequest = applyRequestTransforms(request, delegationTransforms); | ||
@@ -1165,3 +1208,3 @@ if (!skipValidation) { | ||
} | ||
if (operation === 'query' || operation === 'mutation') { | ||
if (targetOperation === 'query' || targetOperation === 'mutation') { | ||
const executor = (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.executor) || createDefaultExecutor(targetSchema, (subschemaConfig === null || subschemaConfig === void 0 ? void 0 : subschemaConfig.rootValue) || targetRootValue); | ||
@@ -1193,3 +1236,3 @@ const executionResult = executor({ | ||
return { | ||
[info.fieldName]: transformedResult, | ||
[targetFieldName]: transformedResult, | ||
}; | ||
@@ -1202,6 +1245,6 @@ }); | ||
function createDefaultExecutor(schema, rootValue) { | ||
return ({ document, context, variables, info }) => execute(schema, document, rootValue || info.rootValue, context, variables); | ||
return ({ document, context, variables, info }) => execute(schema, document, rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue, context, variables); | ||
} | ||
function createDefaultSubscriber(schema, rootValue) { | ||
return ({ document, context, variables, info }) => subscribe(schema, document, rootValue || info.rootValue, context, variables); | ||
return ({ document, context, variables, info }) => subscribe(schema, document, rootValue !== null && rootValue !== void 0 ? rootValue : info === null || info === void 0 ? void 0 : info.rootValue, context, variables); | ||
} | ||
@@ -1234,3 +1277,3 @@ | ||
const unwrappingResolver = fromPath != null | ||
? (parent, args, context, info) => parentErrorResolver(unwrapResult(parent, info, fromPath), args, context, info) | ||
? (parent, args, context, info) => parentErrorResolver(unwrapResult(parent, fromPath), args, context, info) | ||
: parentErrorResolver; | ||
@@ -1264,3 +1307,3 @@ const dehoistingResolver = dehoist | ||
transformRequest(originalRequest) { | ||
const document = replaceFieldsWithFragments$1(this.targetSchema, originalRequest.document, this.mapping); | ||
const document = replaceFieldsWithFragments(this.targetSchema, originalRequest.document, this.mapping); | ||
return { | ||
@@ -1272,3 +1315,3 @@ ...originalRequest, | ||
} | ||
function replaceFieldsWithFragments$1(targetSchema, document, mapping) { | ||
function replaceFieldsWithFragments(targetSchema, document, mapping) { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
@@ -1325,3 +1368,3 @@ return visit(document, visitWithTypeInfo(typeInfo, { | ||
export { AddArgumentsAsVariables, AddMergedTypeFragments as AddMergedTypeSelectionSets, AddReplacementFragments, AddReplacementSelectionSets, AddTypenameToAbstract, CheckResultAndHandleErrors, ExpandAbstractTypes, FilterToSchema, ReplaceFieldWithFragment, checkResultAndHandleErrors, createMergedResolver, createRequest, createRequestFromInfo, defaultMergedResolver, delegateRequest, delegateToSchema, getSubschema, handleResult, isSubschemaConfig }; | ||
export { AddArgumentsAsVariables, AddFragmentsByField, AddSelectionSetsByType as AddMergedTypeSelectionSets, AddSelectionSetsByField, AddTypenameToAbstract, CheckResultAndHandleErrors, ExpandAbstractTypes, FilterToSchema, ReplaceFieldWithFragment, checkResultAndHandleErrors, createMergedResolver, createRequest, createRequestFromInfo, defaultMergedResolver, delegateRequest, delegateToSchema, getSubschema, handleResult, isSubschemaConfig }; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@graphql-tools/delegate", | ||
"version": "6.0.0-alpha-ece9edc.0", | ||
"version": "6.0.0-alpha-f92a671.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,5 +9,5 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/schema": "6.0.0-alpha-ece9edc.0", | ||
"@graphql-tools/utils": "6.0.0-alpha-ece9edc.0", | ||
"tslib": "1.11.1" | ||
"@graphql-tools/schema": "6.0.0-alpha-f92a671.0", | ||
"@graphql-tools/utils": "6.0.0-alpha-f92a671.0", | ||
"tslib": "~2.0.0" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": "git@github.com:ardatan/graphql-tools.git", |
@@ -1,5 +0,4 @@ | ||
import { GraphQLResolveInfo } from 'graphql'; | ||
export declare function isProxiedResult(result: any): any; | ||
export declare function unwrapResult(parent: any, info: GraphQLResolveInfo, path: Array<string>): any; | ||
export declare function unwrapResult(parent: any, path: Array<string>): any; | ||
export declare function dehoistResult(parent: any, delimeter?: string): any; | ||
export declare function mergeProxiedResults(target: any, ...sources: any): any; |
@@ -1,2 +0,2 @@ | ||
import { FieldNode, GraphQLError } from 'graphql'; | ||
export declare function handleNull(fieldNodes: ReadonlyArray<FieldNode>, path: Array<string | number>, errors: ReadonlyArray<GraphQLError>): {}; | ||
import { GraphQLError } from 'graphql'; | ||
export declare function handleNull(errors: ReadonlyArray<GraphQLError>): {}; |
export { default as CheckResultAndHandleErrors } from './CheckResultAndHandleErrors'; | ||
export { checkResultAndHandleErrors } from './CheckResultAndHandleErrors'; | ||
export { default as ExpandAbstractTypes } from './ExpandAbstractTypes'; | ||
export { default as AddReplacementSelectionSets } from './AddReplacementSelectionSets'; | ||
export { default as AddMergedTypeSelectionSets } from './AddMergedTypeSelectionSets'; | ||
export { default as AddSelectionSetsByField } from './AddSelectionSetsByField'; | ||
export { default as AddMergedTypeSelectionSets } from './AddSelectionSetsByType'; | ||
export { default as AddArgumentsAsVariables } from './AddArgumentsAsVariables'; | ||
@@ -10,2 +10,2 @@ export { default as FilterToSchema } from './FilterToSchema'; | ||
export { default as ReplaceFieldWithFragment } from './ReplaceFieldWithFragment'; | ||
export { default as AddReplacementFragments } from './AddReplacementFragments'; | ||
export { default as AddFragmentsByField } from './AddFragmentsByField'; |
@@ -19,4 +19,5 @@ import { GraphQLSchema, GraphQLOutputType, SelectionSetNode, FieldNode, DocumentNode, GraphQLResolveInfo, GraphQLFieldResolver, InlineFragmentNode, FragmentDefinitionNode, GraphQLObjectType, VariableDefinitionNode } from 'graphql'; | ||
} | ||
export interface IDelegateRequestOptions extends IDelegateToSchemaOptions { | ||
export interface IDelegateRequestOptions extends Omit<IDelegateToSchemaOptions, 'info'> { | ||
request: Request; | ||
info?: GraphQLResolveInfo; | ||
} | ||
@@ -84,13 +85,10 @@ export interface ICreateRequestFromInfo { | ||
} | ||
export interface ReplacementSelectionSetMapping { | ||
[typeName: string]: { | ||
[fieldName: string]: SelectionSetNode; | ||
}; | ||
} | ||
export interface ReplacementFragmentMapping { | ||
[typeName: string]: { | ||
[fieldName: string]: InlineFragmentNode; | ||
}; | ||
} | ||
export declare type MergedTypeResolver = (originalResult: any, context: Record<string, any>, info: GraphQLResolveInfo, subschema: GraphQLSchema | SubschemaConfig, selectionSet: SelectionSetNode) => any; | ||
export declare function isSubschemaConfig(value: any): value is SubschemaConfig; | ||
export interface StitchingInfo { | ||
transformedSchemas: Map<GraphQLSchema | SubschemaConfig, GraphQLSchema>; | ||
fragmentsByField: Record<string, Record<string, InlineFragmentNode>>; | ||
selectionSetsByField: Record<string, Record<string, SelectionSetNode>>; | ||
selectionSetsByType: Record<string, SelectionSetNode>; | ||
mergedTypes: Record<string, MergedTypeInfo>; | ||
} |
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
366197
2902
+ Added@graphql-tools/schema@6.0.0-alpha-f92a671.0(transitive)
+ Added@graphql-tools/utils@6.0.0-alpha-f92a671.0(transitive)
+ Addedtslib@1.14.12.0.3(transitive)
- Removed@graphql-tools/schema@6.0.0-alpha-ece9edc.0(transitive)
- Removed@graphql-tools/utils@6.0.0-alpha-ece9edc.0(transitive)
- Removedtslib@1.11.12.8.0(transitive)
Updatedtslib@~2.0.0