Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle errors better in merged schemas (#425)
* Translate paths * Throw errors per field with errors from remote schema
- Loading branch information
1 parent
6793c0e
commit 3e4b61a
Showing
8 changed files
with
362 additions
and
119 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GraphQLFieldResolver, responsePathAsArray } from 'graphql'; | ||
import { locatedError } from 'graphql/error'; | ||
import { getErrorsFromParent, annotateWithChildrenErrors } from './errors'; | ||
|
||
// Resolver that knows how to: | ||
// a) handle aliases for proxied schemas | ||
// b) handle errors from proxied schemas | ||
const defaultMergedResolver: GraphQLFieldResolver<any, any> = ( | ||
parent, | ||
args, | ||
context, | ||
info, | ||
) => { | ||
const responseKey = info.fieldNodes[0].alias | ||
? info.fieldNodes[0].alias.value | ||
: info.fieldName; | ||
const errorResult = getErrorsFromParent(parent, responseKey); | ||
if (errorResult.kind === 'OWN') { | ||
throw locatedError( | ||
errorResult.error.message, | ||
info.fieldNodes, | ||
responsePathAsArray(info.path), | ||
); | ||
} else if (parent) { | ||
let result = parent[responseKey]; | ||
if (errorResult.errors) { | ||
result = annotateWithChildrenErrors(result, errorResult.errors); | ||
} | ||
return result; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
export default defaultMergedResolver; |
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,98 @@ | ||
import { GraphQLResolveInfo, responsePathAsArray } from 'graphql'; | ||
import { locatedError } from 'graphql/error'; | ||
|
||
const ERROR_SYMBOL = Symbol('subSchemaErrors'); | ||
|
||
export function annotateWithChildrenErrors( | ||
object: any, | ||
childrenErrors: Array<{ path?: Array<string | number> }>, | ||
): any { | ||
if (childrenErrors && childrenErrors.length > 0) { | ||
if (Array.isArray(object)) { | ||
const byIndex = {}; | ||
childrenErrors.forEach(error => { | ||
const index = error.path[1]; | ||
const current = byIndex[index] || []; | ||
current.push({ | ||
...error, | ||
path: error.path.slice(1), | ||
}); | ||
byIndex[index] = current; | ||
}); | ||
return object.map((item, index) => | ||
annotateWithChildrenErrors(item, byIndex[index]), | ||
); | ||
} else { | ||
return { | ||
...object, | ||
[ERROR_SYMBOL]: childrenErrors.map(error => ({ | ||
...error, | ||
path: error.path.slice(1), | ||
})), | ||
}; | ||
} | ||
} else { | ||
return object; | ||
} | ||
} | ||
|
||
export function getErrorsFromParent( | ||
object: any, | ||
fieldName: string, | ||
): | ||
| { | ||
kind: 'OWN'; | ||
error: any; | ||
} | ||
| { | ||
kind: 'CHILDREN'; | ||
errors?: Array<{ path?: Array<string | number> }>; | ||
} { | ||
const errors = (object && object[ERROR_SYMBOL]) || []; | ||
const childrenErrors: Array<{ path?: Array<string | number> }> = []; | ||
for (const error of errors) { | ||
if (error.path.length === 1 && error.path[0] === fieldName) { | ||
return { | ||
kind: 'OWN', | ||
error, | ||
}; | ||
} else if (error.path[0] === fieldName) { | ||
childrenErrors.push(error); | ||
} | ||
} | ||
return { | ||
kind: 'CHILDREN', | ||
errors: childrenErrors, | ||
}; | ||
} | ||
|
||
export function checkResultAndHandleErrors( | ||
result: any, | ||
info: GraphQLResolveInfo, | ||
responseKey?: string, | ||
): any { | ||
if (!responseKey) { | ||
responseKey = info.fieldNodes[0].alias | ||
? info.fieldNodes[0].alias.value | ||
: info.fieldName; | ||
} | ||
if (result.errors && (!result.data || result.data[responseKey] == null)) { | ||
const errorMessage = result.errors | ||
.map((error: { message: string }) => error.message) | ||
.join('\n'); | ||
throw locatedError( | ||
errorMessage, | ||
info.fieldNodes, | ||
responsePathAsArray(info.path), | ||
); | ||
} else { | ||
let resultObject = result.data[responseKey]; | ||
if (result.errors) { | ||
resultObject = annotateWithChildrenErrors( | ||
resultObject, | ||
result.errors as Array<{ path?: Array<string> }>, | ||
); | ||
} | ||
return resultObject; | ||
} | ||
} |
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.