Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
feat(BREAKING CHANGE: removed contextware): contextware is out, use s…
Browse files Browse the repository at this point in the history
…tandard context link from apoll

see https://github.com/dacz/apollo-bridge-link-example

BREAKING CHANGE: removed contextware
  • Loading branch information
dacz committed Dec 30, 2017
1 parent f0c3627 commit 670fc95
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 54 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"devDependencies": {
"apollo-cache-inmemory": "^1.1.4",
"apollo-client": "^2.0.4",
"apollo-link-context": "^1.0.3",
"ava": "^0.24.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
Expand Down
27 changes: 2 additions & 25 deletions src/bridgeLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { addMockFunctionsToSchema, makeExecutableSchema } from 'graphql-tools';
import { ApolloLink } from 'apollo-link';
import Observable from 'zen-observable';

export const createBridgeLink = ({
schema,
resolvers,
mock,
context = {},
contextware = [],
}) => {
export const createBridgeLink = ({ schema, resolvers, mock }) => {
let executableSchema;
if (typeof schema === 'string') {
executableSchema = makeExecutableSchema({ typeDefs: schema, resolvers });
Expand All @@ -36,28 +30,11 @@ export const createBridgeLink = ({
return new ApolloLink(
operation =>
new Observable(observer => {
const { headers, credentials } = operation.getContext();
const ctxInitial = {
...context,
...(context.headers || headers
? { headers: { ...context.headers, ...headers } }
: {}),
credentials,
};

// contextware
const ctx = [].concat(contextware).reduce((acc, fn) => {
const rv = fn(acc, operation);
return typeof rv === 'object' && !Array.isArray(rv)
? { ...acc, ...rv }
: acc;
}, ctxInitial);

graphql(
executableSchema,
print(operation.query),
undefined,
ctx,
operation.getContext(),
operation.variables,
operation.operationName
)
Expand Down
47 changes: 18 additions & 29 deletions src/bridgeLink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import { POSTS } from '../testUtils/queries.example';
import { makeExecutableSchema } from 'graphql-tools';
import schemaExample from '../testUtils/schema.example';
import { setContext } from 'apollo-link-context';
import sinon from 'sinon';
import test from 'ava';

Expand Down Expand Up @@ -53,6 +54,12 @@ test('BridgeLink calls deep resolver', async t => {
},
];
const authorReturned = { ...author, __typename: 'User' };
const context = {
GraphQl: 'isCool',
headers: {
'X-hulu': 'sun',
},
};
const opts = {
schema: schemaExample,
resolvers: {
Expand All @@ -63,53 +70,35 @@ test('BridgeLink calls deep resolver', async t => {
author: sinon.stub().returns(Promise.resolve(author)),
},
},
context: {
GraphQl: 'isCool',
headers: {
'X-hulu': 'sun',
},
},
};
const link = new BridgeLink(opts);
const contextLink = setContext(() => context);
const bridgeLink = new BridgeLink(opts);
const cache = new InMemoryCache();
const client = new ApolloClient({ link, cache });
const client = new ApolloClient({
link: contextLink.concat(bridgeLink),
cache,
});
const res = await client.query({ query: POSTS });
t.true(opts.resolvers.Post.author.calledOnce);
const calledWithContext = opts.resolvers.Post.author.args[0][2];
t.is(calledWithContext.GraphQl, opts.context.GraphQl);
t.is(calledWithContext.headers['X-hulu'], opts.context.headers['X-hulu']);
t.deepEqual(res.data.posts[0].author, authorReturned);
t.pass();
});

test('BridgeLink mocks data', async t => {
const opts = {
schema: schemaExample,
mock: true,
};
const link = new BridgeLink(opts);
const cache = new InMemoryCache();
const client = new ApolloClient({ link, cache });
const res = await client.query({ query: POSTS });
t.true(res.data.posts.length > 0, 'should get some mocked data');
t.truthy(res.data.posts[0].author.id, 'should have mocked author');
// keeps context as is
const calledWithContext = opts.resolvers.Post.author.args[0][2];
t.is(calledWithContext.GraphQl, context.GraphQl);
t.is(calledWithContext.headers['X-hulu'], context.headers['X-hulu']);
});

test('BridgeLink calls contextware', async t => {
const returning = { middle: 'data' };
const init = sinon.mock().returns(returning);
test('BridgeLink mocks data', async t => {
const opts = {
schema: schemaExample,
mock: true,
contextware: init,
};
const link = new BridgeLink(opts);
const cache = new InMemoryCache();
const client = new ApolloClient({ link, cache });
const res = await client.query({ query: POSTS });
t.true(res.data.posts.length > 0, 'should get some mocked data');
t.truthy(res.data.posts[0].author.id, 'should have mocked author');
t.true(init.calledOnce);
});

test('BridgeLink should accept executable schema', async t => {
Expand Down

0 comments on commit 670fc95

Please sign in to comment.