relayjsrelayrelaymodernreact-relay

When making a node query, will react-relay check if that node is in the store before making a network request?


If I create a graphql server that has the following schema:

type Node {
   id: ID!
}
type User implements Node {
   id: ID!
   groups: [group!]!
}

type Group implements Node {
  id: ID!
  name: String!
}

type Query {
   me: User!
   node(id: ID!): Node
}

Later using react-relay I make the following query:

query {
  me {
    groups {
        id
        name
    }
  }
}

which returns the following

{
    me: {
        groups: [{
            id: '1',
            name: 'food lovers'
        }]
    }
}

if later on in my application I make another query as follows

query  {
    node(id: "1") {
        ... on Group {
            id
            name
        }
    }
}

will react-relay make a request to the server? or is it smart enough to know that we already have all the data for the requested item in our cache?


Solution

  • According to the documentation the default behavior network-only is not to use the cache and instead always fetch fresh results from the server. You can change this setting by specifying a fetchPolicy of store-and-network which will first use cached results and in parallel fetch fresh data from the network.

    It is also important to know, that relay does garbage collection on it's store, so records that aren't referenced will be removed from the "cache" eventually.

    The documentation for relay's experimental features indicates that there will be a store-only option and APIs to manually retain data in the cache, as well as ways to render partial data, while waiting for the network.