52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { ApolloError, ApolloQueryResult, gql, OperationVariables, useQuery } from '@apollo/client';
|
|
import { Identity, IdentityRootQuery } from '@generated/types';
|
|
import { createContext, useContext } from 'react';
|
|
|
|
export interface IdentityContext {
|
|
data: Identity;
|
|
refetch: () => void;
|
|
}
|
|
|
|
export const IdentityContext = createContext<IdentityContext | null>(null);
|
|
export const IdentityProvider = IdentityContext.Provider;
|
|
|
|
export function useIdentity(): IdentityContext | null {
|
|
const identityContext = useContext(IdentityContext);
|
|
if (identityContext == null) return null;
|
|
return identityContext;
|
|
}
|
|
|
|
export function useIdentityQuery({ skip }: { skip?: boolean }): {
|
|
data: IdentityRootQuery | undefined;
|
|
loading: boolean;
|
|
error: ApolloError | undefined;
|
|
refetch: (
|
|
variables?: Partial<OperationVariables> | undefined
|
|
) => Promise<ApolloQueryResult<IdentityRootQuery>>;
|
|
} {
|
|
const { data, loading, error, refetch } = useQuery<IdentityRootQuery>(query, {
|
|
skip
|
|
});
|
|
return {
|
|
data,
|
|
loading,
|
|
error,
|
|
refetch
|
|
};
|
|
}
|
|
|
|
const query = gql`
|
|
query IdentityRoot {
|
|
identity {
|
|
id
|
|
email
|
|
isDarkTheme
|
|
user {
|
|
id
|
|
username
|
|
createdAt
|
|
updatedAt
|
|
}
|
|
}
|
|
}
|
|
`;
|