Mutating application state

Relay mutations are the actions that cause side effects in your systems, because they change the state of some resource that your UI cares about. What's interesting about Relay mutations is that they care about side effects that happen to your data as a result of a change in the state of something. For example, if you change the name of a user, this will certainly impact a screen that displays the user details. But, it could also impact a listing screen that shows several users.

The idea with Relay mutations is that you can tell them, ahead of time, where the application might be impacted in terms of side effects. Since other components may be affected by a mutation, we have already declared their data dependencies, Relay can handle this sort of thing! Awesome! Let's see what a mutation looks like:

class ChangeAgeMutation extends Relay.Mutation { 
  static fragments = { 
    user: () => Relay.QL` 
      fragment on User { 
        id, 
      } 
    `, 
    viewer: () => Relay.QL` 
      fragment on Viewer { 
        id, 
      } 
    `, 
  } 
 
  getMutation() { 
    return Relay.QL`mutation{changeAge}`; 
  } 
 
  getFatQuery() { 
    return Relay.QL` 
      fragment on ChangeAgePayload @relay(pattern: true) { 
        user { 
          age, 
        }, 
        viewer { 
          users, 
        }, 
     } 
    `; 
  } 
 
  getConfigs() { 
    return [{ 
      type: 'FIELDS_CHANGE', 
      fieldIDs: { 
        user: this.props.user.id, 
        viewer: this.props.viewer.id, 
      }, 
    }]; 
  } 
 
  getVariables() { 
    return { 
      age: this.props.age, 
      id: this.props.todo.id, 
    }; 
  } 
} 

Yikes, that's a lot of code for changing the name of a user. Well, it really isn't that much because this includes handling side effects. Let's do a quick overview of what makes up this mutation:

  • fragments: These GraphQL snippets tell the mutation what data is used by the component, before the mutation actually happens.
  • getMutation(): The actual mutation from the server that will change some backend resource.
  • getFatQuery(): This is how Relay is able to determine what might be affected as a side effect of performing this mutation. For example, the user might change, but also the viewer.users collection.
  • getConfigs(): This tells Relay about the type of mutation we are about to perform so that it can plan accordingly. In this case, we're just changing a property value, but a mutation can also add or remove items from a collection, and so on.
  • getVariables(): The parameters of the mutation that are sent to the backend GraphQL server where the actual mutation is performed.

This is just the declaration of the mutation itself. In the next chapter, we'll walk through performing the actual mutation in response to some event, such as user interaction.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.162.87