Skip to content

useGraphQLQuery

useGraphQLQuery<T>(queryKey, query, options?): UseQueryResult<NoInfer<undefined | T>, GraphQLError>

Hook to execute GraphQL queries with React Query caching

Type ParameterDefault typeDescription
TunknownThe expected response data type
ParameterTypeDescription
queryKeystring | readonly unknown[]Unique key for caching (string or array)
querystringThe GraphQL query string
options?UseGraphQLQueryOptions<T>Query options including variables

UseQueryResult<NoInfer<undefined | T>, GraphQLError>

React Query result object

interface UsersQuery {
users: Array<{ id: string; email: string }>
}
function UsersList() {
const { data, isLoading } = useGraphQLQuery<UsersQuery>(
'users',
`query { users { id email } }`
)
return <div>{data?.users.length} users</div>
}
// With variables
const { data } = useGraphQLQuery<UserQuery>(
['user', userId],
`query GetUser($id: ID!) { user(id: $id) { id email } }`,
{ variables: { id: userId } }
)