8000 feat(package/gqty): Add `onError` option to `resolve()` by vicary · Pull Request #2230 · gqty-dev/gqty · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(package/gqty): Add onError option to resolve() #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/all-buckets-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gqty': minor
---

Add `onError` option to `resolve()`
2 changes: 1 addition & 1 deletion packages/gqty/src/Cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class Cache {
const pathsSnapshot = Object.freeze([...paths]);

this.#subscriptions.set(pathsSnapshot, fn);
this.#subscribeNormalized(paths, fn);
this.#subscribeNormalized(pathsSnapshot, fn);

return () => {
this.#subscriptions.delete(pathsSnapshot);
Expand Down
11 changes: 8 additions & 3 deletions packages/gqty/src/Client/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export type ResolveOptions = ResolverOptions & {
awaitsFetch?: boolean;

onFetch?: (fetchPromise: Promise<unknown>) => void;

/**
* When specified, query errors are called here instead of being thrown.
*/
onError?: (error: unknown) => void;
};

export type SubscribeOptions = ResolverOptions & {
Expand Down Expand Up @@ -387,15 +392,15 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
targetCaches.add(resolverCache);
}

updateCaches(results, [...targetCaches], {
updateCaches(results, targetCaches, {
skipNotify: promiseDropped() || !context.notifyCacheUpdate,
});

correlatedCaches.delete(resolverCache);

return results;
},
// When neughty users are adding selections every next microtask, we
// When naughty users are adding selections every next microtask, we
// forcibly start the fetch after a number of delays. This number is
// picked arbitrarily, it should be a number that is large enough to
// prevent excessive fetches but small enough to not block the
Expand Down Expand Up @@ -600,7 +605,7 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
// Run once to trigger selections
dataFn();

const fetchPromise = resolve().then(dataFn);
const fetchPromise = resolve().then(dataFn, options?.onError);

if (options?.awaitsFetch ?? true) {
await fetchPromise;
Expand Down
6 changes: 3 additions & 3 deletions packages/gqty/src/Client/updateCaches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { GQtyError } from '../Error';
import type { FetchResult } from './resolveSelections';

export const updateCaches = <TData extends Record<string, unknown>>(
results: FetchResult<TData>[],
caches: Cache[],
results: Iterable<FetchResult<TData>>,
caches: Iterable<Cache>,
cacheSetOptions?: CacheSetOptions
) => {
const errorSet = new Set<GraphQLError>();
Expand Down Expand Up @@ -43,6 +43,6 @@ export const updateCaches = <TData extends Record<string, unknown>>(
}

if (errorSet.size) {
throw GQtyError.fromGraphQLErrors([...errorSet]);
throw GQtyError.fromGraphQLErrors(Array.from(errorSet));
}
};
0