Socket
Socket
Sign inDemoInstall

graphql-http-ws-client

Package Overview
Dependencies
37
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 0.2.0

index.js

21

createGraphQLClient.js

@@ -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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc