Skip to content

Commit a292b7d

Browse files
toadkickerphortx
authored andcommitted
added apolloClient option for passing a preconfigured client (#93)
* added apolloClient option for passing a preconfigured Apollo client to the plugin. * dont setup link if apolloClient is used * setup client via function * added some guidance on configuring for AppSync * context hug the apolloClient option * added test for setting apolloClient * rebuild project: # dist/vuex-orm-graphql.es5.js # dist/vuex-orm-graphql.es5.js.map # dist/vuex-orm-graphql.umd.js # dist/vuex-orm-graphql.umd.js.map
1 parent 3fc84fe commit a292b7d

12 files changed

+160
-47
lines changed

dist/vuex-orm-graphql.es5.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.es5.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/guide/setup.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ VuexORM.use(VuexORMGraphQL, { database });
2828
## Possible options
2929

3030
These are the possible options to pass when calling `VuexORM.use()`:
31-
31+
- `apolloClient` (optional): Provide a preconfigured instance of the Apollo client. See [client](#client)
3232
- `database` (required): The Vuex-ORM database.
3333
- `debug` (optional, default: `false`): Set to true to activate the debug logging.
3434
- `url` (optional, default: `/graphql`): The URL to the graphql api. Will be passed to apollo-client.
@@ -39,11 +39,59 @@ These are the possible options to pass when calling `VuexORM.use()`:
3939
- `useGETForQueries` (optional, default: `false`) Use GET for queries (not for mutations). See [apollo-link-http](https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#options)
4040
- `adapter` (optional, default: `DefaultAdapter`). See [Adapters](adapters.md)
4141

42-
More options will come in future releases.
43-
4442
::: tip
4543
We recommend to activate the debug mode in development env automatically via:
4644
```javascript
4745
{ debug: process.env.NODE_ENV !== 'production' }
4846
```
4947
:::
48+
49+
## Client
50+
51+
You can inject your own instance of the Apollo Client using `option.apolloClient`. This is useful if
52+
the app requires a more complex configuration, such as integration wiht AWS AppSync. When `apolloClient`
53+
is used, `plugin-graphql` ignores any other options to configure Apollo client.
54+
55+
Here is an example configuration for AWS AppSync:
56+
57+
```
58+
import VuexORM from '@vuex-orm/core'
59+
import AWSAppSyncClient from 'aws-appsync'
60+
import { Auth } from 'aws-amplify'
61+
import VuexORMGraphQL from '@vuex-orm/plugin-graphql'
62+
63+
import database from '../database'
64+
import awsexports from '../aws-exports'
65+
66+
const options = {
67+
defaultOptions: {
68+
watchQuery: {
69+
fetchPolicy: 'cache-and-network'
70+
}
71+
},
72+
connectionQueryMode: 'nodes',
73+
database: database,
74+
url: awsexports.aws_appsync_graphqlEndpoint,
75+
includeExtensions: true,
76+
debug: process.env.NODE_ENV !== 'production'
77+
}
78+
79+
const config = {
80+
url: awsexports.aws_appsync_graphqlEndpoint,
81+
region: awsexports.aws_appsync_region,
82+
auth: {
83+
type: awsexports.aws_appsync_authenticationType,
84+
jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()
85+
}
86+
}
87+
88+
const client = new AWSAppSyncClient(config, options)
89+
90+
options.apolloClient = client
91+
92+
VuexORM.use(VuexORMGraphQL, options)
93+
94+
export const plugins = [
95+
VuexORM.install(database)
96+
]
97+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"commitizen": "^3.0.0",
7777
"coveralls": "^3.0.2",
7878
"cross-env": "^5.2.0",
79+
"cross-fetch": "^3.0.2",
7980
"cz-conventional-changelog": "^2.1.0",
8081
"graphql": "^0.12.3",
8182
"graphql-tag": "^2.6.1",

src/graphql/apollo.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class Apollo {
3131
const context = Context.getInstance();
3232

3333
// This allows the test suite to pass a custom link
34-
if (context.options.link) {
34+
if (!context.options.apolloClient && context.options.link) {
3535
this.httpLink = context.options.link;
3636
} else {
3737
/* istanbul ignore next */
@@ -42,11 +42,17 @@ export default class Apollo {
4242
});
4343
}
4444

45-
this.apolloClient = new ApolloClient({
46-
link: this.httpLink,
47-
cache: new InMemoryCache(),
48-
connectToDevTools: context.debugMode
49-
});
45+
if (context.options.apolloClient) {
46+
this.apolloClient = (context => {
47+
return context.options.apolloClient;
48+
})(context);
49+
} else {
50+
this.apolloClient = new ApolloClient({
51+
link: this.httpLink,
52+
cache: new InMemoryCache(),
53+
connectToDevTools: context.debugMode
54+
});
55+
}
5056
}
5157

5258
/**

src/support/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Adapter from "../adapters/adapter";
99
export type DispatchFunction = (action: string, data: Data) => Promise<any>;
1010

1111
export interface Options {
12+
apolloClient: any;
1213
database: Database;
1314
url?: string;
1415
headers?: { [index: string]: any };

test/integration/actions/fetch.spec.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,42 +124,42 @@ query User($id: ID!) {
124124
});
125125

126126
describe("without ID but with filter with array", () => {
127-
test("sends the correct query to the API", async () => {
127+
test("sends the correct query to the API", async () => {
128+
// @ts-ignore
129+
await User.fetch(1);
130+
131+
const insertedData = await Post.insert({
132+
data: {
133+
title: "It works!",
134+
content: "This is a test!",
135+
published: false,
136+
otherId: 15,
137+
author: User.find(1)
138+
}
139+
});
140+
141+
const post = insertedData.posts[0];
142+
143+
const request = await recordGraphQLRequest(async () => {
128144
// @ts-ignore
129-
await User.fetch(1);
145+
await User.fetch({ profileId: 2, posts: [post] });
146+
});
130147

131-
const insertedData = await Post.insert({
132-
data: {
133-
title: "It works!",
148+
expect(request!.variables).toEqual({
149+
profileId: 2,
150+
posts: [
151+
{
152+
id: 1,
153+
authorId: 1,
134154
content: "This is a test!",
135-
published: false,
136155
otherId: 15,
137-
author: User.find(1)
156+
published: false,
157+
title: "It works!"
138158
}
139-
});
140-
141-
const post = insertedData.posts[0];
142-
143-
const request = await recordGraphQLRequest(async () => {
144-
// @ts-ignore
145-
await User.fetch({ profileId: 2, posts: [post] });
146-
});
147-
148-
expect(request!.variables).toEqual({
149-
profileId: 2,
150-
posts: [
151-
{
152-
id: 1,
153-
authorId: 1,
154-
content: "This is a test!",
155-
otherId: 15,
156-
published: false,
157-
title: "It works!"
158-
}
159-
]
160-
});
161-
expect(request!.query).toEqual(
162-
`
159+
]
160+
});
161+
expect(request!.query).toEqual(
162+
`
163163
query Users($profileId: ID!, $posts: [PostFilter]!) {
164164
users(filter: {profileId: $profileId, posts: $posts}) {
165165
nodes {
@@ -175,9 +175,9 @@ query Users($profileId: ID!, $posts: [PostFilter]!) {
175175
}
176176
}
177177
`.trim() + "\n"
178-
);
179-
});
178+
);
180179
});
180+
});
181181

