Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Sep 13, 2020
1 parent fbb88a4 commit 63cbd13
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/delegate/src/externalObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function mergeExternalObjects(
let errors: Array<GraphQLError> = [];

sources.forEach((source, index) => {
if (source instanceof GraphQLError) {
if (source instanceof GraphQLError || source === null) {
const selectionSet = selectionSets[index];
const fieldNodes = collectFields(
{
Expand All @@ -57,7 +57,8 @@ export function mergeExternalObjects(
);
const nullResult = {};
Object.keys(fieldNodes).forEach(responseKey => {
nullResult[responseKey] = relocatedError(source, path.concat([responseKey]));
nullResult[responseKey] =
source instanceof GraphQLError ? relocatedError(source, path.concat([responseKey])) : null;
});
results.push(nullResult);
} else {
Expand Down
163 changes: 163 additions & 0 deletions packages/stitch/tests/mergeFailures.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { stitchSchemas } from '@graphql-tools/stitch';
import { ExecutionResult } from '@graphql-tools/utils';
import { graphql, GraphQLError, GraphQLSchema } from 'graphql';

describe('merge failures', () => {
const firstSchema = makeExecutableSchema({
typeDefs: `
type Thing {
id: ID!
name: String!
}
type Query {
thing(id: ID!): Thing
}
`,
resolvers: {
Query: {
thing: (_root, { id }) => ({ id, name: 'The Thing' }),
}
}
});

const getGatewaySchema = (secondSchema: GraphQLSchema): GraphQLSchema => stitchSchemas({
subschemas: [
{
schema: firstSchema,
merge: {
Thing: {
selectionSet: '{ id }',
fieldName: 'thing',
args: ({ id }) => ({ id }),
}
}
},
{
schema: secondSchema,
merge: {
Thing: {
selectionSet: '{ id }',
fieldName: '_thing',
args: ({ id }) => ({ id }),
}
}
},
],
mergeTypes: true
});

it('proxies merged errors', async () => {
const secondSchema = makeExecutableSchema({
typeDefs: `
type Thing {
id: ID!
description: String!
}
type Query {
_thing(id: ID!): Thing
}
`,
resolvers: {
Query: {
_thing: () => new Error('unable to produce the thing'),
}
}
});

const gatewaySchema = getGatewaySchema(secondSchema);

const result = await graphql(gatewaySchema, `
query {
thing(id: 23) {
id
name
description
}
}
`);

const expectedResult: ExecutionResult = {
data: { thing: null },
errors: [new GraphQLError('unable to produce the thing')],
}

expect(result).toEqual(expectedResult);
});

it('proxies inappropriate null', async () => {
const secondSchema = makeExecutableSchema({
typeDefs: `
type Thing {
id: ID!
description: String!
}
type Query {
_thing(id: ID!): Thing
}
`,
resolvers: {
Query: {
_thing: () => null,
}
}
});

const gatewaySchema = getGatewaySchema(secondSchema);

const result = await graphql(gatewaySchema, `
query {
thing(id: 23) {
id
name
description
}
}
`);

const expectedResult: ExecutionResult = {
data: { thing: null },
errors: [new GraphQLError('Cannot return null for non-nullable field Thing.description.')],
}

expect(result).toEqual(expectedResult);
});

it('proxies errors on object', async () => {
const secondSchema = makeExecutableSchema({
typeDefs: `
type Thing {
id: ID!
description: String!
}
type Query {
_thing(id: ID!): Thing
}
`,
resolvers: {
Query: {
_thing: () => ({}),
}
}
});

const gatewaySchema = getGatewaySchema(secondSchema);

const result = await graphql(gatewaySchema, `
query {
thing(id: 23) {
id
name
description
}
}
`);

const expectedResult: ExecutionResult = {
data: { thing: null },
errors: [new GraphQLError('Cannot return null for non-nullable field Thing.description.')],
}

expect(result).toEqual(expectedResult);
});
});

0 comments on commit 63cbd13

Please sign in to comment.