From 670fc95d34e175db2aabd83526e33c76411b8579 Mon Sep 17 00:00:00 2001 From: DavidC Date: Sat, 30 Dec 2017 16:05:15 +0100 Subject: [PATCH] feat(BREAKING CHANGE: removed contextware): contextware is out, use standard context link from apoll see https://github.com/dacz/apollo-bridge-link-example BREAKING CHANGE: removed contextware --- package-lock.json | 9 ++++++++ package.json | 1 + src/bridgeLink.js | 27 ++---------------------- src/bridgeLink.test.js | 47 ++++++++++++++++-------------------------- 4 files changed, 30 insertions(+), 54 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2249061..8ca5f14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -665,6 +665,15 @@ } } }, + "apollo-link-context": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/apollo-link-context/-/apollo-link-context-1.0.3.tgz", + "integrity": "sha512-TS9dtmVQvpKfE/M9B886FaCYQwTT3Fd+kJRFt0Osg4KhOcrEHYbuMvY1DVlr6clZ4vD6Vu15v1D1A8YgXuABiQ==", + "dev": true, + "requires": { + "apollo-link": "1.0.7" + } + }, "apollo-link-dedup": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/apollo-link-dedup/-/apollo-link-dedup-1.0.5.tgz", diff --git a/package.json b/package.json index 5c3ca57..36e5d95 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bridgeLink.js b/src/bridgeLink.js index 3d0284a..b649d8a 100644 --- a/src/bridgeLink.js +++ b/src/bridgeLink.js @@ -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 }); @@ -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 ) diff --git a/src/bridgeLink.test.js b/src/bridgeLink.test.js index d602b4e..7af2d74 100644 --- a/src/bridgeLink.test.js +++ b/src/bridgeLink.test.js @@ -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'; @@ -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: { @@ -63,45 +70,28 @@ 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(); @@ -109,7 +99,6 @@ test('BridgeLink calls contextware', async t => { 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 => {