Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[writeRelayUpdatePayload] Show different error depending on whether a required field was subtracted out or simply not returned by the server #231

Closed
sgwilym opened this issue Sep 3, 2015 · 12 comments

Comments

@sgwilym
Copy link
Contributor

sgwilym commented Sep 3, 2015

I've got a mutation that creates a new Leaf in a schema kind of like this:

Trunk {
  Branches {
    Leaves
  },
  Leaves
}

The fat query in my mutation looks like this:

fragment on CreateLeafPayload {
  trunk { leaves },
  newTreeLeafEdge,
  branch { leaves },
  newBranchLeafEdge
}

and the configs look like this:

{
  type: 'RANGE_ADD',
  parentName: 'branch',
  parentID: this.props.branch.id,
  connectionName: 'leaves',
  edgeName: 'newBranchLeafEdge',
  rangeBehaviors: {
    '': 'append'
  }
},
{
  type: 'RANGE_ADD',
  parentName: 'trunk',
  parentID: this.props.trunk.id,
  connectionName: 'leaves',
  edgeName: 'newTrunkLeafEdge',
  rangeBehaviors: {
    '': 'append'
  }
}

Executing this query throws a warning in the console:

writeRelayUpdatePayload(): Expected response payload to include the newly created edge `newUserComicEdge` and its `node` field. Did you forget to update the `RANGE_ADD` mutation config?

The mutation query received by the server doesn’t have the trunk { id } or newTrunkLeafEdge fields, so the GraphQL server doesn’t return them. I know Relay intersects the mutation query with a 'tracked query' — maybe something is going wrong there?

@sgwilym sgwilym changed the title Missing fields in RANGE_ADD Mutation Payload Missing fields in Mutation Payloads Sep 3, 2015
@sgwilym
Copy link
Contributor Author

sgwilym commented Sep 3, 2015

This might not be anything to do with RANGE_ADD, as I'm now getting a similar problem in an extremely simply FIELDS_CHANGE mutation: Relay never queries for updatedObject { updatedField }.

I found the cause of this (#232), but the problem in my first comment still stands.

@sgwilym sgwilym changed the title Missing fields in Mutation Payloads Relay omitting fields on Mutation payload fragments Sep 3, 2015
@yungsters
Copy link
Contributor

I found the cause of this (#232), but the problem in my first comment still stands.

Thanks for investigating and reporting. If #232 fixes your problem, which remaining problem are you referring to in the first comment?

@sgwilym
Copy link
Contributor Author

sgwilym commented Sep 3, 2015

The problem is that the mutation doesn’t include the fragments for trunk or newTrunkLeafEdge, so the returned payload is missing them to, meaning I get the error:

writeRelayUpdatePayload(): Expected response payload to include the newly created edge newUserComicEdgeand itsnodefield. Did you forget to update theRANGE_ADD mutation config?

When the RANGE_ADD mutation config is present and correct.

@sgwilym
Copy link
Contributor Author

sgwilym commented Sep 3, 2015

Just to take this out of the abstract a bit, I've made a gist with the actual mutation, where I'm calling it from, the resulting query + variables, and the returned payload: https://gist.github.com/sgwilym/77191368cf1885a289e2

@sgwilym
Copy link
Contributor Author

sgwilym commented Sep 3, 2015

Thanks to the help of @devknoll, I found out this issue is because nowhere else in the app is user { comics } being queried, so the query from the mutation is being subtracted. I guess my only remaining confusion then is whether Relay should be throwing errors like the one posted above when it doesn't receive the corresponding fragment for a query that it itself subtracted.

@josephsavona
Copy link
Contributor

@sgwilym Good point - the error could be more informative and check whether the field was in the query (which means its a server error to not include in the response) or if it isn't in the query (which means it was subtracted and the message could tell you the field isn't being queried anywhere).

@bmcmahen
Copy link

bmcmahen commented Sep 8, 2015

I spent quite a while debugging the same thing, too. It was the same issue that @sgwilym was having; i.e., the app wasn't querying the relevant fields. A more helpful warning message might be useful here.

@xymostech
Copy link

I'm running into this issue as well. In my case, I'm trying to use the returned data in the mutation's onSuccess (in particular, I'm routing to a page with the returned node's id), so I need the data to be returned, even if the data isn't being queried elsewhere in the app.

Is there a (good) way to force the fields to be put in the query, even if they're not used? I could do something silly like add a dummy query viewer { books(first: 1) { edges { nodes { id } } } } to ensure that the fields are used, but that doesn't seem great.

@steveluscher
Copy link
Contributor

I wrote about how to use the REQUIRED_CHILDREN config to force certain fields to be made available to your onSuccess handler, @xymostech: http://stackoverflow.com/questions/32539189/how-to-get-the-id-of-a-new-object-from-a-mutation

See also #237.

@steveluscher steveluscher changed the title Relay omitting fields on Mutation payload fragments [writeRelayUpdatePayload] Show different error depending on whether a required field was subtracted out or simply not returned by the server Sep 17, 2015
@steveluscher
Copy link
Contributor

The task. Given a RANGE_ADD mutation config:

  1. If Relay never queries for the new edge because they were eliminated during the intersection of the tracked and fat queries, explain that there's a client-side problem and how to fix it (namely to make sure the parent/connection is used – ie. tracked – in your app)
  2. If the server never responds with the newly created edge despite having been queried for it, explain that the problem is on the server side.

@xymostech
Copy link

@steveluscher Awesome! Looks like #237 also addresses the fact that REQUIRED_CHILDREN is undocumented so far. 👍

@josephsavona
Copy link
Contributor

merging into #542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants