graphql-apollo-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ {
2
+ "responsive-preview": {
3
+ "Mobile": [
4
+ 320,
5
+ 675
6
+ ],
7
+ "Tablet": [
8
+ 1024,
9
+ 765
10
+ ],
11
+ "Desktop": [
12
+ 1400,
13
+ 800
14
+ ],
15
+ "Desktop HD": [
16
+ 1920,
17
+ 1080
18
+ ]
19
+ }
20
+ }
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # GraphqQL WebSocket Client
2
+
3
+
4
+ ### To Install dependencies
5
+ - `npm install` or `yarn`
6
+
7
+ ### To run this example:
8
+
9
+ - `npm run dev` or `yarn dev`
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
+ }