Capítulo 7 de 80
Because TanStack Query is built on plain Promises, any GraphQL client that returns a Promise (e.g. graphql-request) works as a queryFn with no special integration — pairing it with GraphQL Code Generator gives fully-typed operations.
queryFn just needs to return a Promise; GraphQL is one option among REST/gRPC/anything-async, not a first-class special case.graphql-request + GraphQL Code Generator's graphql() tag produces a typed document; passing it to request() inside queryFn gives a fully-typed data with no manual typing.import request from 'graphql-request'
import { graphql } from './gql/gql'
const allFilmsQuery = graphql(/* GraphQL */ `
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) { edges { node { id title } } }
}
`)
function App() {
const { data } = useQuery({
queryKey: ['films'],
queryFn: () => request('https://swapi-graphql.netlify.app/.netlify/functions/index', allFilmsQuery, { first: 10 }),
})
}
queryFn — no adapter or special hook needed.queryFn returns a Promise) that makes this integration trivial.