From ae1203f5b9e7af52e03dc622e88a3e7b13d757a8 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Fri, 25 Nov 2022 19:24:36 +0300 Subject: [PATCH] session api upgrade --- src/context/session.tsx | 10 +++++----- src/graphql/mutation/my-session.ts | 2 +- src/graphql/types.gen.ts | 2 +- src/utils/apiClient.ts | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/context/session.tsx b/src/context/session.tsx index 47a51e76..966d8d6f 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -8,7 +8,7 @@ type SessionContextType = { session: InitializedResource isAuthenticated: Accessor actions: { - refreshSession: () => AuthResult | Promise + getSession: () => AuthResult | Promise signIn: ({ email, password }: { email: string; password: string }) => Promise signOut: () => Promise confirmEmail: (token: string) => Promise @@ -17,7 +17,7 @@ type SessionContextType = { const SessionContext = createContext() -const refreshSession = async (): Promise => { +const getSession = async (): Promise => { try { const authResult = await apiClient.getSession() if (!authResult) { @@ -37,7 +37,7 @@ export function useSession() { } export const SessionProvider = (props: { children: JSX.Element }) => { - const [session, { refetch: refetchRefreshSession, mutate }] = createResource(refreshSession, { + const [session, { refetch: refetchSession, mutate }] = createResource(getSession, { ssrLoadFrom: 'initial', initialValue: null }) @@ -65,7 +65,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { } const actions = { - refreshSession: refetchRefreshSession, + getSession: refetchSession, signIn, signOut, confirmEmail @@ -74,7 +74,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { const value: SessionContextType = { session, isAuthenticated, actions } onMount(() => { - refetchRefreshSession() + refetchSession() }) return {props.children} diff --git a/src/graphql/mutation/my-session.ts b/src/graphql/mutation/my-session.ts index aba14e7d..122cd3b1 100644 --- a/src/graphql/mutation/my-session.ts +++ b/src/graphql/mutation/my-session.ts @@ -2,7 +2,7 @@ import { gql } from '@urql/core' export default gql` mutation RefreshSessionMutation { - refreshSession { + getSession { error token user { diff --git a/src/graphql/types.gen.ts b/src/graphql/types.gen.ts index e199e4cf..aaf6681f 100644 --- a/src/graphql/types.gen.ts +++ b/src/graphql/types.gen.ts @@ -186,7 +186,7 @@ export type Mutation = { inviteChat: Result markAsRead: Result rateUser: Result - refreshSession: AuthResult + getSession: AuthResult registerUser: AuthResult removeAuthor: Result sendLink: Result diff --git a/src/utils/apiClient.ts b/src/utils/apiClient.ts index 49733097..aea12c74 100644 --- a/src/utils/apiClient.ts +++ b/src/utils/apiClient.ts @@ -179,11 +179,11 @@ export const apiClient = { throw new ApiError('unknown', response.error.message) } - if (response.data?.refreshSession?.error) { - throw new ApiError('unknown', response.data.refreshSession.error) + if (response.data?.getSession?.error) { + throw new ApiError('unknown', response.data.getSession.error) } - return response.data.refreshSession + return response.data.getSession }, getAllTopics: async () => { const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()