importtype{CodegenConfig}from'@graphql-codegen/cli'import{addTypenameSelectionDocumentTransform}from'@graphql-codegen/client-preset'constconfig:CodegenConfig={schema:'http://localhost:54321/graphql/v1',// Using the local endpoint, update if neededdocuments:'src/**/*.tsx',overwrite:true,ignoreNoDocuments:true,generates:{'src/gql/':{preset:'client',documentTransforms:[addTypenameSelectionDocumentTransform],plugins:[],config:{scalars:{UUID:'string',Date:'string',Time:'string',Datetime:'string',JSON:'string',BigInt:'string',BigFloat:'string',Opaque:'any',},},},},hooks:{afterAllFileWrite:['npm run prettier'],// optional},}exportdefaultconfig
Configuring Apollo Client
This example uses Supabase for the GraphQL server, but pg_graphql can be used independently.
import{ApolloClient,InMemoryCache,createHttpLink,defaultDataIdFromObject}from'@apollo/client'import{setContext}from'@apollo/client/link/context'import{relayStylePagination}from'@apollo/client/utilities'importsupabasefrom'./supabase'constcache=newInMemoryCache({dataIdFromObject(responseObject){if('nodeId'inresponseObject){return`${responseObject.nodeId}`}returndefaultDataIdFromObject(responseObject)},possibleTypes:{Node:['Todos']}// optional, but useful to specify supertype-subtype relationshipstypePolicies:{Query:{fields:{todosCollection:relayStylePagination(),// example of paginating a collectionnode:{read(_,{args,toReference}){constref=toReference({nodeId:args?.nodeId,})returnref},},},},},})consthttpLink=createHttpLink({uri:'http://localhost:54321/graphql/v1',})constauthLink=setContext(async(_,{headers})=>{consttoken=(awaitsupabase.auth.getSession()).data.session?.access_tokenreturn{headers:{...headers,Authorization:token?`Bearer ${token}`:'',},}})constapolloClient=newApolloClient({link:authLink.concat(httpLink),cache,})exportdefaultapolloClient
typePolicies.Query.fields.node is also optional, but useful for reducing cache misses. Learn more about Redirecting to cached data.