3

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
        }
      ]
    }
  ]
}
Noitidart
  • 35,443
  • 37
  • 154
  • 323

2 Answers2

4

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.

Community
  • 1
  • 1
marktani
  • 7,578
  • 6
  • 37
  • 60
0

You can do this on scaphold.io. The Logic system includes more than just mutation callbacks. You can fire functions before mutations to validate/clean input before it is saved to the DB, after to manage connections like this that will get returned in that same mutation payload, and asynchronously (like mutation callbacks) for kicking off long standing tasks. You can even compose functions together to pass meta-data through a chain of function invocations.

mparis
  • 3,623
  • 1
  • 17
  • 16
  • 1
    Interesting thanks @mparis! I was hoping in graphql you can create a related entry at same time (in one operation) as createUser, is it not possible in graphql? So each mutation in graphql can only affect one model at a time? – Noitidart Feb 09 '17 at 20:16
  • 1
    You're very welcome :). GraphQL doesn't really limit anything and it is up to the implementation to decide what is possible. For example, you can also create nested objects when they are involved in a one-to-one or one-to-many relation (E.G. https://scaphold.io/docs/#nested-create). Many-to-many is a little trickier and the post-op logic functions can help managing those. For example if a new user signed up and you wanted to enroll them in a team (E.G. if they were signing up for an app like slack) – mparis Feb 09 '17 at 20:44
  • may you please see my updated question, I think I was really confused at the start. I think it's more simple then I thought. I need to set a related field's value on create, is this possible? – Noitidart Feb 09 '17 at 23:16
  • Hi all mparis had correct answer I think, until I edited my question, please don't downvote. I think downvotes should go to my original post as I phrased it very badly. – Noitidart Feb 10 '17 at 04:03