Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Oct 11, 2017
1 parent 1657ac0 commit c04216d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/stitching/defaultMergedResolver.ts
Expand Up @@ -11,20 +11,20 @@ const defaultMergedResolver: GraphQLFieldResolver<any, any> = (
context,
info,
) => {
const fieldName = info.fieldNodes[0].alias
const responseKey = info.fieldNodes[0].alias
? info.fieldNodes[0].alias.value
: info.fieldName;
const { ownError, childrenErrors } = getErrorsFromParent(parent, fieldName);
if (ownError) {
const errorResult = getErrorsFromParent(parent, responseKey);
if (errorResult.kind === 'OWN') {
throw locatedError(
ownError.message,
errorResult.error.message,
info.fieldNodes,
responsePathAsArray(info.path),
);
} else if (parent) {
let result = parent[fieldName];
if (childrenErrors) {
result = annotateWithChildrenErrors(result, childrenErrors);
let result = parent[responseKey];
if (errorResult.errors) {
result = annotateWithChildrenErrors(result, errorResult.errors);
}
return result;
} else {
Expand Down
31 changes: 19 additions & 12 deletions src/stitching/errors.ts
@@ -1,7 +1,7 @@
import { GraphQLResolveInfo, responsePathAsArray } from 'graphql';
import { locatedError } from 'graphql/error';

const ERROR_SYMBOL = Symbol('Schema Merging Error');
const ERROR_SYMBOL = Symbol('subSchemaErrors');

export function annotateWithChildrenErrors(
object: any,
Expand Down Expand Up @@ -39,37 +39,44 @@ export function annotateWithChildrenErrors(
export function getErrorsFromParent(
object: any,
fieldName: string,
): {
ownError?: any;
childrenErrors?: Array<{ path?: Array<string | number> }>;
} {
):
| {
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 {
ownError: error,
kind: 'OWN',
error,
};
} else if (error.path[0] === fieldName) {
childrenErrors.push(error);
}
}
return {
childrenErrors,
kind: 'CHILDREN',
errors: childrenErrors,
};
}

export function checkResultAndHandleErrors(
result: any,
info: GraphQLResolveInfo,
fieldName?: string,
responseKey?: string,
): any {
if (!fieldName) {
fieldName = info.fieldNodes[0].alias
if (!responseKey) {
responseKey = info.fieldNodes[0].alias
? info.fieldNodes[0].alias.value
: info.fieldName;
}
if (result.errors && (!result.data || result.data[fieldName] == null)) {
if (result.errors && (!result.data || result.data[responseKey] == null)) {
const errorMessage = result.errors
.map((error: { message: string }) => error.message)
.join('\n');
Expand All @@ -79,7 +86,7 @@ export function checkResultAndHandleErrors(
responsePathAsArray(info.path),
);
} else {
let resultObject = result.data[fieldName];
let resultObject = result.data[responseKey];
if (result.errors) {
resultObject = annotateWithChildrenErrors(
resultObject,
Expand Down

0 comments on commit c04216d

Please sign in to comment.