14
votes

I have two Relay mutations that I'm nesting to first add an object then set its name. I believe what I'm passing to the second mutation is in fact data fetched by Relay, but it appears to disagree with me. The code in the React view is as follows:

Relay.Store.update(
        new AddCampaignFeatureLabelMutation({
            campaign: this.props.campaign
        }),
        {
            onSuccess: (data) => {
                Relay.Store.update(
                    new FeatureLabelNameMutation({
                        featureLabel: data.addCampaignFeatureLabel.featureLabelEdge.node,
                        name: this.addLabelInputField.value
                    })
                );
            },
            onFailure: () => {}
        }
    );

This does work, but gives me a warning:

Warning: RelayMutation: Expected prop `featureLabel` supplied to `FeatureLabelNameMutation` to be data fetched by Relay. This is likely an error unless you are purposely passing in mock data that conforms to the shape of this mutation's fragment.

Why does Relay think the data isn't fetched? Do I maybe need to explicitly return the new featureLabel in the payload somehow?

2

2 Answers

42
votes

I ran into the same problem and it took me some time to figure out what was going on, so this might help others:

As the warning says, you have to provide an entity to the mutation that was fetched by Relay. BUT what the warning does not say is that it has to be fetched with the mutation in mind.

So basically you have to add the mutation you are going to execute on it in the future in the initial query like this:

          fragment on Person {
            firstname,
            lastname,
            language,
            ${UpdatePersonMutation.getFragment('person')}
          }

This will add the necessary pieces to the entity in the store which are needed by the mutation.

In you case what you have to do is to add the FeatureLabelNameMutation getFragment to your AddCampaignFeatureLabelMutation query. This will bring back your featureLabel entity with the necessary information for the FeatureLabelNameMutation to succeed without warning.

The Relay documentation is very very poor on this and many other areas.

7
votes

Relay expects any fragments for your mutation to come from your props. Since you're using data coming from your callback and not something from your container props Relay raises that warning.

Take a look at the source: https://github.com/facebook/relay/blob/master/src/mutation/RelayMutation.js#L289-L307