Search code examples
laravelgraphqllaravel-lighthouse

Nested mutations don't seem to work in Lighthouse 3.7


I'm trying to setup a schema in Lighthouse 3.7/Laravel 5.8 What I want to achieve is this: A User should be able to create Clists. There is a One to Many relation between User and Clist. And I'm trying to implement nested mutations as described here.

I have already implemented the "query" part and it works fine. But when I test a createClist mutation in GraphQL Playground, I get this error:

"debugMessage": "Array to string conversion",

"message": "Internal server error",

"extensions": {
        "category": "internal"
      },
...

And I can't figure out what I'm doing wrong.

Here is my code:

type Mutation {
  createClist(input: CreateClistInput! @spread): Clist @create
}

input CreateClistInput {
    name: String!
    description: String
    starred: Boolean
    user: CreateUserRelation!
    ctags: CreateCtagRelation
}

input CreateUserRelation {
  connect: ID!
}

input CreateCtagRelation {
  create: [CreateCtagInput!]
  connect: [ID!]
  sync: [ID!]
}

input CreateCtagInput {
  name: String!
}


And here is a screenshot of GraphQL Playground:

enter image description here


Solution

  • When using the @spread directive a typehint on the relationship in your model is required.

    Taken from the docs there is the following example:

    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    
    class Post extends Model 
    {
        // WORKS
        public function user(): BelongsTo
        {
            return $this->belongsTo(User::class);
        }
    
        // DOES NOT WORK
        public function comments()
        {
            return $this->hasMany(Comment::class);        
        }
    }
    

    Lighthouse uses the type hint to determine how it should handle the relationship.