@swan-io/graphql-client
Advanced tools
Comparing version 0.1.0-beta4 to 0.1.0-beta5
@@ -12,2 +12,3 @@ import { DocumentNode } from "@0no-co/graphql.web"; | ||
variables: Record<string, unknown>; | ||
withCredentials?: boolean; | ||
}; | ||
@@ -37,5 +38,7 @@ export type MakeRequest = (config: RequestConfig) => Future<Result<unknown, ClientError>>; | ||
}) => Option<ConnectionUpdate<unknown>>; | ||
export type RequestOverrides = Partial<Pick<RequestConfig, "url" | "headers" | "withCredentials">>; | ||
type RequestOptions<Data, Variables> = { | ||
optimize?: boolean; | ||
connectionUpdates?: GetConnectionUpdate<Data, Variables>[] | undefined; | ||
overrides?: RequestOverrides | undefined; | ||
}; | ||
@@ -54,3 +57,3 @@ export declare class Client { | ||
subscribe(func: () => void): () => boolean; | ||
request<Data, Variables>(document: TypedDocumentNode<Data, Variables>, variables: Variables, { optimize, connectionUpdates, }?: RequestOptions<Data, Variables>): Future<Result<Data, ClientError>>; | ||
request<Data, Variables>(document: TypedDocumentNode<Data, Variables>, variables: Variables, { optimize, connectionUpdates, overrides, }?: RequestOptions<Data, Variables>): Future<Result<Data, ClientError>>; | ||
readFromCache<Data, Variables>(document: TypedDocumentNode<Data, Variables>, variables: Variables): Option<Result<unknown, unknown>>; | ||
@@ -57,0 +60,0 @@ query<Data, Variables>(document: TypedDocumentNode<Data, Variables>, variables: Variables, requestOptions?: RequestOptions<Data, Variables>): Future<Result<Data, ClientError>>; |
@@ -1085,2 +1085,3 @@ 'use strict'; | ||
operationName, | ||
withCredentials, | ||
document, | ||
@@ -1094,2 +1095,3 @@ variables | ||
headers, | ||
withCredentials: boxed.Option.fromNullable(withCredentials).getWithDefault(false), | ||
body: JSON.stringify({ | ||
@@ -1153,3 +1155,4 @@ operationName, | ||
optimize = false, | ||
connectionUpdates | ||
connectionUpdates, | ||
overrides | ||
} = {}) { | ||
@@ -1177,3 +1180,2 @@ const transformedDocument = this.getTransformedDocument(document); | ||
url: this.url, | ||
headers: this.headers, | ||
operationName, | ||
@@ -1183,3 +1185,8 @@ document: possiblyOptimizedQuery.getWithDefault( | ||
), | ||
variables: variablesAsRecord | ||
variables: variablesAsRecord, | ||
...overrides, | ||
headers: { | ||
...this.headers, | ||
...overrides != null ? overrides.headers : null | ||
} | ||
}).mapOk((data) => data).tapOk((data) => { | ||
@@ -1254,3 +1261,3 @@ writeOperationToCache( | ||
const runQuery = react.useCallback( | ||
(variables) => { | ||
(variables, { overrides } = {}) => { | ||
setStableVariables( | ||
@@ -1262,3 +1269,3 @@ (stableVariables2) => stableVariables2.match({ | ||
); | ||
return client.request(stableQuery, variables, { optimize }).tap(() => setIsQuerying(false)); | ||
return client.request(stableQuery, variables, { optimize, overrides }).tap(() => setIsQuerying(false)); | ||
}, | ||
@@ -1269,3 +1276,3 @@ [client, optimize, stableQuery] | ||
const exposedRunQuery = react.useCallback( | ||
(variables) => { | ||
(variables, config) => { | ||
if (timeoutRef.current !== void 0) { | ||
@@ -1276,3 +1283,3 @@ clearTimeout(timeoutRef.current); | ||
if (debounce === void 0) { | ||
return runQuery(variables); | ||
return runQuery(variables, config); | ||
} else { | ||
@@ -1282,3 +1289,3 @@ const [future, resolve] = boxed.Deferred.make(); | ||
(variables2) => { | ||
runQuery(variables2).tap(resolve); | ||
runQuery(variables2, config).tap(resolve); | ||
}, | ||
@@ -1309,6 +1316,7 @@ debounce, | ||
const commitMutation = react.useCallback( | ||
(variables) => { | ||
(variables, { overrides } = {}) => { | ||
setData(boxed.AsyncData.Loading()); | ||
return client.commitMutation(stableMutation, variables, { | ||
connectionUpdates: connectionUpdatesRef.current | ||
connectionUpdates: connectionUpdatesRef.current, | ||
overrides | ||
}).tap((result) => setData(boxed.AsyncData.Done(result))); | ||
@@ -1387,6 +1395,7 @@ }, | ||
}; | ||
var useQuery = (query, variables, { suspense = false, optimize = false } = {}) => { | ||
var useQuery = (query, variables, { suspense = false, optimize = false, overrides } = {}) => { | ||
const client = react.useContext(ClientContext); | ||
const [stableQuery] = react.useState(query); | ||
const [stableVariables, setStableVariables] = react.useState([variables, variables]); | ||
const [stableOverrides, setStableOverrides] = react.useState(overrides); | ||
react.useEffect(() => { | ||
@@ -1399,2 +1408,8 @@ const [providedVariables] = stableVariables; | ||
}, [stableVariables, variables]); | ||
react.useEffect(() => { | ||
if (!deepEqual(stableOverrides, overrides)) { | ||
setIsReloading(true); | ||
setStableOverrides(overrides); | ||
} | ||
}, [stableOverrides, overrides]); | ||
const getSnapshot = react.useCallback(() => { | ||
@@ -1417,10 +1432,20 @@ return client.readFromCache(stableQuery, stableVariables[1]); | ||
} | ||
const request = client.query(stableQuery, stableVariables[1], { optimize }).tap(() => setIsReloading(false)); | ||
const request = client.query(stableQuery, stableVariables[1], { | ||
optimize, | ||
overrides: stableOverrides | ||
}).tap(() => setIsReloading(false)); | ||
return () => request.cancel(); | ||
}, [client, suspense, optimize, stableQuery, stableVariables]); | ||
}, [ | ||
client, | ||
suspense, | ||
optimize, | ||
stableOverrides, | ||
stableQuery, | ||
stableVariables | ||
]); | ||
const [isRefreshing, setIsRefreshing] = react.useState(false); | ||
const refresh = react.useCallback(() => { | ||
setIsRefreshing(true); | ||
return client.request(stableQuery, stableVariables[1]).tap(() => setIsRefreshing(false)); | ||
}, [client, stableQuery, stableVariables]); | ||
return client.query(stableQuery, stableVariables[1], { overrides: stableOverrides }).tap(() => setIsRefreshing(false)); | ||
}, [client, stableQuery, stableOverrides, stableVariables]); | ||
const [isReloading, setIsReloading] = react.useState(false); | ||
@@ -1430,4 +1455,4 @@ const reload = react.useCallback(() => { | ||
setStableVariables(([stable]) => [stable, stable]); | ||
return client.request(stableQuery, stableVariables[0]).tap(() => setIsReloading(false)); | ||
}, [client, stableQuery, stableVariables]); | ||
return client.query(stableQuery, stableVariables[0], { overrides: stableOverrides }).tap(() => setIsReloading(false)); | ||
}, [client, stableQuery, stableOverrides, stableVariables]); | ||
const isLoading = isRefreshing || isReloading || asyncData.isLoading(); | ||
@@ -1434,0 +1459,0 @@ const asyncDataToExpose = isReloading ? boxed.AsyncData.Loading() : isLoading ? previousAsyncData : asyncData; |
import { AsyncData, Future, Result } from "@swan-io/boxed"; | ||
import { RequestOverrides } from "../client"; | ||
import { ClientError } from "../errors"; | ||
@@ -8,6 +9,9 @@ import { TypedDocumentNode } from "../types"; | ||
}; | ||
export type DeferredQueryExtraConfig = { | ||
overrides?: RequestOverrides; | ||
}; | ||
export type DeferredQuery<Data, Variables> = readonly [ | ||
AsyncData<Result<Data, ClientError>>, | ||
{ | ||
query: (variables: Variables) => Future<Result<Data, ClientError>>; | ||
query: (variables: Variables, config?: DeferredQueryExtraConfig) => Future<Result<Data, ClientError>>; | ||
reset: () => void; | ||
@@ -14,0 +18,0 @@ } |
import { AsyncData, Future, Result } from "@swan-io/boxed"; | ||
import { GetConnectionUpdate } from "../client"; | ||
import { GetConnectionUpdate, RequestOverrides } from "../client"; | ||
import { ClientError } from "../errors"; | ||
import { TypedDocumentNode } from "../types"; | ||
export type MutationExtraConfig = { | ||
overrides?: RequestOverrides; | ||
}; | ||
export type Mutation<Data, Variables> = readonly [ | ||
(variables: Variables) => Future<Result<Data, ClientError>>, | ||
(variables: Variables, config?: MutationExtraConfig) => Future<Result<Data, ClientError>>, | ||
AsyncData<Result<Data, ClientError>> | ||
@@ -8,0 +11,0 @@ ]; |
import { AsyncData, Future, Result } from "@swan-io/boxed"; | ||
import { RequestOverrides } from "../client"; | ||
import { ClientError } from "../errors"; | ||
@@ -7,2 +8,3 @@ import { TypedDocumentNode } from "../types"; | ||
optimize?: boolean; | ||
overrides?: RequestOverrides; | ||
}; | ||
@@ -18,2 +20,2 @@ export type Query<Data, Variables> = readonly [ | ||
]; | ||
export declare const useQuery: <Data, Variables>(query: TypedDocumentNode<Data, Variables>, variables: Variables, { suspense, optimize }?: QueryConfig) => Query<Data, Variables>; | ||
export declare const useQuery: <Data, Variables>(query: TypedDocumentNode<Data, Variables>, variables: Variables, { suspense, optimize, overrides }?: QueryConfig) => Query<Data, Variables>; |
{ | ||
"name": "@swan-io/graphql-client", | ||
"version": "0.1.0-beta4", | ||
"version": "0.1.0-beta5", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A simple, typesafe GraphQL client for React", |
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
336410
3205