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

Commit

Permalink
fix(stitching): pass along more errors
Browse files Browse the repository at this point in the history
partially addresses #26.
  • Loading branch information
yaacovCR committed Mar 26, 2020
1 parent d978ef6 commit d7e7b8b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/stitching/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export function relocatedError(
}

export function annotateWithChildrenErrors(object: any, childrenErrors: ReadonlyArray<GraphQLError>): any {
if (!object) {
return object;
}

if (!Array.isArray(childrenErrors)) {
object[ERROR_SYMBOL] = [];
return object;
Expand Down
90 changes: 88 additions & 2 deletions src/test/testErrors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from 'chai';
import { GraphQLResolveInfo, GraphQLError } from 'graphql';
import { expect, assert } from 'chai';
import { GraphQLResolveInfo, GraphQLError, graphql } from 'graphql';
import {
relocatedError,
getErrorsFromParent,
Expand All @@ -8,6 +8,8 @@ import {
import { checkResultAndHandleErrors } from '../stitching/checkResultAndHandleErrors';

import 'mocha';
import { makeExecutableSchema } from '../makeExecutableSchema';
import { mergeSchemas } from '../stitching';

class ErrorWithExtensions extends GraphQLError {
constructor(message: string, code: string) {
Expand Down Expand Up @@ -96,3 +98,87 @@ describe('Errors', () => {
});
});
});

describe('passes along errors for missing fields on list', () => {
it('hides error if null allowed', async () => {
const typeDefs = `
type Query {
getOuter: Outer
}
type Outer {
innerList: [Inner]!
}
type Inner {
mandatoryField: String!
}
`;

const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Query: {
getOuter: () => ({
innerList: [{}]
})
},
}
});

const mergedSchema = mergeSchemas({
schemas: [schema]
});
const result = await graphql(mergedSchema, `{ getOuter { innerList { mandatoryField } } }`);
expect(result).to.deep.equal({
data: {
getOuter: {
innerList: [null],
},
},
});
});

it('if non-null', async () => {
const typeDefs = `
type Query {
getOuter: Outer
}
type Outer {
innerList: [Inner!]!
}
type Inner {
mandatoryField: String!
}
`;

const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Query: {
getOuter: () => ({
innerList: [{}]
})
},
}
});

const mergedSchema = mergeSchemas({
schemas: [schema]
});
const result = await graphql(mergedSchema, `{ getOuter { innerList { mandatoryField } } }`);
expect(result).to.deep.equal({
data: {
getOuter: null,
},
errors: [{
locations: [{
column: 26,
line: 1,
}],
message: 'Cannot return null for non-nullable field Inner.mandatoryField.',
path: [
'getOuter',
],
}]
});
});
});

0 comments on commit d7e7b8b

Please sign in to comment.