Lighthouse: type input doesn't allow creation of new model

Created on 17 Jul 2018  路  6Comments  路  Source: nuwave/lighthouse

I'm not sure if this is an issue, or lack of understanding from my part.

I have a mutation:

input UserInput {
    email: String! @rules(apply: [ "required", "email", "unique:users", "min:4", "max:255"])
    password: String! @bcrypt  @rules(apply: [ "required", "min:4", "max:255"])
}

extend type Mutation @middleware(checks: ["api", "auth:api"]){
    registerUser(input: UserInput!): User
        @create(model: "App\\User\\Models\\User")
        @check(can: ["create-users"])
}

And my mutation

mutation Register($input: UserInput!) {
    registerUser(input: $input) {
        id
        email
    }
}

// Vars
'input: {
    'email': '[email protected]',
    'password': 'secret',
}

I get the error SQLSTATE[HY000]: General error: 1 no such column: input.email (SQL: select count(*) as aggregate from \"users\" where "input"."email" = [email protected])

If I don't use the UserInput and hard code the values in, all works as expected.

It seems that it is looking to an input table rather than the users table.

I imagine I have a syntax error somewhere? Or should this be right?

Lighthouse Version: 2.1.*
Laravel Version: 5.6.*
PHP Version: 7.1.17

Thanks guys

Most helpful comment

I've found a fix! unique:users,email.

All 6 comments

The @create directive just passes in the given args directly to the create() of the Eloquent model. So if you use that, you will have to inline the args into the query.

graphql extend type Mutation @middleware(checks: ["api", "auth:api"]){ registerUser( email: String! @rules(apply: [ "required", "email", "unique:users", "min:4", "max:255"]) password: String! @bcrypt @rules(apply: [ "required", "min:4", "max:255"]) ): User @create(model: "App\\User\\Models\\User") @check(can: ["create-users"]) }

I am currently working on a solution that will allow you to do nested mutations to create multiple models with one query. It works in such a way that the root query fields expect InputObjects, just like the one you defined.

You can track the progress at #207 , i would be really happy to get your input on that. I guess it will take a while to get fully worked in, in the meantime, you might also solve this by adding in a custom directive.

Legendary. Thanks so much @spawnia . I will certainly check out that issue.

Cheers.

Guys I'm new to GraphQL and having similar issue. What's wrong? Can't I use input without writing my own mutation resolver?

Schema:

input CreateUserInput {
    name: String! @rules(apply: ["max:255"])
    email: String! @rules(apply: ["email", "max:255", "unique:users"])
    password: String! @hash @rules(apply: ["min:8"])
}

type Mutation {
    createUser(input: CreateUserInput! @spread): User @create
}

type User {
    id: ID!
    name: String!
    email: String!
    created_at: DateTime!
    updated_at: DateTime!
}

Request:

mutation {
  createUser(
    input: {
      name: "John"
      email: "[email protected]"
      password: "donotusethispassword"
    }
  ) {
    id
    name
    email
  }
}

Response:

{
  "errors": [
    {
      "debugMessage": "SQLSTATE[HY000]: General error: 1 no such column: input.email (SQL: select count(*) as aggregate from \"users\" where \"input\".\"email\" = [email protected])",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      }
    }
  ]
}

PHP v7.4.3
Laravel v8.13.0
Lighthouse v4.18.0

This is the problem with unique rule on your email. I think spawnia wants to improve this behavior in future, but for now, as spawnia said:

you will have to inline the args into the query.

So instead of using

createUser(input: CreateUserInput! @spread): User @create

try this.

createUser(
    name: String! @rules(apply: ["max:255"])
    email: String! @rules(apply: ["email", "max:255", "unique:users"])
    password: String! @hash @rules(apply: ["min:8"])
): User @create

This is also the way I solved it in my project. Not the prettiest solution, but it works without implementing some extra stuff

@lorado thanks. Isn't #207 supposed to fix this? I see it's merged. Also I saw a lot of code like mine in official docs, that's why I think it should work.

I've found a fix! unique:users,email.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

souljacker picture souljacker  路  3Comments

sadhakbj picture sadhakbj  路  4Comments

vine1993 picture vine1993  路  3Comments

let-aurn picture let-aurn  路  3Comments

eriktisme picture eriktisme  路  4Comments