-
-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
166 additions
and
2 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 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,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); | ||
}); | ||
}); |