@graphiql/toolkit
Advanced tools
Comparing version 0.2.0 to 0.2.1
# @graphiql/toolkit | ||
## 0.2.1 | ||
### Patch Changes | ||
- [`3f002710`](https://github.com/graphql/graphiql/commit/3f00271089cbc519e221976c9308f60b317cae80) [#1840](https://github.com/graphql/graphiql/pull/1840) Thanks [@enisdenjo](https://github.com/enisdenjo)! - Use provided `wsConnectionParams` | ||
* [`94f16957`](https://github.com/graphql/graphiql/commit/94f169572f643374ead829af690b6dcc2eb0b6a1) [#1841](https://github.com/graphql/graphiql/pull/1841) Thanks [@enisdenjo](https://github.com/enisdenjo)! - Subscriptions async iterator completes and better error handling | ||
## 0.2.0 | ||
@@ -4,0 +12,0 @@ |
@@ -121,3 +121,16 @@ "use strict"; | ||
return push_pull_async_iterable_iterator_1.makeAsyncIterableIteratorFromSink(function (sink) { | ||
return wsClient.subscribe(graphQLParams, sink); | ||
return wsClient.subscribe(graphQLParams, __assign(__assign({}, sink), { error: function (err) { | ||
if (err instanceof Error) { | ||
sink.error(err); | ||
} | ||
else if (err instanceof CloseEvent) { | ||
sink.error(new Error(("Socket closed with event " + err.code + " " + (err.reason || '')).trim())); | ||
} | ||
else { | ||
sink.error(new Error(err.map(function (_a) { | ||
var message = _a.message; | ||
return message; | ||
}).join(', '))); | ||
} | ||
} })); | ||
}); | ||
@@ -204,3 +217,3 @@ }; }; | ||
if (options.subscriptionUrl) { | ||
return exports.createWebsocketsFetcherFromUrl(options.subscriptionUrl); | ||
return exports.createWebsocketsFetcherFromUrl(options.subscriptionUrl, options.wsConnectionParams); | ||
} | ||
@@ -207,0 +220,0 @@ }; |
import { createMultipartFetcher, createSimpleFetcher, isSubscriptionWithName, getWsFetcher, } from './lib'; | ||
export function createGraphiQLFetcher(options) { | ||
let httpFetch; | ||
if (typeof window !== null && window?.fetch) { | ||
if (typeof window !== null && (window === null || window === void 0 ? void 0 : window.fetch)) { | ||
httpFetch = window.fetch; | ||
} | ||
if (options?.enableIncrementalDelivery === null || | ||
if ((options === null || options === void 0 ? void 0 : options.enableIncrementalDelivery) === null || | ||
options.enableIncrementalDelivery !== false) { | ||
@@ -26,3 +26,3 @@ options.enableIncrementalDelivery = true; | ||
} | ||
const isSubscription = isSubscriptionWithName(fetcherOpts?.documentAST, graphQLParams.operationName); | ||
const isSubscription = isSubscriptionWithName(fetcherOpts === null || fetcherOpts === void 0 ? void 0 : fetcherOpts.documentAST, graphQLParams.operationName); | ||
if (isSubscription) { | ||
@@ -29,0 +29,0 @@ if (!wsFetcher) { |
@@ -0,1 +1,29 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
}; | ||
import { visit } from 'graphql'; | ||
@@ -9,3 +37,4 @@ import { meros } from 'meros'; | ||
OperationDefinition(node) { | ||
if (name === node.name?.value) { | ||
var _a; | ||
if (name === ((_a = node.name) === null || _a === void 0 ? void 0 : _a.value)) { | ||
if (node.operation === 'subscription') { | ||
@@ -19,14 +48,10 @@ isSubscription = true; | ||
}; | ||
export const createSimpleFetcher = (options, httpFetch) => async (graphQLParams, fetcherOpts) => { | ||
const data = await httpFetch(options.url, { | ||
export const createSimpleFetcher = (options, httpFetch) => (graphQLParams, fetcherOpts) => __awaiter(void 0, void 0, void 0, function* () { | ||
const data = yield httpFetch(options.url, { | ||
method: 'POST', | ||
body: JSON.stringify(graphQLParams), | ||
headers: { | ||
'content-type': 'application/json', | ||
...options.headers, | ||
...fetcherOpts?.headers, | ||
}, | ||
headers: Object.assign(Object.assign({ 'content-type': 'application/json' }, options.headers), fetcherOpts === null || fetcherOpts === void 0 ? void 0 : fetcherOpts.headers), | ||
}); | ||
return data.json(); | ||
}; | ||
}); | ||
export const createWebsocketsFetcherFromUrl = (url, connectionParams) => { | ||
@@ -45,3 +70,13 @@ let wsClient; | ||
}; | ||
export const createWebsocketsFetcherFromClient = (wsClient) => (graphQLParams) => makeAsyncIterableIteratorFromSink(sink => wsClient.subscribe(graphQLParams, sink)); | ||
export const createWebsocketsFetcherFromClient = (wsClient) => (graphQLParams) => makeAsyncIterableIteratorFromSink(sink => wsClient.subscribe(graphQLParams, Object.assign(Object.assign({}, sink), { error: err => { | ||
if (err instanceof Error) { | ||
sink.error(err); | ||
} | ||
else if (err instanceof CloseEvent) { | ||
sink.error(new Error(`Socket closed with event ${err.code} ${err.reason || ''}`.trim())); | ||
} | ||
else { | ||
sink.error(new Error(err.map(({ message }) => message).join(', '))); | ||
} | ||
} }))); | ||
export const createLegacyWebsocketsFetcher = (legacyWsClient) => (graphQLParams) => { | ||
@@ -51,25 +86,33 @@ const observable = legacyWsClient.request(graphQLParams); | ||
}; | ||
export const createMultipartFetcher = (options, httpFetch) => async function* (graphQLParams, fetcherOpts) { | ||
const response = await httpFetch(options.url, { | ||
method: 'POST', | ||
body: JSON.stringify(graphQLParams), | ||
headers: { | ||
'content-type': 'application/json', | ||
accept: 'application/json, multipart/mixed', | ||
...options.headers, | ||
...fetcherOpts?.headers, | ||
}, | ||
}).then(response => meros(response, { | ||
multiple: true, | ||
})); | ||
if (!isAsyncIterable(response)) { | ||
return yield response.json(); | ||
} | ||
for await (const chunk of response) { | ||
if (chunk.some(part => !part.json)) { | ||
const message = chunk.map(part => `Headers::\n${part.headers}\n\nBody::\n${part.body}`); | ||
throw new Error(`Expected multipart chunks to be of json type. got:\n${message}`); | ||
export const createMultipartFetcher = (options, httpFetch) => function (graphQLParams, fetcherOpts) { | ||
return __asyncGenerator(this, arguments, function* () { | ||
var e_1, _a; | ||
const response = yield __await(httpFetch(options.url, { | ||
method: 'POST', | ||
body: JSON.stringify(graphQLParams), | ||
headers: Object.assign(Object.assign({ 'content-type': 'application/json', accept: 'application/json, multipart/mixed' }, options.headers), fetcherOpts === null || fetcherOpts === void 0 ? void 0 : fetcherOpts.headers), | ||
}).then(response => meros(response, { | ||
multiple: true, | ||
}))); | ||
if (!isAsyncIterable(response)) { | ||
return yield __await(yield yield __await(response.json())); | ||
} | ||
yield chunk.map(part => part.body); | ||
} | ||
try { | ||
for (var response_1 = __asyncValues(response), response_1_1; response_1_1 = yield __await(response_1.next()), !response_1_1.done;) { | ||
const chunk = response_1_1.value; | ||
if (chunk.some(part => !part.json)) { | ||
const message = chunk.map(part => `Headers::\n${part.headers}\n\nBody::\n${part.body}`); | ||
throw new Error(`Expected multipart chunks to be of json type. got:\n${message}`); | ||
} | ||
yield yield __await(chunk.map(part => part.body)); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (response_1_1 && !response_1_1.done && (_a = response_1.return)) yield __await(_a.call(response_1)); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}); | ||
}; | ||
@@ -84,5 +127,5 @@ export const getWsFetcher = (options) => { | ||
if (options.subscriptionUrl) { | ||
return createWebsocketsFetcherFromUrl(options.subscriptionUrl); | ||
return createWebsocketsFetcherFromUrl(options.subscriptionUrl, options.wsConnectionParams); | ||
} | ||
}; | ||
//# sourceMappingURL=lib.js.map |
{ | ||
"name": "@graphiql/toolkit", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Utility to build a fetcher for GraphiQL", | ||
@@ -23,3 +23,3 @@ "contributors": [ | ||
"dependencies": { | ||
"@n1ru4l/push-pull-async-iterable-iterator": "^2.0.1", | ||
"@n1ru4l/push-pull-async-iterable-iterator": "^2.1.4", | ||
"graphql-ws": "^4.3.2", | ||
@@ -29,4 +29,4 @@ "meros": "^1.1.4" | ||
"devDependencies": { | ||
"graphql": "experimental-stream-defer", | ||
"isomorphic-fetch": "^3.0.0", | ||
"graphql": "experimental-stream-defer", | ||
"subscriptions-transport-ws": "^0.9.18" | ||
@@ -33,0 +33,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
import { DocumentNode, visit } from 'graphql'; | ||
import { DocumentNode, visit, GraphQLError } from 'graphql'; | ||
import { meros } from 'meros'; | ||
@@ -97,3 +97,22 @@ import { createClient, Client, ClientOptions } from 'graphql-ws'; | ||
makeAsyncIterableIteratorFromSink<FetcherResult>(sink => | ||
wsClient!.subscribe(graphQLParams, sink), | ||
wsClient!.subscribe(graphQLParams, { | ||
...sink, | ||
error: err => { | ||
if (err instanceof Error) { | ||
sink.error(err); | ||
} else if (err instanceof CloseEvent) { | ||
sink.error( | ||
new Error( | ||
`Socket closed with event ${err.code} ${err.reason || ''}`.trim(), | ||
), | ||
); | ||
} else { | ||
sink.error( | ||
new Error( | ||
(err as GraphQLError[]).map(({ message }) => message).join(', '), | ||
), | ||
); | ||
} | ||
}, | ||
}), | ||
); | ||
@@ -170,4 +189,7 @@ | ||
if (options.subscriptionUrl) { | ||
return createWebsocketsFetcherFromUrl(options.subscriptionUrl); | ||
return createWebsocketsFetcherFromUrl( | ||
options.subscriptionUrl, | ||
options.wsConnectionParams, | ||
); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
399789
1223
0