Skip to content

Commit

Permalink
Merge pull request #2821 from lgandecki/passContextToLocalDataStore
Browse files Browse the repository at this point in the history
pass context to localGraphQLDatasource for testing purposes
  • Loading branch information
James Baxley committed Jun 12, 2019
2 parents 79fc259 + a4db6d4 commit b4c6642
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### vNext

- `apollo-gateway`: Pass `context` through to the `graphql` command in `LocalGraphQLDatasource` `process` method [PR #2821](https://github.com/apollographql/apollo-server/pull/2821)
- `apollo-engine-reporting`: Set `forbiddenOperation` and `registeredOperation` later in the request lifecycle [PR #2828](https://github.com/apollographql/apollo-server/pull/2828)

### v2.6.2
Expand Down
Expand Up @@ -18,6 +18,7 @@ export class LocalGraphQLDataSource implements GraphQLDataSource {

async process<TContext>({
request,
context,
}: Pick<GraphQLRequestContext<TContext>, 'request' | 'context'>): Promise<
GraphQLResponse
> {
Expand All @@ -26,6 +27,7 @@ export class LocalGraphQLDataSource implements GraphQLDataSource {
source: request.query!,
variableValues: request.variables,
operationName: request.operationName,
contextValue: context,
});
}

Expand Down
@@ -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' } });
});
});

0 comments on commit b4c6642

Please sign in to comment.