Lighthouse: Nested creation not working with HasMany relations

Created on 26 Apr 2020  路  3Comments  路  Source: nuwave/lighthouse

Describe the bug

Nested @create creates only one relation for a HasMany relation.

Here is my model

type User {
    id: ID!
    name: String!
    images: [Image!]! @hasMany
}

type Image {
    id: ID!
    url: String!
    user: User! @belongsTo
}

And my mutation

input UserInput {
    name: String!
    images: [ImageInput!]! @create
}

input ImageInput {
    url: String!
}

type Mutation {
    createUser(user: UserInput! @spread): User @create
}

Then, my query

mutation CreateUser($user: UserInput!) {
  createUser(user: $user) {
    id
  }
}

In my network tab (Request Payload), I have

{
  "operationName": "CreateUser",
  "variables": {
    "user": {
      "name": "John Doe",
      "images": [
        { "url": "http://lorem.com/Pya0.png" },
        { "url": "http://lorem.com/awka.png" }
      ]
    }
  },
  "query": "mutation CreateUser($user: UserInput!) {\n  createUser(user: $user) {\n    id\n    __typename\n  }\n}\n"
}

Output/Logs

But my here is the logs of my SQL.

[2020-04-25 21:35:07] local.DEBUG: insert into `user` (`name`, `updated_at`, `created_at`) values (?, ?, ?)  
[2020-04-25 21:35:07] local.DEBUG: insert into `images` (`user_id`, `url`) values (?, ?)  
[2020-04-25 21:35:07] local.DEBUG: update `images` set `url` = ? where `id` = ?  

It looks like it overrides the first Image item.

Expected behavior/Solution

The last SQL statement should also be an insert.
I want my relations to be User HasMany Image.
But it looks like it's User HasOne Image.

Environment

"nuwave/lighthouse": "^4.11",
"laravel/framework": "^7.0"
bug

All 3 comments

This could be related to https://github.com/nuwave/lighthouse/pull/1321, can you try installing dev-master and check if that fixes the error for you?

So yes.. it works fine with dev-master 馃敟

Alright. I think the latest release should also work: https://github.com/nuwave/lighthouse/releases/tag/v4.12.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhoanborges picture jhoanborges  路  4Comments

spawnia picture spawnia  路  3Comments

souljacker picture souljacker  路  3Comments

a-ssassi-n picture a-ssassi-n  路  3Comments

mehranabi picture mehranabi  路  3Comments