Skip to content

Commit 4ec8a36

Browse files
committed
Remove slicknode-client dependency, refactoring
1 parent 84f1b96 commit 4ec8a36

File tree

7 files changed

+391
-227
lines changed

7 files changed

+391
-227
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
"@types/chai": "^4.1.4",
3232
"@types/mocha": "^5.2.5",
3333
"@types/sinon": "^5.0.2",
34-
"apollo-cache-inmemory": "^1.2.10",
35-
"apollo-client": "^2.4.2",
3634
"chai": "^4.1.2",
3735
"coveralls": "^3.0.2",
36+
"graphql-tag": "^2.10.0",
3837
"mocha": "^5.2.0",
3938
"nyc": "^13.0.1",
4039
"rimraf": "^2.6.2",
@@ -61,7 +60,6 @@
6160
},
6261
"dependencies": {
6362
"apollo-link": "^1.2.2",
64-
"graphql": "^14.0.2",
65-
"slicknode-client": "^0.2.2"
63+
"graphql": "^14.0.2"
6664
}
6765
}

src/__tests__/index-test.ts

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,69 @@
1-
import { InMemoryCache } from 'apollo-cache-inmemory';
2-
import { ApolloClient } from 'apollo-client';
1+
import {ApolloLink, execute, FetchResult, GraphQLRequest, Observable} from 'apollo-link';
32
import {expect} from 'chai';
43
import { parse } from 'graphql';
4+
import gql from 'graphql-tag';
55
import sinon from 'sinon';
6-
import Client from 'slicknode-client';
76
import SlicknodeLink from '../index';
87

98
describe('SlicknodeLink', () => {
10-
it('fails for missing configuration options', () => {
11-
expect(() => {
12-
const link = new SlicknodeLink({
13-
client: null,
14-
});
15-
}).to.throw('No instance of a client provided in configuration options for SlicknodeLink');
16-
});
17-
18-
it('creates link successfully', () => {
19-
const client = new Client({
20-
endpoint: 'https://localhost',
21-
});
22-
const link = new SlicknodeLink({
23-
client,
24-
});
25-
expect(link).to.not.equal(null);
26-
});
27-
28-
it('sends and receives requests with apollo client', async () => {
29-
const client = new Client({
30-
endpoint: 'https://localhost',
31-
});
32-
const link = new SlicknodeLink({
33-
client,
34-
});
35-
36-
const payload = {
37-
data: {
38-
value: 'test',
39-
},
9+
it('forwards request to next link', (done) => {
10+
const data = {
11+
test: true,
4012
};
41-
sinon.stub(client, 'fetch').resolves(payload);
42-
const apolloClient = new ApolloClient({
43-
link,
44-
cache: new InMemoryCache(),
45-
});
46-
47-
const result = await apolloClient.query({
48-
query: parse('{value}'),
13+
const slicknodeLink = ApolloLink.from([
14+
new SlicknodeLink(),
15+
new ApolloLink((e) => {
16+
return new Observable<FetchResult>((observer) => {
17+
observer.next({data});
18+
});
19+
}),
20+
]);
21+
const query = gql`{test}`;
22+
const request: GraphQLRequest = {
23+
query,
24+
variables: {},
25+
};
26+
const observable = execute(slicknodeLink, request);
27+
observable.subscribe({
28+
next(result: FetchResult) {
29+
expect(result.data).to.equal(data);
30+
done();
31+
},
32+
error: done,
4933
});
50-
51-
expect(result.data).to.deep.equal(payload.data);
5234
});
5335

54-
it('handles error with apollo client', async () => {
55-
const client = new Client({
56-
endpoint: 'https://localhost',
57-
});
58-
const link = new SlicknodeLink({
59-
client,
36+
it('forwards accessToken from options', (done) => {
37+
const data = {
38+
test: true,
39+
};
40+
const accessToken = 'abc123';
41+
const slicknodeLink = new SlicknodeLink({
42+
accessToken,
6043
});
61-
62-
const errorMessage = 'Error fetching data';
63-
sinon.stub(client, 'fetch').throws(errorMessage);
64-
const apolloClient = new ApolloClient({
65-
link,
66-
cache: new InMemoryCache(),
44+
const link = ApolloLink.from([
45+
slicknodeLink,
46+
new ApolloLink((operation) => {
47+
expect(operation.getContext()).to.deep.equal({
48+
headers: {
49+
Authorization: `Bearer ${accessToken}`,
50+
},
51+
});
52+
done();
53+
return null;
54+
}),
55+
]);
56+
const query = gql`{test}`;
57+
const request: GraphQLRequest = {
58+
query,
59+
variables: {},
60+
};
61+
const observable = execute(link, request);
62+
observable.subscribe({
63+
next(result: FetchResult) {
64+
expect(result.data).to.equal(data);
65+
},
66+
error: done,
6767
});
68-
69-
try {
70-
const result = await apolloClient.query({
71-
query: parse('{value}'),
72-
});
73-
} catch (e) {
74-
expect(e.networkError.name).to.deep.equal(errorMessage);
75-
}
7668
});
7769
});

0 commit comments

Comments
 (0)