-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
2e53294
commit c6910ab
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/apollo-gateway/src/__tests__/integration/custom-directives.test.ts
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,104 @@ | ||
import gql from 'graphql-tag'; | ||
import { execute } from '../execution-utils'; | ||
|
||
import * as accounts from '../__fixtures__/schemas/accounts'; | ||
import * as books from '../__fixtures__/schemas/books'; | ||
import * as inventory from '../__fixtures__/schemas/inventory'; | ||
import * as product from '../__fixtures__/schemas/product'; | ||
import * as reviews from '../__fixtures__/schemas/reviews'; | ||
|
||
import { astSerializer, queryPlanSerializer } from '../../snapshotSerializers'; | ||
|
||
expect.addSnapshotSerializer(astSerializer); | ||
expect.addSnapshotSerializer(queryPlanSerializer); | ||
|
||
describe('custom executable directives', () => { | ||
it('passes directives along in requests to underlying services', async () => { | ||
const query = gql` | ||
query GetReviewers { | ||
topReviews { | ||
body @stream | ||
} | ||
} | ||
`; | ||
|
||
const { data, errors, queryPlan } = await execute( | ||
[accounts, books, inventory, product, reviews], | ||
{ | ||
query, | ||
}, | ||
); | ||
|
||
expect(errors).toBeUndefined(); | ||
expect(queryPlan).toCallService('reviews'); | ||
expect(queryPlan).toMatchInlineSnapshot(` | ||
QueryPlan { | ||
Fetch(service: "reviews") { | ||
{ | ||
topReviews { | ||
body @stream | ||
} | ||
} | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it("returns validation errors when directives aren't present across all services", async () => { | ||
const invalidService = { | ||
name: 'invalidService', | ||
typeDefs: gql` | ||
directive @invalid on QUERY | ||
`, | ||
}; | ||
|
||
const query = gql` | ||
query GetReviewers { | ||
topReviews { | ||
body @stream | ||
} | ||
} | ||
`; | ||
|
||
expect( | ||
execute([accounts, books, inventory, product, reviews, invalidService], { | ||
query, | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
"[@stream] -> Custom directives must be implemented in every service. The following services do not implement the @stream directive: invalidService. | ||
[@invalid] -> Custom directives must be implemented in every service. The following services do not implement the @invalid directive: accounts, books, inventory, product, reviews." | ||
`); | ||
}); | ||
|
||
it("returns validation errors when directives aren't identical across all services", async () => { | ||
const invalidService = { | ||
name: 'invalid', | ||
typeDefs: gql` | ||
directive @stream on QUERY | ||
`, | ||
}; | ||
|
||
const query = gql` | ||
query GetReviewers { | ||
topReviews { | ||
body @stream | ||
} | ||
} | ||
`; | ||
|
||
expect( | ||
execute([accounts, books, inventory, product, reviews, invalidService], { | ||
query, | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
"[@stream] -> custom directives must be defined identically across all services. See below for a list of current implementations: | ||
accounts: directive @stream on FIELD | ||
books: directive @stream on FIELD | ||
inventory: directive @stream on FIELD | ||
product: directive @stream on FIELD | ||
reviews: directive @stream on FIELD | ||
invalid: directive @stream on QUERY" | ||
`); | ||
}); | ||
}); |