Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2821 from lgandecki/passContextToLocalDataStore
pass context to localGraphQLDatasource for testing purposes
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 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
44 changes: 44 additions & 0 deletions
44
packages/apollo-gateway/src/datasources/__tests__/LocalGraphQLDatasource.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,44 @@ | ||
import { LocalGraphQLDataSource } from '../LocalGraphQLDatasource'; | ||
import { buildFederatedSchema } from '@apollo/federation'; | ||
import gql from 'graphql-tag'; | ||
|
||
describe('constructing requests', () => { | ||
it('accepts context', async () => { | ||
const typeDefs = gql` | ||
type Query { | ||
me: User | ||
} | ||
type User { | ||
id: ID | ||
name: String! | ||
} | ||
`; | ||
const resolvers = { | ||
Query: { | ||
me(_, __, { userId }) { | ||
const users = [ | ||
{ id: 1, name: 'otherGuy' }, | ||
{ id: 2, name: 'james' }, | ||
{ | ||
id: 3, | ||
name: 'someoneElse', | ||
}, | ||
]; | ||
return users.find(user => user.id === userId); | ||
}, | ||
}, | ||
}; | ||
const schema = buildFederatedSchema([{ typeDefs, resolvers }]); | ||
|
||
const DataSource = new LocalGraphQLDataSource(schema); | ||
|
||
const { data } = await DataSource.process({ | ||
request: { | ||
query: '{ me { name } }', | ||
}, | ||
context: { userId: 2 }, | ||
}); | ||
|
||
expect(data).toEqual({ me: { name: 'james' } }); | ||
}); | ||
}); |