如何使用变量在graphql中添加条目

时间:2019-02-02 01:29:53

标签: graphql strapi

如果有问题,我正在使用带有tradi cms的GraphQL插件。

我无法弄清楚如何使用动态变量更新现有查询。我的原始突变没有使用变量:

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

我知道,如果我想创建一个使用变量的新条目我应该写它像这样(由大卫迷宫答案在这里: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
      }
    }
  }
});

但是如何更新现有查询?我应该在哪里放置

where: {id: "1234"}

如何提供条目的现有ID?

1 个答案:

答案 0 :(得分:1)

我不知道这种绑带cms,但顺便说一下它看起来已经在起作用,我会尝试这样的方法来进行更新:

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" }]
        }
      }
    }
  }
});

尝试一下,看看它是否有效。

相关问题