graphql-http-ws-client
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -1,10 +0,10 @@ | ||
import { ApolloClient } from "apollo-client"; | ||
import { getMainDefinition } from "apollo-utilities"; | ||
import { InMemoryCache } from "apollo-cache-inmemory"; | ||
import { ApolloClient } from "@apollo/client/core"; | ||
import { InMemoryCache } from "@apollo/client/cache"; | ||
import createGraphQLLinks from "./createGraphQLLinks"; | ||
export default (graphQLURL, passedOptions = {}) => { | ||
const options = Object.assign({ | ||
resolvers: {} | ||
}, passedOptions); | ||
const options = { | ||
resolvers: {}, | ||
...passedOptions | ||
}; | ||
@@ -24,6 +24,9 @@ const links = createGraphQLLinks(graphQLURL, options); | ||
return Object.assign(links, { | ||
return { | ||
...links, | ||
client: client, | ||
cache: cache | ||
}); | ||
} | ||
}; | ||
}; | ||
@@ -1,7 +0,7 @@ | ||
import {concat, split} from "apollo-link"; | ||
import {getMainDefinition} from "apollo-utilities"; | ||
import { HttpLink } from "apollo-link-http"; | ||
import { WebSocketLink } from "apollo-link-ws"; | ||
import { concat, split } from '@apollo/client/core'; | ||
import { getMainDefinition } from '@apollo/client/utilities'; | ||
import { RetryLink } from "@apollo/client/link/retry"; | ||
import { HttpLink } from "@apollo/client/link/http"; | ||
import { WebSocketLink } from "@apollo/client/link/ws"; | ||
import { SubscriptionClient } from "subscriptions-transport-ws"; | ||
import { RetryLink } from "apollo-link-retry"; | ||
@@ -11,6 +11,7 @@ // TODO allow override of fetch options as in https://www.apollographql.com/docs/link/links/http/ | ||
export default (graphQLURL, passedOptions) => { | ||
const options = Object.assign({ | ||
const options = { | ||
createHTTPLink: true, | ||
createWebsocketLink: true | ||
}, passedOptions); | ||
createWebsocketLink: true, | ||
...passedOptions | ||
}; | ||
@@ -23,28 +24,33 @@ const httpURLToWS = (url) => { | ||
if(options.createHTTPLink) { | ||
if(typeof options.fetch !== 'function' && (typeof window !== 'object' || typeof window.fetch !== 'function')) | ||
throw new Error(`Missing fetch implementation on window.fetch or options.fetch`); | ||
const fetchImplementation = (typeof options.fetch === 'function') ? options.fetch : window.fetch; | ||
httpLink = new HttpLink({ uri: graphQLURL, fetch: fetchImplementation }); | ||
const httpLinkOptions = { | ||
uri: graphQLURL, | ||
...options.httpLinkOptions | ||
}; | ||
if(typeof httpLinkOptions.fetch !== 'function' && (typeof window !== 'object' || typeof window.fetch !== 'function')) | ||
throw new Error(`Missing fetch implementation on window.fetch or options.httpLinkOptions.fetch`); | ||
httpLink = new HttpLink(httpLinkOptions); | ||
} | ||
let websocketLink = null; | ||
let wsLink = null; | ||
let subscriptionClient = null; | ||
if(options.createWebsocketLink) { | ||
if(typeof options.websocket !== 'function' && (typeof window !== 'object' || typeof window.WebSocket !== 'function')) | ||
if(options.createWSLink) { | ||
const wsLinkOptions = { | ||
reconnect: true, | ||
...options.wsLinkOptions | ||
}; | ||
if(typeof options.ws !== 'function' && (typeof window !== 'object' || typeof window.WebSocket !== 'function')) | ||
throw new Error(`Missing websocket implementation on window.WebSocket or options.websocket`); | ||
const websocketImplementation = (typeof options.websocket === 'function') ? options.websocket : window.WebSocket; | ||
const websocketImplementation = (typeof options.ws === 'function') ? options.ws : window.WebSocket; | ||
subscriptionClient = new SubscriptionClient(httpURLToWS(graphQLURL), Object.assign( | ||
{ | ||
reconnect: true | ||
}, | ||
filterObject(options, ['reconnect', 'timeout', 'lazy', 'reconnect', 'reconnectionAttempts', 'connectionCallback', 'inactivityTimeout', 'connectionParams']) | ||
), websocketImplementation); | ||
subscriptionClient = new SubscriptionClient(httpURLToWS(graphQLURL), wsLinkOptions, websocketImplementation); | ||
websocketLink = new WebSocketLink(subscriptionClient); | ||
wsLink = new WebSocketLink(subscriptionClient); | ||
} | ||
let transportLink = null; | ||
if(httpLink !== null && websocketLink !== null) { // httpLink and websocketLink exist | ||
if(httpLink !== null && wsLink !== null) { // httpLink and websocketLink exist | ||
transportLink = split(({ query }) => { | ||
@@ -54,7 +60,7 @@ const { kind, operation } = getMainDefinition(query); | ||
}, | ||
websocketLink, | ||
wsLink, | ||
httpLink); | ||
} | ||
else if(websocketLink !== null) // only websocketLink exists | ||
transportLink = websocketLink; | ||
else if(wsLink !== null) // only websocketLink exists | ||
transportLink = wsLink; | ||
else if(httpLink !== null) // only httpLink (no Subscriptions) | ||
@@ -74,3 +80,3 @@ transportLink = httpLink; | ||
httpLink: httpLink, | ||
websocketLink: websocketLink, | ||
wsLink: wsLink, | ||
transportLink: transportLink, | ||
@@ -80,11 +86,2 @@ retryLink: retryLink, | ||
}; | ||
function filterObject(rawObject, filterKeys) { | ||
return Object.keys(rawObject) | ||
.filter(key => filterKeys.includes(key)) | ||
.reduce((filteredObject, key) => { | ||
filteredObject[key] = rawObject[key]; | ||
return filteredObject; | ||
}, {}); | ||
} | ||
} |
{ | ||
"name": "graphql-http-ws-client", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"private": false, | ||
@@ -14,15 +14,8 @@ "author": { | ||
"dependencies": { | ||
"apollo-cache-inmemory": "^1.6.3", | ||
"apollo-client": "^2.6.4", | ||
"apollo-link-error": "^1.1.12", | ||
"apollo-link-http": "^1.5.16", | ||
"apollo-link-retry": "^2.2.15", | ||
"apollo-link-ws": "^1.0.19", | ||
"apollo-utilities": "^1.3.2", | ||
"esm": "^3.2.25", | ||
"graphql": "^14.5.8", | ||
"graphql-tag": "^2.10.1", | ||
"subscriptions-transport-ws": "^0.9.16" | ||
"@apollo/client": "^3.1.3", | ||
"graphql": "15.3.0", | ||
"subscriptions-transport-ws": "^0.9.17" | ||
}, | ||
"devDependencies": { | ||
"esm": "^3.2.25", | ||
"apollo-cache-persist": "^0.1.1", | ||
@@ -32,4 +25,4 @@ "node-fetch": "^2.6.0", | ||
}, | ||
"main": "createGraphQLClient.js", | ||
"module": "createGraphQLClient.js" | ||
"main": "./index.js", | ||
"module": "./index.js" | ||
} |
@@ -5,4 +5,3 @@ ## GraphQL client over HTTP/WS | ||
import createGraphQLClient from "graphql-http-ws-client"; | ||
import gql from "graphql-tag"; | ||
import {createGraphQLClient, gql} from "graphql-http-ws-client"; | ||
import WebSocket from "ws"; | ||
@@ -13,3 +12,5 @@ import fetch from "node-fetch"; | ||
websocket: WebSocket, | ||
fetch: fetch | ||
httpLinkOptions: { | ||
fetch: fetch | ||
} | ||
}); | ||
@@ -19,9 +20,10 @@ | ||
import createGraphQLClient from "graphql-http-ws-client"; | ||
import gql from "graphql-tag"; | ||
import {createGraphQLClient, gql} from "graphql-http-ws-client"; | ||
import fetch from "node-fetch"; | ||
const { client } = createGraphQLClient("MY_GRAPHQL_URL", { | ||
fetch: fetch, | ||
createWebsocketLink: false | ||
httpLinkOptions: { | ||
fetch: fetch | ||
}, | ||
createWSLink: false | ||
}); | ||
@@ -31,4 +33,3 @@ | ||
import createGraphQLClient from "graphql-http-ws-client"; | ||
import gql from "graphql-tag"; | ||
import {createGraphQLClient, gql} from "graphql-http-ws-client"; | ||
import WebSocket from "ws"; | ||
@@ -46,6 +47,4 @@ import fetch from "node-fetch"; | ||
import fetch from "node-fetch"; | ||
import createGraphQLClient from "graphql-http-ws-client"; | ||
import gql from "graphql-tag"; | ||
import {createGraphQLClient, gql} from "graphql-http-ws-client"; | ||
import { persistCache } from "apollo-cache-persist"; | ||
const { client, cache } = createGraphQLClient("MY_GRAPHQL_URL"); | ||
@@ -68,2 +67,37 @@ | ||
}); | ||
### Simple Queries | ||
Using the [server example from graphql-http-ws-server](https://github.com/johnvmt/graphql-http-ws-server#readme) | ||
client.query({ | ||
query: gql(`query { | ||
hello | ||
}`) | ||
}).then(({data}) => { | ||
console.log("DATA", data); | ||
}); | ||
### Simple Subscriptions | ||
Using the [server example from graphql-http-ws-server](https://github.com/johnvmt/graphql-http-ws-server#readme) | ||
client.subscribe({ | ||
query: gql(`subscription { | ||
time | ||
}`) | ||
}).subscribe({ | ||
next({data}) { | ||
console.log(data); | ||
} | ||
}); | ||
### Changes | ||
#### v0.2.0 | ||
- Module now exports gql and all exports from @apollo/client/core | ||
- Renamed createWebsocketLink to createWSLink and websocket option to ws for consistency with options | ||
- New httpLinkOptions and wsLinkOptions parameters | ||
- fetch option moves to httpLinkOptions option | ||
- all ws link options move to wsLinkOptions option |
6640
3
5
94
97
4
+ Added@apollo/client@^3.1.3
+ Added@apollo/client@3.11.8(transitive)
+ Added@graphql-typed-document-node/core@3.2.0(transitive)
+ Added@wry/caches@1.0.1(transitive)
+ Added@wry/context@0.7.4(transitive)
+ Added@wry/equality@0.5.7(transitive)
+ Added@wry/trie@0.4.30.5.0(transitive)
+ Addedgraphql@15.3.0(transitive)
+ Addedhoist-non-react-statics@3.3.2(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedoptimism@0.18.0(transitive)
+ Addedprop-types@15.8.1(transitive)
+ Addedreact-is@16.13.1(transitive)
+ Addedrehackt@0.1.0(transitive)
+ Addedresponse-iterator@0.2.6(transitive)
+ Addedsymbol-observable@4.0.0(transitive)
+ Addedts-invariant@0.10.3(transitive)
+ Addedzen-observable-ts@1.2.5(transitive)
- Removedapollo-cache-inmemory@^1.6.3
- Removedapollo-client@^2.6.4
- Removedapollo-link-error@^1.1.12
- Removedapollo-link-http@^1.5.16
- Removedapollo-link-retry@^2.2.15
- Removedapollo-link-ws@^1.0.19
- Removedapollo-utilities@^1.3.2
- Removedesm@^3.2.25
- Removedgraphql-tag@^2.10.1
- Removed@types/node@22.7.4(transitive)
- Removed@types/zen-observable@0.8.00.8.7(transitive)
- Removed@wry/context@0.4.4(transitive)
- Removed@wry/equality@0.1.11(transitive)
- Removedapollo-cache@1.3.5(transitive)
- Removedapollo-cache-inmemory@1.6.6(transitive)
- Removedapollo-client@2.6.10(transitive)
- Removedapollo-link@1.2.14(transitive)
- Removedapollo-link-error@1.1.13(transitive)
- Removedapollo-link-http@1.5.17(transitive)
- Removedapollo-link-http-common@0.2.16(transitive)
- Removedapollo-link-retry@2.2.16(transitive)
- Removedapollo-link-ws@1.0.20(transitive)
- Removedapollo-utilities@1.3.4(transitive)
- Removedesm@3.2.25(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedgraphql@14.7.0(transitive)
- Removedoptimism@0.10.3(transitive)
- Removedts-invariant@0.4.4(transitive)
- Removedtslib@1.14.1(transitive)
- Removedundici-types@6.19.8(transitive)
- Removedzen-observable-ts@0.8.21(transitive)
Updatedgraphql@15.3.0