0

I'm searching for 3 days how to achieve an nested mutation.

I'm using Nuxt JS, Strapi with Graphql and graphql-tag module, and MongoDb

I have two collections : Heaturls and heatmap, with a relation Heatmap has and belongs to one Heaturl

I use store to query and mutate.

What I want is to add new entry to my Heatuls collection and after it create new entry in my Heatmap collection with the ID of my previously created heaturl entry.

Here are my functions Store.js

async saveHeaturl({
    commit,
    dispatch
  }, datas) {
    console.log(this.app)
    return await this.app.apolloProvider.clients.defaultClient.mutate({
      mutation: CREATE_HEAT_MUTATION,
      variables: {
        ...datas,
        site: this.$auth.$state.user.site,
      }
    }).then((res) => {
      commit('addHeatItem', res.data.createHeaturl.heaturl)
      console.log('res ???', res.data)
      dispatch("saveHeatmap", res.data.createHeaturl.heaturl)
    })
  },
async saveHeatmap({
    commit,
    
  }, datas) {
    console.log("on a bien disptach le bordel", datas)

    return await this.app.apolloProvider.clients.defaultClient.mutate({
      mutation: CREATE_HEATMAP_MUTATION,
      variables: {
        heatdatas:JSON.stringify({}),
        heaturl_id: datas._id
      }
    }).then(res => {
      console.log("res ======", res)
    })
  },

The saveHeatmap is dispatch after saveHeaturl is done.

Here are my query structures

export const CREATE_HEATMAP_MUTATION = gql `
  mutation createHeatmap($heatdatas: JSON!, $heaturl_id: ID!) {
  createHeatmap(
    input: { data: { heatdatas: $heatdatas, heaturl_id: $heaturl_id } }
  ) {
    heatmap {
      _id
      heaturl_id {
        _id       
      }
    }
  }
}
`

export const CREATE_HEAT_MUTATION = gql `
  mutation createHeaturl(
  $status: Boolean!
  $recordingflow: Int!
  $urlname: String!
  $pagecasting: Boolean!
  $pageurl: String!
  $datamatching: Int!
  $shortdesc: String!
  $pagesnapshot: JSON!
  $site:ID!
) {
  createHeaturl(
    input: {
      data: {
        urlname: $urlname
        recordingflow: $recordingflow
        status: $status
        pagecasting: $pagecasting
        pageurl: $pageurl
        datamatching: $datamatching
        shortdesc: $shortdesc
        pagesnapshot: $pagesnapshot
        site:$site
      }
    }
  ) {
    heaturl {
      _id
      createdAt
      status
      urlname
      pageurl
      shortdesc

    }
  }
}
`

When I use my form to save my heaturl, everything is ok (it's saved in my DB) and I get the last created id. When I dispatch the second action, I always get an error.

{,…}
data: {createHeatmap: null}
createHeatmap: null
errors: [{message: "Cannot convert object to primitive value", locations: [{line: 2, column: 3}],…}]
0: {message: "Cannot convert object to primitive value", locations: [{line: 2, column: 3}],…}
extensions: {code: "INTERNAL_SERVER_ERROR", exception: {,…}}
locations: [{line: 2, column: 3}]
message: "Cannot convert object to primitive value"
path: ["createHeatmap"]

I don't know how to solve this issue. I've tried changing types format without succes.

I don't understand why I have such error. I have other relations with others collection, and it's working fine.

Can anyone give me a clue or any tutorial to solve my problem.

Many thanks to everyone

Fabien

FabienC
  • 53
  • 1
  • 7
  • does `createHeatmap` work in playground with query variables with jsoned empty object? – xadm Feb 04 '21 at 21:50
  • No, Same issue : Cannot convert object to primitive value – FabienC Feb 04 '21 at 22:56
  • then it's a resolver problem? – xadm Feb 04 '21 at 23:00
  • I'm new to graphql, and I'm using graphql-tag to manage my queries. What, or How do you think I can console.log what's happening ? What ever data I send, I still get this error. I've tried sending either one or the orther arguments to the createHeatmap, but, same error. I'm still reading apollo and grpahql-tag docs. But notthing about relations between collections. I've seen other frameworks proposing it, like Prisma (if I understood well), but I have no idea how to do it – FabienC Feb 04 '21 at 23:06
  • separate problems, fix BE before coding FE – xadm Feb 04 '21 at 23:18
  • Sorry, what does this mean " fix BE before coding FE" ? – FabienC Feb 04 '21 at 23:23
  • BackEnd/API vs FrontEnd – xadm Feb 04 '21 at 23:24
  • If I use BE via Strapi, no problem. I can create new heatmap, and associate it to one Heaturl. When I try to do it FE, there this issue – FabienC Feb 04 '21 at 23:28
  • editing content other way than using graphql API doesn't make API working OK – xadm Feb 04 '21 at 23:42
  • Your right. I'll find out why it not working this way. If you can advise any readings ! – FabienC Feb 04 '21 at 23:48
  • https://javascript.info/object-toprimitive ... are you using `alert( someObject)` in resolver? – xadm Feb 05 '21 at 00:14
  • I finally found my answer. You were totally right. Problem was in BE. I've tried JSON.parse on an object. I've modified it and now everything is fine. Thanks – FabienC Feb 06 '21 at 08:52

0 Answers0