Skip to content

Commit 0829357

Browse files
committed
Add integration tests for custom directives
These tests verify that the query plan includes custom directives in requests to underlying services. They also ensure that validation errors are as expected when services define custom directives in an invalid way.
1 parent a812d8d commit 0829357

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import gql from 'graphql-tag';
2+
import { execute } from '../execution-utils';
3+
4+
import * as accounts from '../__fixtures__/schemas/accounts';
5+
import * as books from '../__fixtures__/schemas/books';
6+
import * as inventory from '../__fixtures__/schemas/inventory';
7+
import * as product from '../__fixtures__/schemas/product';
8+
import * as reviews from '../__fixtures__/schemas/reviews';
9+
10+
import { astSerializer, queryPlanSerializer } from '../../snapshotSerializers';
11+
12+
expect.addSnapshotSerializer(astSerializer);
13+
expect.addSnapshotSerializer(queryPlanSerializer);
14+
15+
describe('custom executable directives', () => {
16+
it('passes directives along in requests to underlying services', async () => {
17+
const query = gql`
18+
query GetReviewers {
19+
topReviews {
20+
body @stream
21+
}
22+
}
23+
`;
24+
25+
const { data, errors, queryPlan } = await execute(
26+
[accounts, books, inventory, product, reviews],
27+
{
28+
query,
29+
},
30+
);
31+
32+
expect(errors).toBeUndefined();
33+
expect(queryPlan).toCallService('reviews');
34+
expect(queryPlan).toMatchInlineSnapshot(`
35+
QueryPlan {
36+
Fetch(service: "reviews") {
37+
{
38+
topReviews {
39+
body @stream
40+
}
41+
}
42+
},
43+
}
44+
`);
45+
});
46+
47+
it("returns validation errors when directives aren't present across all services", async () => {
48+
const invalidService = {
49+
name: 'invalidService',
50+
typeDefs: gql`
51+
directive @invalid on QUERY
52+
`,
53+
};
54+
55+
const query = gql`
56+
query GetReviewers {
57+
topReviews {
58+
body @stream
59+
}
60+
}
61+
`;
62+
63+
expect(
64+
execute([accounts, books, inventory, product, reviews, invalidService], {
65+
query,
66+
}),
67+
).rejects.toThrowErrorMatchingInlineSnapshot(`
68+
"[@stream] -> Custom directives must be implemented in every service. The following services do not implement the @stream directive: invalidService.
69+
70+
[@invalid] -> Custom directives must be implemented in every service. The following services do not implement the @invalid directive: accounts, books, inventory, product, reviews."
71+
`);
72+
});
73+
74+
it("returns validation errors when directives aren't identical across all services", async () => {
75+
const invalidService = {
76+
name: 'invalid',
77+
typeDefs: gql`
78+
directive @stream on QUERY
79+
`,
80+
};
81+
82+
const query = gql`
83+
query GetReviewers {
84+
topReviews {
85+
body @stream
86+
}
87+
}
88+
`;
89+
90+
expect(
91+
execute([accounts, books, inventory, product, reviews, invalidService], {
92+
query,
93+
}),
94+
).rejects.toThrowErrorMatchingInlineSnapshot(`
95+
"[@stream] -> custom directives must be defined identically across all services. See below for a list of current implementations:
96+
accounts: directive @stream on FIELD
97+
books: directive @stream on FIELD
98+
inventory: directive @stream on FIELD
99+
product: directive @stream on FIELD
100+
reviews: directive @stream on FIELD
101+
invalid: directive @stream on QUERY"
102+
`);
103+
});
104+
});

0 commit comments

Comments
 (0)