-
I'm trying to use Hasura with Clerk for authentication. I followed all of the steps here. Here's my code where I set up the auth headers: import { useState, useEffect } from "react";
import { GraphQLClient } from "graphql-request";
import { useAuth } from "@clerk/clerk-expo";
const endpoint = "https://my-app.hasura.app/v1/graphql";
function useGraphqlClient() {
const [token, setToken] = useState<string | null>(null);
const { getToken } = useAuth();
useEffect(() => {
(async () => {
setToken(await getToken({ template: "hasura" }));
})();
}, []);
console.log("token", token);
const client = new GraphQLClient(endpoint, {
headers: { Authorization: `Bearer ${token}` },
});
return client;
}
export default useGraphqlClient; Here's where I try to fetch data: import { Text } from "react-native";
import { gql } from "graphql-request";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import useGraphqlClient from "../../hooks/useGraphqlClient";
const id = "b99bc981-4ecd-49da-b743-7aa26b43c8de";
function useMyProfile(myId: string) {
const client = useGraphqlClient();
return useQuery({
queryKey: ["users", myId],
queryFn: async () => {
// @ts-ignore
const { user } = await client.request(
gql`
query GetMyProfile($myId: uuid!) {
users_by_pk(id: $myId) {
name
}
}
`,
{ myId }
);
return user;
},
});
}
function EditProfileScreen() {
const { status, data, error } = useMyProfile(id);
if (status === "error" && error instanceof Error) {
return <Text style={{ color: "red" }}>{error.message}</Text>;
}
if (status === "loading") {
return <Text>Loading...</Text>;
}
return <Text>Hello, {data.name}</Text>;
}
export default EditProfileScreen; But I get this error:
This is what my JWT looks like: This is what my Clerk JWT template config looks like: This is what my Hasura env config looks like: How do I fix this error? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I tried adding |
Beta Was this translation helpful? Give feedback.
-
I also tried creating a brand new Hasura project, but got exactly the same result. |
Beta Was this translation helpful? Give feedback.
-
I figured it out. It was just a very misleading error message. Due to a race condition/caching error, my token was coming back as
So the "1 part" alluded to by the error message was just the string "null". The fix was to update my code to make sure that the auth token gets set in the headers before the request is made. |
Beta Was this translation helpful? Give feedback.
I figured it out. It was just a very misleading error message. Due to a race condition/caching error, my token was coming back as
null
. So my request headers looked like this:So the "1 part" alluded to by the error message was just the string "null". The fix was to update my code to make sure that the auth token gets set in the headers before the request is made.