How to add data from the mutation with no previous id

Viewed 123

Let's say I have a query

query {
  project(thing: "cats") {
    id
  }
}

That returns

{
  "data": {
    "project": null
  }
}

because the project for cats doesn't yet exist. Now I would like to create it using Relay (classic). My mutation looks something like this (simplified a bit by hardcoding variables etc.):

export class CreateProjectMutation extends Relay.Mutation {
  getMutation () {
    return Relay.QL`
      mutation {
        createProject (input: { thing: "cats" }) {
          project {
            id
          }
        }
      }
    `;
  }

  getFatQuery () {
    return Relay.QL`
      fragment on ProjectCreationPayload @relay(pattern: true) {
        project
      }
    `;
  }

  getConfigs () {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {

        // PROBLEM: There is no project id yet to change!
        project: this.props.project.id
      }
    }];
  }

  static fragments = {
    project: () => Relay.QL`
      fragment on Project {
        id
      }
    `
  };
}

So the problem is that I cannot do a FIELDS_CHANGE because the project does not yet have an id attached to it. How could I replace the existing

{ project: null }

in the client store with the mutation payload (project id that is generated server-side)?

1 Answers
Related