Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relay mutation expects data fetched by Relay

Tags:

relayjs

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?

like image 522
logos Avatar asked Nov 18 '15 00:11

logos


1 Answers

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.

like image 53
Christine Avatar answered Sep 28 '22 05:09

Christine