feat(core): add useQueryImmutable (#5299)
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import { getIsOwnerQuery } from '@affine/graphql';
|
import { getIsOwnerQuery } from '@affine/graphql';
|
||||||
import { useQuery } from '@affine/workspace/affine/gql';
|
import { useQueryImmutable } from '@affine/workspace/affine/gql';
|
||||||
|
|
||||||
export function useIsWorkspaceOwner(workspaceId: string) {
|
export function useIsWorkspaceOwner(workspaceId: string) {
|
||||||
const { data } = useQuery({
|
const { data } = useQueryImmutable({
|
||||||
query: getIsOwnerQuery,
|
query: getIsOwnerQuery,
|
||||||
variables: {
|
variables: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { serverConfigQuery } from '@affine/graphql';
|
import { serverConfigQuery } from '@affine/graphql';
|
||||||
import { useQuery } from '@affine/workspace/affine/gql';
|
import { useQueryImmutable } from '@affine/workspace/affine/gql';
|
||||||
import type { BareFetcher, Middleware } from 'swr';
|
import type { BareFetcher, Middleware } from 'swr';
|
||||||
|
|
||||||
const wrappedFetcher = (fetcher: BareFetcher<any> | null, ...args: any[]) =>
|
const wrappedFetcher = (fetcher: BareFetcher<any> | null, ...args: any[]) =>
|
||||||
@@ -10,13 +10,10 @@ const errorHandler: Middleware = useSWRNext => (key, fetcher, config) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useServerFlavor = () => {
|
export const useServerFlavor = () => {
|
||||||
const { data: config, error } = useQuery(
|
const { data: config, error } = useQueryImmutable(
|
||||||
{ query: serverConfigQuery },
|
{ query: serverConfigQuery },
|
||||||
{
|
{
|
||||||
use: [errorHandler],
|
use: [errorHandler],
|
||||||
revalidateOnFocus: false,
|
|
||||||
revalidateOnMount: false,
|
|
||||||
revalidateIfStale: false,
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import type { GraphQLError } from 'graphql';
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import type { Key, SWRConfiguration, SWRResponse } from 'swr';
|
import type { Key, SWRConfiguration, SWRResponse } from 'swr';
|
||||||
import useSWR, { useSWRConfig } from 'swr';
|
import useSWR, { useSWRConfig } from 'swr';
|
||||||
|
import useSWRImutable from 'swr/immutable';
|
||||||
import useSWRInfinite from 'swr/infinite';
|
import useSWRInfinite from 'swr/infinite';
|
||||||
import type {
|
import type {
|
||||||
SWRMutationConfiguration,
|
SWRMutationConfiguration,
|
||||||
@@ -44,7 +45,7 @@ export const fetcher = gqlFetcherFactory(
|
|||||||
* })
|
* })
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function useQuery<Query extends GraphQLQuery>(
|
type useQueryFn = <Query extends GraphQLQuery>(
|
||||||
options: QueryOptions<Query>,
|
options: QueryOptions<Query>,
|
||||||
config?: Omit<
|
config?: Omit<
|
||||||
SWRConfiguration<
|
SWRConfiguration<
|
||||||
@@ -54,31 +55,35 @@ export function useQuery<Query extends GraphQLQuery>(
|
|||||||
>,
|
>,
|
||||||
'fetcher'
|
'fetcher'
|
||||||
>
|
>
|
||||||
): SWRResponse<
|
) => SWRResponse<
|
||||||
QueryResponse<Query>,
|
QueryResponse<Query>,
|
||||||
GraphQLError | GraphQLError[],
|
GraphQLError | GraphQLError[],
|
||||||
{
|
{
|
||||||
suspense: true;
|
suspense: true;
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
export function useQuery<Query extends GraphQLQuery>(
|
|
||||||
options: QueryOptions<Query>,
|
|
||||||
config?: any
|
|
||||||
) {
|
|
||||||
const configWithSuspense: SWRConfiguration = useMemo(
|
|
||||||
() => ({
|
|
||||||
suspense: true,
|
|
||||||
...config,
|
|
||||||
}),
|
|
||||||
[config]
|
|
||||||
);
|
|
||||||
|
|
||||||
return useSWR(
|
const createUseQuery =
|
||||||
() => ['cloud', options.query.id, options.variables],
|
(immutable: boolean): useQueryFn =>
|
||||||
() => fetcher(options),
|
(options, config) => {
|
||||||
configWithSuspense
|
const configWithSuspense: SWRConfiguration = useMemo(
|
||||||
);
|
() => ({
|
||||||
}
|
suspense: true,
|
||||||
|
...config,
|
||||||
|
}),
|
||||||
|
[config]
|
||||||
|
);
|
||||||
|
|
||||||
|
const useSWRFn = immutable ? useSWRImutable : useSWR;
|
||||||
|
return useSWRFn(
|
||||||
|
options ? () => ['cloud', options.query.id, options.variables] : null,
|
||||||
|
options ? () => fetcher(options) : null,
|
||||||
|
configWithSuspense
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useQuery = createUseQuery(false);
|
||||||
|
export const useQueryImmutable = createUseQuery(true);
|
||||||
|
|
||||||
export function useQueryInfinite<Query extends GraphQLQuery>(
|
export function useQueryInfinite<Query extends GraphQLQuery>(
|
||||||
options: Omit<QueryOptions<Query>, 'variables'> & {
|
options: Omit<QueryOptions<Query>, 'variables'> & {
|
||||||
|
|||||||
Reference in New Issue
Block a user