Skip to content
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

feat(packages/gqty): support operationName #1276

Merged
merged 12 commits into from
Nov 29, 2022
5 changes: 5 additions & 0 deletions .changeset/cool-wolves-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gqty': minor
---

feat(pacakge/gqty): support GraphQL operation names
Binary file removed internal/gqless.sketch
Binary file not shown.
104 changes: 64 additions & 40 deletions internal/website/docs/client/fetching-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: fetching-data
title: Fetching data
---

#### Prerequisites
### Prerequisites

Make sure you've completed [Getting Started](/docs/getting-started) first.

Expand Down Expand Up @@ -60,7 +60,66 @@ if (possibleData instanceof Promise) {
}
```

## track
## Refetching data

### refetch

A special function that accepts object proxies, or functions.

When dealing with object proxies, it recovers **all** the history of the specific object down the tree of selections,
and refetchs them, returning the same object back after all the resolutions are done.

On the other hand, when used with a **function**, it calls the function (using the core scheduler) ignoring possible nulls in the middle in the first pass,
and returns whatever the function gave back as a promise.

```ts
import { query, refetch, resolved } from '../gqty';

// ...

const data = await resolved(() => {
const user = query.user;

if (user) {
return {
id: user.name,
name: user.name,
};
}
return null;
});

// Later...

// It will refetch 'id' & 'name' of 'query.user'
const refetchedUser = await refetch(query.user);
```

## Advanced Usages

### Operation name

A custom operation name can be added to GraphQL queries, mutations and subscriptions.

This is useful when mocking for unit tests, and advanced server logics.

```ts
import { query, resolved, inlineResolved } from '../gqty';

resolved(() => query.helloWorld, { operationName: 'MyQuery' });

inlineResolved(() => query.helloWorld, { operationName: 'MyQuery' });
```

Both `resolved` and `inlineResolved` above will generate the following query:

```graphql
query MyQuery {
helloWorld
}
```

### track

The function `track` accepts a callback that gets automatically called every time related data arrives or related cache changes, particularly useful for subscriptions, but it also works for queries.

Expand All @@ -80,7 +139,7 @@ setTimeout(() => {
}, 5000);
```

### track options
#### track options

```ts
track(
Expand All @@ -103,42 +162,7 @@ track(
);
```

## Refetching data

### refetch

A special function that accepts object proxies, or functions.

When dealing with object proxies, it recovers **all** the history of the specific object down the tree of selections,
and refetchs them, returning the same object back after all the resolutions are done.

On the other hand, when used with a **function**, it calls the function (using the core scheduler) ignoring possible nulls in the middle in the first pass,
and returns whatever the function gave back as a promise.

```ts
import { query, refetch, resolved } from '../gqty';

// ...

const data = await resolved(() => {
const user = query.user;

if (user) {
return {
id: user.name,
name: user.name,
};
}
return null;
});

// Later...

// It will refetch 'id' & 'name' of 'query.user'
const refetchedUser = await refetch(query.user);
```

## Using the Scheduler directly
### The scheduler

GQty exposes the Scheduler API, which is used by the helper functions above.

Expand All @@ -155,7 +179,7 @@ async function Example() {
}
```

### Error handling
#### Error handling

The scheduler resolves to a promise of a `GQtyError`:

Expand Down
14 changes: 7 additions & 7 deletions packages/gqty/src/Accessor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
decycle,
isInteger,
isObject,
retrocycle,
isObjectWithType,
retrocycle,
} from '../Utils';

const ProxySymbol = Symbol('gqty-proxy');
Expand Down Expand Up @@ -97,9 +97,9 @@ export interface AssignSelections {

export interface AccessorCreators<
GeneratedSchema extends {
query: {};
mutation: {};
subscription: {};
query: object;
mutation: object;
subscription: object;
}
> {
createAccessor: (
Expand All @@ -123,9 +123,9 @@ export interface AccessorCreators<

export function createAccessorCreators<
GeneratedSchema extends {
query: {};
mutation: {};
subscription: {};
query: object;
mutation: object;
subscription: object;
} = never
>(innerState: InnerClientState): AccessorCreators<GeneratedSchema> {
const {
Expand Down
8 changes: 4 additions & 4 deletions packages/gqty/src/Client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { EventHandler } from '../Events';
import { createPrefetch, Prefetch } from '../Helpers/prefetch';
import { createRefetch, Refetch } from '../Helpers/refetch';
import { createSSRHelpers, HydrateCache, PrepareRender } from '../Helpers/ssr';
import { createTracker, Track } from '../Helpers/track';
import { createInterceptorManager, InterceptorManager } from '../Interceptor';
import {
createNormalizationHandler,
Expand All @@ -42,7 +43,6 @@ import {
Resolvers,
RetryOptions,
} from './resolvers';
import { createTracker, Track } from '../Helpers/track';

export interface InnerClientState {
allowCache: boolean;
Expand Down Expand Up @@ -186,9 +186,9 @@ export interface Mutate<

export interface GQtyClient<
GeneratedSchema extends {
query: {};
mutation: {};
subscription: {};
query: object;
mutation: object;
subscription: object;
}
> extends PersistenceHelpers {
query: GeneratedSchema['query'];
Expand Down
Loading