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): wrapping and hoisting field transforms
BREAKING CHANGE: Previous version of createMergedResolver did not work with multiple layers of field wrapping. extractFields was not working, deprecated in favor of hoistFieldNodes. createMergedResolver now relies on two helper functions, dehoistResult, complement to hoistFieldNoes, and unwrapResult, complement to wrapFieldNodes.
- Loading branch information
Showing
9 changed files
with
180 additions
and
219 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,81 +1,35 @@ | ||
import { GraphQLObjectType, getNamedType, responsePathAsArray } from 'graphql'; | ||
import { IFieldResolver } from '../Interfaces'; | ||
import { | ||
getErrors, | ||
getSubschemas, | ||
} from './proxiedResult'; | ||
import { unwrapResult, dehoistResult } from './proxiedResult'; | ||
import defaultMergedResolver from './defaultMergedResolver'; | ||
import { extractOneLevelOfFields } from './extractFields'; | ||
import { handleNull, handleObject } from './checkResultAndHandleErrors'; | ||
|
||
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 = [], | ||
fromPath, | ||
fromField, | ||
dehoist, | ||
}: { | ||
fromPath?: Array<string>; | ||
toPath?: Array<string>; | ||
fromField?: string; | ||
dehoist?: string; | ||
}): IFieldResolver<any, any> { | ||
return async (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 = getNamedType(returnType) as GraphQLObjectType; | ||
returnType = (parentType as GraphQLObjectType).getFields()[pathSegment].type; | ||
path = { prev: path, key: pathSegment }; | ||
}); | ||
|
||
if (!fieldNodes.length) { | ||
return null; | ||
return (parent, args, context, info) => { | ||
if (dehoist) { | ||
parent = dehoistResult(parent, dehoist); | ||
} | ||
|
||
let fieldName; | ||
|
||
const fromPathLength = fromPath.length; | ||
if (fromPathLength) { | ||
const fromParentPathLength = fromPathLength - 1; | ||
|
||
for (let i = 0; i < fromParentPathLength; i++) { | ||
const responseKey = fromPath[i]; | ||
const errors = getErrors(parent, responseKey); | ||
const subschemas = getSubschemas(parent); | ||
const result = parent[responseKey]; | ||
if (result == null) { | ||
return handleNull(fieldNodes, responsePathAsArray(path), errors); | ||
} | ||
parent = handleObject(result, errors, subschemas); | ||
} | ||
|
||
fieldName = fromPath[fromPathLength - 1]; | ||
if (fromPath) { | ||
parent = unwrapResult(parent, info, fromPath); | ||
} | ||
|
||
if (!fieldName) { | ||
fieldName = toPath[toPath.length - 1]; | ||
if (!parent) { | ||
parent = {}; | ||
} | ||
|
||
return defaultMergedResolver(parent, args, context, { | ||
...info, | ||
fieldName, | ||
fieldNodes, | ||
returnType, | ||
parentType, | ||
path, | ||
}); | ||
return parent instanceof Error ? | ||
parent : fromField ? | ||
defaultMergedResolver(parent, args, context, { | ||
...info, | ||
fieldName: fromField, | ||
}) : | ||
defaultMergedResolver(parent, args, context, info); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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.