Next.js Discord

Discord Forum

Can i add my createGraphQLClient inside of a middleware ?

Unanswered
Siamese Crocodile posted this in #help-forum
Open in Discord
Siamese CrocodileOP
I crate a GraphQLClient to send custom headers, also i set up QueryProvider and stasff (there i have set up that meta
 
export function createGraphQLClient(accessToken?: string) {
  return new GraphQLClient(Config.gatewayGraphQLUrl, {
    headers: {
      ...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}),
    },
    credentials: 'include',
  })
}

A simple example is
export const useCurrentUser = () => {
  const {
    data: user,
    error,
    isLoading,
  } = useSuspenseQuery({
    queryKey: [STATE_KEYS.CURRENT_USER],
    queryFn: async ({ meta }) => {
      const client = createGraphQLClient(meta?.accessToken as string);
      const { currentUser } = await client.request(CurrentUserDocument);
      return currentUser;
    },
  });

  return {
    user,
    error,
    isLoading,
  };
};

My question is, can i createGraphQLClient inside of a middleware in next.js ?

0 Replies