Search code examples
graphqlgraphcool

createUser update related field - understanding relation


I need to set a related field's value on create, is this possible?

Details: I have a User model with fields: email, displayname. I have a Verify model with fields: code, action.

I created a relation between the two models like this:

I want to createUser and set the related fields of code and action at the same time. I tried this:

mutation {
  createUser
  (
    email:"noit@mail.com",
    displayname:"noit",
    password:"123",
    code: "this is a code",
    action: "REGISTER"
  ) {
    id
  }
}

This fails with:

{
  "data": null,
  "errors": [
    {
      "message": "Unknown argument 'code' on field 'createUser' of type 'Mutation'. (line 2, column 76):\n  createUser(email: \"noit@mail.com\", displayname: \"noit\", password: \"123\", code: \"this is a code\", action: \"REGISTER\") {\n                                                                           ^",
      "locations": [
        {
          "line": 2,
          "column": 76
        }
      ]
    },
    {
      "message": "Unknown argument 'action' on field 'createUser' of type 'Mutation'. (line 2, column 100):\n  createUser(email: \"noit@mail.com\", displayname: \"noit\", password: \"123\", code: \"this is a code\", action: \"REGISTER\") {\n                                                                                                   ^",
      "locations": [
        {
          "line": 2,
          "column": 100
        }
      ]
    }
  ]
}

Solution

  • We specifically designed the Graphcool API to handle cases like this as simple as possible, you can do it like this:

    mutation { 
      createUser (
        email:"noit@mail.com",
        displayname:"noit",
        password:"123",
        blahVerify: {
          code: "this is a code",
          action: "REGISTER"  
        }) {
        id
        blahVerify {
          id  
        } 
      }
    }
    

    Note the nested blahVerify object argument.

    This answer to a similar question goes a bit more into detail and also shows how you can use GraphQL variables to send nested mutations from Apollo Client.

    As a sidenote, depending on the different possible value for the action of a Verify node, you might want to use an enum field rather than strings. You can read more about enum fields in the documentation.