0

I have a problem. I want to create query to update some fileds on multiple model, it should looks like this:

mutation{
    updateInternalOrder( input: {
        state: {
            connect: 1
        }
        id_internal_orders: [1,2] <= here
    }){
        id_internal_orders
        qty
        state {
            id_internal_orders_states,
            name
        }
    } 
}

In this query i would like to assign(update) id_internal_orders_states(in states) in id_internal_orders that has id: 1 and 2.

How to do that?

Schema(lighthouse-php) that works only if i provide a single id, not array:

extend type Mutation {
    updateInternalOrder(input: UpdateInternalOrders! @spread): InternalOrders @update
}

input UpdateInternalOrders {
    id_internal_orders: Int!
    state: InternalOrdersStatesHasOne
    qty: Int
    id_supplier: Int
}

input InternalOrdersStatesHasOne {
    connect: Int
}
burasuk
  • 162
  • 2
  • 8

1 Answers1

0

Instead of this

input UpdateInternalOrders {
    id_internal_orders: Int!
    state: InternalOrdersStatesHasOne
    qty: Int
    id_supplier: Int
}

Your schema should look like this

input UpdateInternalOrders {
    id_internal_orders: [Int]!
    state: InternalOrdersStatesHasOne
    qty: Int
    id_supplier: Int
}

So this way id_internal_orders will be define as an array

Solution for the error Argument 2 passed to Nuwave\\Lighthouse\\Execution\\Arguments\\ArgPartitioner::relationMethods() must be an instance of Illuminate\\Database\\Eloquent\\Model, instance of Illuminate\\Database\\Eloquent\\Collection given

The error you get is because you might be using an ORM. The data passed to the mutation is a collection, probably because you manipulate model generated by your ORM. GraphQL expect an array and not a Collection.

You must either convert the collection in array. But this is not recommended. In case there is a collection of object with collection. You’ll have to convert the collection and all the collection inside each object of the parent collection. This can get complicated very fast.

Or you can find a way to not manipulate your model in your front end and manipulate data transfer object instead. But I can’t really help you here since I don’t know where the data come from.

Joyescat
  • 507
  • 5
  • 11
  • i try this, but doesn't work. instead i have error: `Argument 2 passed to Nuwave\\Lighthouse\\Execution\\Arguments\\ArgPartitioner::relationMethods() must be an instance of Illuminate\\Database\\Eloquent\\Model, instance of Illuminate\\Database\\Eloquent\\Collection given` – burasuk Oct 14 '21 at 06:00
  • I made an edit to answer your question – Joyescat Oct 14 '21 at 13:01