From c48dbc418c41cc1d39b7e96b5c4d3be9621e1e11 Mon Sep 17 00:00:00 2001 From: robbywashere Date: Sat, 27 Oct 2018 18:33:25 -0400 Subject: [PATCH] closes #167 --- src/__tests__/restLink.ts | 47 +++++++++++++++++++++++++++++++++++++++ src/restLink.ts | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/__tests__/restLink.ts b/src/__tests__/restLink.ts index 2c1c87f..83e9f7a 100755 --- a/src/__tests__/restLink.ts +++ b/src/__tests__/restLink.ts @@ -1827,6 +1827,52 @@ describe('Query options', () => { afterEach(() => { fetchMock.restore(); }); + + describe('context', () => { + it('makes context values available in query', async () => { + const contextMiddleware = new ApolloLink((operation, forward) => { + operation.setContext({ + constants: { lang: 'en' }, + }); + return forward(operation).map(result => { + const { + constants: { lang }, + } = operation.getContext(); + expect(lang).toBe('en'); + return result; + }); + }); + + const link = ApolloLink.from([ + contextMiddleware, + new RestLink({ + uri: '/api', + }), + ]); + + const postQuery = gql` + query postTitle { + post + @rest(type: "Post", path: "/posts?lang={context.constants.lang}") { + id + title + } + } + `; + + fetchMock.get('path:/api/posts', {}); + + await makePromise( + execute(link, { + operationName: 'postQuery', + query: postQuery, + }), + ); + + expect(fetchMock.called('/api/posts?lang=en')).toBe(true); + }); + }); + describe('credentials', () => { it('adds credentials to the request from the setup', async () => { expect.assertions(1); @@ -2767,6 +2813,7 @@ describe('Mutation', () => { afterEach(() => { fetchMock.restore(); }); + it('builds request body containing Strings/Objects/Arrays types without changing their types', async () => { // tests convertObjectKeys functionality // see: https://github.com/apollographql/apollo-link-rest/issues/45 diff --git a/src/restLink.ts b/src/restLink.ts index 0053358..c0dfb11 100755 --- a/src/restLink.ts +++ b/src/restLink.ts @@ -783,7 +783,7 @@ interface RequestContext { /** Exported variables fulfilled in this request, using @export(as:) */ exportVariables: { [key: string]: any }; - + constants?: { [key: string]: any }; endpoints: RestLink.Endpoints; customFetch: RestLink.CustomFetch; operationType: OperationTypeNode; @@ -1245,6 +1245,7 @@ export class RestLink extends ApolloLink { const requestContext: RequestContext = { headers, + constants: context.constants, endpoints: this.endpoints, // Provide an empty hash for this request's exports to be stuffed into exportVariables: {},