Modify your relay.config.js file to reflect the following:
1 2 3 4 5 6 7 8 91011121314151617181920
module.exports={// standard relay config optionssrc:'./src',language:'typescript',schema:'./data/schema.graphql',exclude:['**/node_modules/**','**/__mocks__/**','**/__generated__/**'],// pg_graphql specific optionsschemaConfig:{nodeInterfaceIdField:'nodeId',nodeInterfaceIdVariableName:'nodeId',},customScalarTypes:{UUID:'string',Datetime:'string',JSON:'string',BigInt:'string',BigFloat:'string',Opaque:'any',},}
schemaConfig tells the Relay compiler where to find the nodeId field on the node interface
customScalarTypes will improve Relay's type emission
Note
For Relay versions older than v16.2.0, it should be named customScalars instead.
Configuring your Relay Environment
This example uses Supabase for the GraphQL server, but pg_graphql can be used independently.
import{Environment,FetchFunction,Network,RecordSource,Store,}from'relay-runtime'importsupabase,{SUPABASE_ANON_KEY,SUPABASE_URL}from'./supabase'constfetchQuery:FetchFunction=async(operation,variables)=>{const{data:{session},}=awaitsupabase.auth.getSession()constresponse=awaitfetch(`${SUPABASE_URL}/graphql/v1`,{method:'POST',headers:{'Content-Type':'application/json',apikey:SUPABASE_ANON_KEY,Authorization:`Bearer ${session?.access_token??SUPABASE_ANON_KEY}`,},body:JSON.stringify({query:operation.text,variables,}),})returnawaitresponse.json()}constnetwork=Network.create(fetchQuery)conststore=newStore(newRecordSource())constenvironment=newEnvironment({network,store,getDataID:(node)=>node.nodeId,missingFieldHandlers:[{handle(field,_record,argValues){if(field.name==='node'&&'nodeId'inargValues){// If field is node(nodeId: $nodeId), look up the record by the value of $nodeIdreturnargValues.nodeId}returnundefined},kind:'linked',},],})exportdefaultenvironment
getDataID is the most important option to add, as it tells Relay how to store data correctly in the cache.
import{ConnectionHandler,graphql,useMutation}from'react-relay'// inside a React componentconst[todoCreateMutate,isMutationInFlight]=useMutation<TodoCreateMutation>(CreateTodoMutation)// inside your create todo functionconstconnectionID=ConnectionHandler.getConnectionID('root','TodoList_query_todosCollection')todoCreateMutate({variables:{input:{// ...new todo data},connections:[connectionID],},})