-
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.
Validate variables at the gateway level (#3213)
Previously, the gateway depended on downstream services to report any errors with graphql execution. In the case of invalid variables, it's not necessary to make any fetches or even build a query plan since we can perform that validation when the request is first received. Validating at the gateway also guarantees that we surface invalid queries to users, independent of how the downstream services are implemented and what kind of validation _they_ perform on incoming requests.
- Loading branch information
1 parent
a0290c2
commit e151191
Showing
2 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/apollo-gateway/src/__tests__/gateway/executor.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,38 @@ | ||
import gql from 'graphql-tag'; | ||
import { ApolloGateway } from '../../'; | ||
|
||
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'; | ||
|
||
describe('ApolloGateway executor', () => { | ||
it('validates requests prior to execution', async () => { | ||
const gateway = new ApolloGateway({ | ||
localServiceList: [accounts, books, inventory, product, reviews], | ||
}); | ||
|
||
const { executor } = await gateway.load(); | ||
|
||
const { errors } = await executor({ | ||
document: gql` | ||
query InvalidVariables($first: Int!) { | ||
topReviews(first: $first) { | ||
body | ||
} | ||
} | ||
`, | ||
request: { | ||
variables: { first: '3' }, | ||
}, | ||
queryHash: 'hashed', | ||
context: null, | ||
cache: {} as any, | ||
}); | ||
|
||
expect(errors![0].message).toMatch( | ||
'Variable "$first" got invalid value "3"; Expected type Int.', | ||
); | ||
}); | ||
}); |
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