Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #167 #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/__tests__/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result>(
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);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: {},
Expand Down