182182
describe("without ID but with filter with object", () => {
183183
test("sends the correct query to the API", async () => {

test/support/mock-apollo-client.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as fetch from "cross-fetch";
2+
import { ApolloClient, FetchPolicy } from "apollo-client";
3+
import { InMemoryCache } from "apollo-cache-inmemory";
4+
import { HttpLink } from "apollo-link-http";
5+
import { ApolloLink } from "apollo-link";
6+
7+
export default class MockApolloClient {
8+
/**
9+
* The http link instance to use.
10+
* @type {HttpLink}
11+
*/
12+
private readonly httpLink: ApolloLink;
13+
/**
14+
* The ApolloClient instance
15+
* @type {ApolloClient}
16+
*/
17+
private readonly apolloClient: ApolloClient<any>;
18+
/**
19+
* @constructor
20+
*/
21+
public constructor() {
22+
/* istanbul ignore next */
23+
this.httpLink = new HttpLink({
24+
...fetch,
25+
uri: "/graphql",
26+
credentials: "same-origin",
27+
useGETForQueries: false
28+
});
29+
30+
this.apolloClient = new ApolloClient({
31+
link: this.httpLink,
32+
cache: new InMemoryCache(),
33+
connectToDevTools: true
34+
});
35+
}
36+
}

test/unit/context.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { setupMockData } from "../support/mock-data";
22
import Context from "../../src/common/context";
3+
import MockApolloClient from "../support/mock-apollo-client";
34

45
let store;
56
let vuexOrmGraphQL;
@@ -17,6 +18,13 @@ describe("Context", () => {
1718
});
1819
});
1920

21+
describe(".apolloClient", () => {
22+
test("returns an apollo client", () => {
23+
context.options.apolloClient = new MockApolloClient();
24+
expect(context.options.apolloClient instanceof MockApolloClient).toBe(true);
25+
});
26+
});
27+
2028
describe(".getModel", () => {
2129
test("returns a model", () => {
2230
expect(context.getModel("post")).toEqual(context.models.get("post"));

yarn.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,14 @@ cross-env@^5.2.0:
33153315
cross-spawn "^6.0.5"
33163316
is-windows "^1.0.0"
33173317

3318+
cross-fetch@^3.0.2:
3319+
version "3.0.2"
3320+
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.2.tgz#b7136491967031949c7f86b15903aef4fa3f1768"
3321+
integrity sha512-a4Z0EJ5Nck6QtMy9ZqloLfpvu2uMV3sBfMCR+CgSBCZc6z5KR4bfEiD3dkepH8iZgJMXQpTqf8FjMmvu/GMFkg==
3322+
dependencies:
3323+
node-fetch "2.3.0"
3324+
whatwg-fetch "3.0.0"
3325+
33183326
cross-spawn@^5.0.1:
33193327
version "5.1.0"
33203328
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -7340,7 +7348,7 @@ node-fetch-npm@^2.0.2:
73407348
json-parse-better-errors "^1.0.0"
73417349
safe-buffer "^5.1.1"
73427350

7343-
node-fetch@^2.1.1, node-fetch@^2.3.0:
7351+
node-fetch@2.3.0, node-fetch@^2.1.1, node-fetch@^2.3.0:
73447352
version "2.3.0"
73457353
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
73467354

@@ -11138,6 +11146,11 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
1113811146
dependencies:
1113911147
iconv-lite "0.4.24"
1114011148

11149+
whatwg-fetch@3.0.0:
11150+
version "3.0.0"
11151+
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
11152+
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
11153+
1114111154
whatwg-mimetype@^2.1.0:
1114211155
version "2.2.0"
1114311156
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.2.0.tgz#a3d58ef10b76009b042d03e25591ece89b88d171"

0 commit comments

Comments
 (0)