graphql-apollo-client 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.codesandbox/workspace.json +20 -0
- package/README.md +9 -0
- package/index.js +98 -0
- package/package.json +28 -0
package/README.md
ADDED
package/index.js
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
const { InMemoryCache } = require("apollo-cache-inmemory");
|
2
|
+
const ApolloClient = require("apollo-client").default;
|
3
|
+
const { WebSocketLink } = require("apollo-link-ws");
|
4
|
+
const gql = require("graphql-tag");
|
5
|
+
const WebSocket = require("ws");
|
6
|
+
const ApolloLink = require("apollo-link");
|
7
|
+
const { createHttpLink } = require("apollo-link-http");
|
8
|
+
const { getMainDefinition } = require("apollo-utilities");
|
9
|
+
const fetch = (...args) =>
|
10
|
+
import("node-fetch").then(({ default: fetch }) => fetch(...args));
|
11
|
+
|
12
|
+
/**
|
13
|
+
*
|
14
|
+
* @param {{ httpEndpoint: string, wsEndpoint: string, bearerToken: string }} params
|
15
|
+
* @returns { ApolloClient }
|
16
|
+
*/
|
17
|
+
|
18
|
+
const configureClient = ({ httpEndpoint, wsEndpoint, bearerToken }) => {
|
19
|
+
const wsLink = new WebSocketLink({
|
20
|
+
uri: wsEndpoint,
|
21
|
+
options: {
|
22
|
+
reconnect: true,
|
23
|
+
connectionParams: async () => {
|
24
|
+
return {
|
25
|
+
Authorization: bearerToken
|
26
|
+
};
|
27
|
+
}
|
28
|
+
},
|
29
|
+
webSocketImpl: WebSocket
|
30
|
+
});
|
31
|
+
|
32
|
+
const httpLink = createHttpLink({
|
33
|
+
uri: httpEndpoint,
|
34
|
+
fetch: fetch,
|
35
|
+
headers: {
|
36
|
+
Authorization: bearerToken
|
37
|
+
}
|
38
|
+
});
|
39
|
+
|
40
|
+
const splitLink = ApolloLink.split(
|
41
|
+
({ query }) => {
|
42
|
+
const definition = getMainDefinition(query);
|
43
|
+
|
44
|
+
return (
|
45
|
+
definition.kind === "OperationDefinition" &&
|
46
|
+
definition.operation === "subscription"
|
47
|
+
);
|
48
|
+
},
|
49
|
+
wsLink,
|
50
|
+
httpLink
|
51
|
+
);
|
52
|
+
const apolloClient = new ApolloClient({
|
53
|
+
cache: new InMemoryCache(),
|
54
|
+
link: splitLink
|
55
|
+
});
|
56
|
+
|
57
|
+
return apolloClient
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
/**
|
63
|
+
* Example
|
64
|
+
*/
|
65
|
+
|
66
|
+
const GRAPHQL_ENDPOINT = "https://api.ecomon.no";
|
67
|
+
const GRAPHQL_ENDPOINT_WS = "wss://api.ecomon.no";
|
68
|
+
const TOKEN = "Bearer intermediary-api-key_858bef2f-5b67-49b2-b7e0-c07a093f98be";
|
69
|
+
|
70
|
+
const client = configureClient({
|
71
|
+
httpEndpoint: GRAPHQL_ENDPOINT,
|
72
|
+
wsEndpoint: GRAPHQL_ENDPOINT_WS,
|
73
|
+
bearerToken: TOKEN
|
74
|
+
})
|
75
|
+
|
76
|
+
// Test subscription
|
77
|
+
client
|
78
|
+
.subscribe({
|
79
|
+
query: gql`
|
80
|
+
subscription {
|
81
|
+
realtimeMeasurement(deviceId: "pM8SEyRQ") {
|
82
|
+
activePowerImport
|
83
|
+
time
|
84
|
+
}
|
85
|
+
}
|
86
|
+
`,
|
87
|
+
variables: {}
|
88
|
+
})
|
89
|
+
.subscribe({
|
90
|
+
next(data) {
|
91
|
+
console.log(data);
|
92
|
+
}
|
93
|
+
});
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
module.exports = configureClient
|
package/package.json
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"name": "graphql-apollo-client",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"main": "index.js",
|
5
|
+
"license": "MIT",
|
6
|
+
"dependencies": {
|
7
|
+
"apollo-cache-inmemory": "1.6.6",
|
8
|
+
"apollo-client": "2.6.10",
|
9
|
+
"apollo-link": "1.2.14",
|
10
|
+
"apollo-link-error": "1.1.13",
|
11
|
+
"apollo-link-http": "1.5.17",
|
12
|
+
"apollo-link-ws": "1.0.20",
|
13
|
+
"apollo-utilities": "1.3.4",
|
14
|
+
"graphql": "16.6.0",
|
15
|
+
"graphql-tag": "2.12.6",
|
16
|
+
"graphql-ws": "5.10.1",
|
17
|
+
"node-fetch": "3.2.10",
|
18
|
+
"nodemon": "2.0.19",
|
19
|
+
"subscriptions-transport-ws": "0.11.0",
|
20
|
+
"ws": "8.8.1"
|
21
|
+
},
|
22
|
+
"scripts": {
|
23
|
+
"start": "node index.js",
|
24
|
+
"dev": "nodemon index.js"
|
25
|
+
},
|
26
|
+
"keywords": [],
|
27
|
+
"description": ""
|
28
|
+
}
|