piral-urql
Advanced tools
Comparing version 0.9.0-pre.746 to 0.9.0-pre.751
import { Extend } from 'piral-core'; | ||
import { PiletGqlApi, UrqlClient } from './types'; | ||
export interface GqlConfig { | ||
/** | ||
* Sets the default request init settings. | ||
*/ | ||
default?: RequestInit; | ||
/** | ||
* Sets the URL of the GraphQL endpoint. | ||
* @default location.origin | ||
*/ | ||
url?: string; | ||
/** | ||
* Sets the URL for the GraphQL subscription endpoint. | ||
*/ | ||
subscriptionUrl?: false | string; | ||
/** | ||
* Sets if the subscription should be lazy initialized. | ||
*/ | ||
lazy?: boolean; | ||
/** | ||
* Optional callback to the be used in case of a connection. | ||
*/ | ||
onConnected?(): void; | ||
/** | ||
* Optional callbsack to be used in case of a disconnect. | ||
* @param err The connection error. | ||
*/ | ||
onDisconnected?(err: Array<Error>): void; | ||
} | ||
/** | ||
* Sets up an urql client by using the given config. | ||
* @param config The configuration for the new urql client. | ||
*/ | ||
export declare function setupGqlClient(config?: GqlConfig): UrqlClient; | ||
/** | ||
* Creates a new Piral GraphQL API extension. | ||
@@ -38,0 +5,0 @@ * @param client The specific urql client to be used, if any. |
@@ -5,41 +5,25 @@ "use strict"; | ||
const urql_1 = require("urql"); | ||
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws"); | ||
const queries_1 = require("./queries"); | ||
/** | ||
* Sets up an urql client by using the given config. | ||
* @param config The configuration for the new urql client. | ||
*/ | ||
function setupGqlClient(config = {}) { | ||
const url = config.url || location.origin; | ||
const subscriptionUrl = (config.subscriptionUrl || url).replace(/^http/i, 'ws'); | ||
const subscriptionClient = config.subscriptionUrl !== false && | ||
new subscriptions_transport_ws_1.SubscriptionClient(subscriptionUrl, { | ||
reconnect: true, | ||
lazy: config.lazy || false, | ||
inactivityTimeout: 0, | ||
connectionCallback(err) { | ||
const { onConnected, onDisconnected } = config; | ||
const errors = err && (Array.isArray(err) ? err : [err]); | ||
if (errors && errors.length > 0) { | ||
typeof onDisconnected === 'function' && onDisconnected(errors); | ||
} | ||
else { | ||
typeof onConnected === 'function' && onConnected(); | ||
} | ||
}, | ||
}); | ||
const forwardSubscription = (operation) => subscriptionClient.request(operation); | ||
const exchanges = [...urql_1.defaultExchanges]; | ||
if (subscriptionClient) { | ||
exchanges.push(urql_1.subscriptionExchange({ | ||
forwardSubscription, | ||
})); | ||
} | ||
return new urql_1.Client({ | ||
url, | ||
fetchOptions: config.default || {}, | ||
exchanges, | ||
const setup_1 = require("./setup"); | ||
function extendOptions(context, options) { | ||
const originalHeaders = options.headers || {}; | ||
const headerPromises = []; | ||
context.emit('before-fetch', { | ||
headers: originalHeaders, | ||
setHeaders(headers) { | ||
if (headers) { | ||
headerPromises.push(headers); | ||
} | ||
}, | ||
}); | ||
return Promise.all(headerPromises).then(newHeaders => { | ||
const headers = newHeaders.reduce((obj, header) => { | ||
if (typeof header === 'object' && header) { | ||
return Object.assign({}, obj, header); | ||
} | ||
return obj; | ||
}, originalHeaders); | ||
return Object.assign({}, options, { headers }); | ||
}); | ||
} | ||
exports.setupGqlClient = setupGqlClient; | ||
/** | ||
@@ -49,14 +33,15 @@ * Creates a new Piral GraphQL API extension. | ||
*/ | ||
function createGqlApi(client = setupGqlClient()) { | ||
function createGqlApi(client = setup_1.setupGqlClient()) { | ||
return context => { | ||
context.includeProvider(React.createElement(urql_1.Provider, { value: client })); | ||
return { | ||
query(q, options) { | ||
return queries_1.gqlQuery(client, q, options); | ||
query(q, o) { | ||
return extendOptions(context, o).then(options => queries_1.gqlQuery(client, q, options)); | ||
}, | ||
mutate(q, options) { | ||
return queries_1.gqlMutation(client, q, options); | ||
mutate(q, o) { | ||
return extendOptions(context, o).then(options => queries_1.gqlMutation(client, q, options)); | ||
}, | ||
subscribe(q, subscriber, options) { | ||
return queries_1.gqlSubscription(client, q, subscriber, options); | ||
subscribe(q, subscriber, o) { | ||
const unsubscribe = extendOptions(context, o).then(options => queries_1.gqlSubscription(client, q, subscriber, options)); | ||
return () => unsubscribe.then(cb => cb()); | ||
}, | ||
@@ -63,0 +48,0 @@ }; |
export * from './create'; | ||
export * from './pilets'; | ||
export * from './queries'; | ||
export * from './setup'; | ||
export * from './types'; |
@@ -7,2 +7,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./queries"), exports); | ||
tslib_1.__exportStar(require("./setup"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -24,5 +24,5 @@ "use strict"; | ||
function gqlQuery(client, q, options = {}) { | ||
const { variables, cache } = options; | ||
const { variables, cache, headers = {} } = options; | ||
const request = urql_1.createRequest(q, variables); | ||
const response = client.executeQuery(request, { requestPolicy: cache }); | ||
const response = client.executeQuery(request, { requestPolicy: cache, fetchOptions: { headers } }); | ||
return pipeToPromise(response); | ||
@@ -38,5 +38,5 @@ } | ||
function gqlMutation(client, q, options = {}) { | ||
const { variables } = options; | ||
const { variables, headers = {} } = options; | ||
const request = urql_1.createRequest(q, variables); | ||
const response = client.executeMutation(request); | ||
const response = client.executeMutation(request, { fetchOptions: { headers } }); | ||
return pipeToPromise(response); | ||
@@ -53,5 +53,5 @@ } | ||
function gqlSubscription(client, q, subscriber, options = {}) { | ||
const { variables } = options; | ||
const { variables, headers = {} } = options; | ||
const request = urql_1.createRequest(q, variables); | ||
const response = client.executeSubscription(request); | ||
const response = client.executeSubscription(request, { fetchOptions: { headers } }); | ||
const [teardown] = wonka_1.pipe(response, wonka_1.subscribe(({ data, error }) => { | ||
@@ -58,0 +58,0 @@ subscriber(data, error); |
@@ -20,4 +20,10 @@ import 'piral-core'; | ||
} | ||
export interface GqlQueryOptions { | ||
export interface GqlOperationOptions { | ||
/** | ||
* Defines the additional headers to use. | ||
*/ | ||
headers?: any; | ||
} | ||
export interface GqlQueryOptions extends GqlOperationOptions { | ||
/** | ||
* The variables to be used in the query. | ||
@@ -31,3 +37,3 @@ */ | ||
} | ||
export interface GqlMutationOptions { | ||
export interface GqlMutationOptions extends GqlOperationOptions { | ||
/** | ||
@@ -38,3 +44,3 @@ * The variables to be used in the query. | ||
} | ||
export interface GqlSubscriptionOptions { | ||
export interface GqlSubscriptionOptions extends GqlOperationOptions { | ||
/** | ||
@@ -41,0 +47,0 @@ * The variables to be used in the subscription. |
{ | ||
"name": "piral-urql", | ||
"version": "0.9.0-pre.746", | ||
"version": "0.9.0-pre.751", | ||
"description": "Extensions for providing a GraphQL client in Piral.", | ||
@@ -43,3 +43,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"piral-core": "^0.9.0-pre.746" | ||
"piral-core": "^0.9.0-pre.751" | ||
}, | ||
@@ -56,3 +56,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "d79bf9b30d1f80473bbd6cb8225cbb1156ecb7d7" | ||
"gitHead": "461d07d5b5d1f7a3d9512b933f065ea100762889" | ||
} |
export * from './create'; | ||
export * from './pilets'; | ||
export * from './queries'; | ||
export * from './setup'; | ||
export * from './types'; |
@@ -27,5 +27,5 @@ import { OperationResult, createRequest, Client } from 'urql'; | ||
export function gqlQuery<TResult = any>(client: Client, q: string, options: GqlQueryOptions = {}) { | ||
const { variables, cache } = options; | ||
const { variables, cache, headers = {} } = options; | ||
const request = createRequest(q, variables); | ||
const response = client.executeQuery(request, { requestPolicy: cache }); | ||
const response = client.executeQuery(request, { requestPolicy: cache, fetchOptions: { headers } }); | ||
return pipeToPromise<TResult>(response); | ||
@@ -41,5 +41,5 @@ } | ||
export function gqlMutation<TResult = any>(client: Client, q: string, options: GqlMutationOptions = {}) { | ||
const { variables } = options; | ||
const { variables, headers = {} } = options; | ||
const request = createRequest(q, variables); | ||
const response = client.executeMutation(request); | ||
const response = client.executeMutation(request, { fetchOptions: { headers } }); | ||
return pipeToPromise<TResult>(response); | ||
@@ -61,5 +61,5 @@ } | ||
) { | ||
const { variables } = options; | ||
const { variables, headers = {} } = options; | ||
const request = createRequest(q, variables); | ||
const response = client.executeSubscription(request); | ||
const response = client.executeSubscription(request, { fetchOptions: { headers } }); | ||
const [teardown] = pipe( | ||
@@ -66,0 +66,0 @@ response, |
@@ -24,4 +24,11 @@ import 'piral-core'; | ||
export interface GqlQueryOptions { | ||
export interface GqlOperationOptions { | ||
/** | ||
* Defines the additional headers to use. | ||
*/ | ||
headers?: any; | ||
} | ||
export interface GqlQueryOptions extends GqlOperationOptions { | ||
/** | ||
* The variables to be used in the query. | ||
@@ -36,3 +43,3 @@ */ | ||
export interface GqlMutationOptions { | ||
export interface GqlMutationOptions extends GqlOperationOptions { | ||
/** | ||
@@ -44,3 +51,3 @@ * The variables to be used in the query. | ||
export interface GqlSubscriptionOptions { | ||
export interface GqlSubscriptionOptions extends GqlOperationOptions { | ||
/** | ||
@@ -47,0 +54,0 @@ * The variables to be used in the subscription. |
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
30011
28
614