denvned / isomorphic-relay

Adds server side rendering support to React Relay
BSD 2-Clause "Simplified" License
242 stars 53 forks source link

Why does Relay sometimes prefix query keys with underscores and random strings? #62

Closed damassi closed 7 years ago

damassi commented 7 years ago

Forgive me if this should be posted in the Relay repo, but since this has to do with data before its sent over to the client I figured I would post here first.

So I'm trying to get response information from a Relay request before its sent over to the client, but occasionally, when inspecting the response object, it looks like there's some kind of reconciliation going on where Relay prefixes my object keys with an _ and a random string, e.g., _user3fdg.

For example, given this dummy query:

export default {
  viewer: (component, params) => Relay.QL`
    query {
      viewer {
        sale(id: "los-angeles-modern-auctions-march-2015") {
          name
        }
        sales {
          id
          sale_artworks {
            id
          }
        }
     }
  }
}

Returns this response:

...

IsomorphicRelay.prepareData(renderProps, networkLayer).then(({ data }) => {
  console.log(data)
})
screen shot 2017-02-06 at 9 53 36 pm

Notice the strange _saleti0VC key thats being passed back (I'm expecting just sale), while the sales key seems correct? This makes it impossible to deterministically grab data from a response to perform side-effects server-side before the data on the server is sent to the client.

What I would expect is that once IsomorphicRelay.prepareData returns its data response that it is the final representation that is rehydrated on the client, but unfortunately that's not so. It seems like the final tree is only resolved after it gets all the way to the client and injected as props into a Relay.Container component.

Thoughts?

damassi commented 7 years ago

Going to go ahead and close this -- After some internal discussion it's best to stay within the component model for accessing Relay-resolved data.

denvned commented 7 years ago

Looks like Relay automatically aliases field queries having arguments to avoid potential name conflicts for multiple queries of the same field but with different arguments: http://graphql.org/learn/queries/#aliases You can try to specify an alias yourself.

Also see a proper way to query data outside a Relay container: https://github.com/facebook/relay/issues/114#issuecomment-197003816 (the bottom part of the comment).

damassi commented 7 years ago

@denvned - Thank you so much for pointing me to that issue! Of course the api method is undocumented in Relay docs but such is life 🤕