Search code examples
graphqlstrapi

How to udpate an entry in graphql using variables


I'm using GraphQL plugin with strapi cms if it matters.

I cannot figure out how to update an existing query using a dynamic variable. My original mutation without using variables:

mutation {
  updateExam(input: {
    where: {
      id: "1234"
    },
    data: {
      questions: "hello"
    }
  }) {
    exam {
      questions
    }
  }
}

I learned that if I would like to create a new entry using variables I should write it like so (answer by David Maze here: How to pass JSON object in grpahql and strapi):

const response = await strap.request('POST', '/graphql', {
  data: {
    query: `mutation CreateExam($input: CreateExamInput!) {
  createExam(input: $input) {
    exam { name, desription, time, questions }
  }
}`,
    variables: {
      input: {
        name: examInfo.newExamName,
        desription: examInfo.newExamDescription,
        time: Number(examInfo.newExamTime),
        questions: [{ gf: "hello" }],
        subjects: [this.state.modalSubjeexisting
      }
    }
  }
});

But how can I update an exising query? Where should I put the

where: {id: "1234"}

How can I provide the existing id of the entry?


Solution

  • I don't know about this strapi cms, but by the way it looks the mutation you have already working, I'd try something like this for the update one:

        const response = await strap.request('POST', '/graphql', {
          data: {
            query: `mutation UpdateExam($input: UpdateExamInput!) {
              updateExam(input: $input) {
                exam { 
                  questions 
                }
              }
            }`,
            variables: {
              input: {
                where: {
                  id: examInfo.id
                },
                data: {
                  questions: [{ gf: "hello" }]
                }
              }
            }
          }
        });
    
    

    Give it a try and see if it works.