2

I am building a nativescript mobile application which consume graphql API, and I am using apollo client via apollo boost.

The problem appear when I am trying to send array of objects inside the mutation like below:

let {
    to,
    total,
    drugList
} = order

apolloClient.mutate({
    mutation: gql `mutation {
        makeOrder(
            to: "${to}",
            total: ${total},
            drugList: ${drugList}
        ){
            id
        }
    }`
}).then((res) => {
    console.log(res)
}).catch((error) => {
    console.log(error)
})

I have tried to log the drugList inside a template literals like:

console.log(`${drugList}`)

But I got [object object],[object object] then I have tried to use ${[...drugList]} instead and I got the desired structure of array of objects but the mutate function of apollo client doesn't accept it (doesn't execute the mutation or log an error).

Am I miss something to make it run or are there any recommendation to run it?

7
  • What is order, what value exactly does drugList have? Commented Oct 5, 2019 at 16:40
  • Notice that in your query you are using a gql-tagged template literal, which you cannot compare to the simple template string in your console.log test. Commented Oct 5, 2019 at 16:41
  • @Bergi order object contains to, total and drugList and those are id, float and array of objects respectively. Commented Oct 5, 2019 at 16:48
  • @Bergi Okay that's a good note, but how to send an array of object inside it? Commented Oct 5, 2019 at 16:49
  • 1
    You might also try console.log(gql `mutation { makeOrder( to: "${to}", total: ${total}, drugList: ${drugList} ){ id } }`) Commented Oct 5, 2019 at 16:51

1 Answer 1

2

Thanks to Bergi, after his notice that gql-tagged template literal cannot be compared to the simple template string in a console.log test.

So I have searched around this and figured out that variables property would solve this problem. Here is the final result:

let {
    to,
    total,
    drugList
} = order

apolloClient.mutate({
    mutation: gql `mutation ($to: ID!, $total: Float!, $drugList: [OrderDrugsListInput!]!) {
        makeOrder(
            to: $to,
            total: $total,
            drugList: $drugList
        ){
            id
        }
    }`,
    variables: {
        to: to,
        total: total,
        drugList: drugList
    }
}).then((res) => {
    console.log(res)
}).catch((error) => {
    console.log(error)
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.