This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stitching): fixes error mapping with ExtendSchema transform. Also…
…, refactors! = uses createMergedResolver instead of individual wrapFields, extractFields, and renameFields functions. These functionss likely should be deprecated or at least renamed, as their function (creating specialized resolvers for merging) is not clear from current names. createMergedResolver works with multiple layers of wrapping, extracting, or even a combination thereof. = exports new extractFields function for use within a fieldNodeTransformerMap when wrapping fields. = allows specification of the fieldNodeTransformerMap directly on ExtendSchema transform so that separate MapFields transform not required. Under the hood, the ExtendSchema transform calls its own MapFields transform. = fixes file structure; as most of the recent new functionality is stitching functionality rather than transform functionality, even though it relies on the new ExtendSchema and MapFields transforms.
- Loading branch information
Showing
8 changed files
with
230 additions
and
100 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { IFieldResolver } from '../Interfaces'; | ||
import { defaultMergedResolver } from '../stitching'; | ||
import { GraphQLObjectType } from 'graphql'; | ||
import { extractOneLevelOfFields } from './extractFields'; | ||
|
||
export function wrapField(wrapper: string, fieldName: string): IFieldResolver<any, any> { | ||
return createMergedResolver({ fromPath: [wrapper, fieldName] }); | ||
} | ||
|
||
export function extractField(fieldName: string): IFieldResolver<any, any> { | ||
return createMergedResolver({ toPath: [fieldName] }); | ||
} | ||
|
||
export function renameField(fieldName: string): IFieldResolver<any, any> { | ||
return createMergedResolver({ fromPath: [fieldName] }); | ||
} | ||
|
||
export function createMergedResolver({ | ||
fromPath = [], | ||
toPath = [], | ||
}: { | ||
toPath?: Array<string>; | ||
fromPath?: Array<string>; | ||
}): IFieldResolver<any, any> { | ||
return (parent, args, context, info) => { | ||
|
||
let fieldNodes = info.fieldNodes; | ||
let returnType = info.returnType; | ||
let parentType = info.parentType; | ||
let path = info.path; | ||
|
||
toPath.forEach(pathSegment => { | ||
fieldNodes = extractOneLevelOfFields(fieldNodes, pathSegment, info.fragments); | ||
parentType = returnType as GraphQLObjectType; | ||
returnType = (returnType as GraphQLObjectType).getFields()[pathSegment].type; | ||
path = { prev: path, key: pathSegment }; | ||
}); | ||
|
||
if (!fieldNodes.length) { | ||
return null; | ||
} | ||
|
||
let fieldName; | ||
|
||
const fromPathLength = fromPath.length; | ||
if (fromPathLength) { | ||
parent = fromPath.slice(0, -1).reduce((p, pathSegment) => p[pathSegment], parent); | ||
fieldName = fromPath[fromPathLength - 1]; | ||
} | ||
|
||
if (!fieldName) { | ||
fieldName = toPath[toPath.length - 1]; | ||
} | ||
|
||
return defaultMergedResolver(parent, args, context, { | ||
...info, | ||
fieldName, | ||
fieldNodes, | ||
returnType, | ||
parentType, | ||
path, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { FieldNode, FragmentDefinitionNode } from 'graphql'; | ||
import { collectFields } from './collectFields'; | ||
|
||
export function extractFields({ | ||
fieldNode, | ||
path = [], | ||
fragments = {}, | ||
}: { | ||
fieldNode: FieldNode; | ||
path?: Array<string>; | ||
fragments?: Record<string, FragmentDefinitionNode>; | ||
}) { | ||
const fieldNodes = collectFields(fieldNode.selectionSet, fragments); | ||
return path.length ? path.reduce( | ||
(acc, pathSegment) => extractOneLevelOfFields(acc, pathSegment, fragments), | ||
fieldNodes, | ||
) : fieldNodes; | ||
} | ||
|
||
export function extractOneLevelOfFields( | ||
fieldNodes: ReadonlyArray<FieldNode>, | ||
fieldName: string, | ||
fragments: Record<string, FragmentDefinitionNode>, | ||
) { | ||
const newFieldNodes: Array<FieldNode> = []; | ||
fieldNodes.forEach(fieldNode => { | ||
collectFields(fieldNode.selectionSet, fragments).forEach(selection => { | ||
if (selection.name.value === fieldName) { | ||
newFieldNodes.push(selection); | ||
} | ||
}); | ||
}); | ||
return newFieldNodes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.