Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
refactor(handleNull): refactor to handleErrors
Browse files Browse the repository at this point in the history
Consolidates a bit more code within checkResultAndHandleErrors.
  • Loading branch information
yaacovCR committed Nov 7, 2019
1 parent eea0e7a commit b39ca7f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
34 changes: 9 additions & 25 deletions src/stitching/checkResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ export function checkResultAndHandleErrors(
responseKey = getResponseKeyFromInfo(info);
}

if (!result.data) {
if (result.errors) {
throw relocatedError(
combineErrors(result.errors),
info.fieldNodes,
responsePathAsArray(info.path)
);
} else {
return null;
}
}

if (result.data[responseKey] == null) {
return handleNull(info, result.errors || []);
if (!result.data || result.data[responseKey] == null) {
return (result.errors) ? handleErrors(info, result.errors) : null;
}

return handleResult(info, result.data[responseKey], result.errors || []);
Expand Down Expand Up @@ -72,17 +60,13 @@ function parseOutputValue(type: GraphQLType, value: any) {
}
}

export function handleNull(
export function handleErrors(
info: GraphQLResolveInfo,
errors: ReadonlyArray<GraphQLError>,
): null {
if (errors.length) {
throw relocatedError(
combineErrors(errors),
info.fieldNodes,
responsePathAsArray(info.path)
);
} else {
return null;
}
) {
throw relocatedError(
combineErrors(errors),
info.fieldNodes,
responsePathAsArray(info.path)
);
}
4 changes: 2 additions & 2 deletions src/stitching/defaultMergedResolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLFieldResolver, defaultFieldResolver } from 'graphql';
import { getErrorsFromParent, MERGED_NULL_SYMBOL } from './errors';
import { handleResult, handleNull } from './checkResultAndHandleErrors';
import { handleResult, handleErrors } from './checkResultAndHandleErrors';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';

// Resolver that knows how to:
Expand All @@ -24,7 +24,7 @@ const defaultMergedResolver: GraphQLFieldResolver<any, any> = (parent, args, con
const result = parent[responseKey];

if (result == null || result[MERGED_NULL_SYMBOL]) {
return handleNull(info, errors);
return (errors.length) ? handleErrors(info, errors) : null;
}

return handleResult(info, result, errors);
Expand Down
13 changes: 3 additions & 10 deletions src/stitching/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {
ASTNode
} from 'graphql';

export let MERGED_NULL_SYMBOL: any;
export let ERROR_SYMBOL: any;
if (
(typeof global !== 'undefined' && 'Symbol' in global) ||
(typeof window !== 'undefined' && 'Symbol' in window)
) {
MERGED_NULL_SYMBOL = Symbol('mergedNull');
ERROR_SYMBOL = Symbol('subSchemaErrors');
} else {
MERGED_NULL_SYMBOL = '@@__mergedNull';
ERROR_SYMBOL = '@@__subSchemaErrors';
}

Expand Down Expand Up @@ -40,16 +43,6 @@ export function relocatedError(
);
}

export let MERGED_NULL_SYMBOL: any;
if (
(typeof global !== 'undefined' && 'Symbol' in global) ||
(typeof window !== 'undefined' && 'Symbol' in window)
) {
MERGED_NULL_SYMBOL = Symbol('mergedNull');
} else {
MERGED_NULL_SYMBOL = '@@__mergedNull';
}

export function createMergedResult(object: any, childrenErrors: ReadonlyArray<GraphQLError> = []): any {
if (!object) {
object = {
Expand Down

0 comments on commit b39ca7f

Please sign in to comment.