简体   繁体   English

如何进行 GraphQL 更新突变?

[英]How to make a GraphQL update mutation?

I'm new to GraphQL.我是 GraphQL 的新手。 I've got a schema and mutation for adding data, but I'm a bit stuck on how to make an update mutation to update existing data on the database.我有一个用于添加数据的模式和突变,但我对如何进行更新突变以更新数据库中的现有数据有些困惑。

I think I know what I might need to do to achieve this, but I'd appreciate some pointers on whether or not my idea is the right approach or not.我知道我可能需要做什么来实现这一点,但我很感激我的想法是否是正确的方法。

Here's my schema which contains my mutation GraphQLObject.这是我的架构,其中包含我的突变 GraphQLObject。

const graphql = require("graphql");
const _ = require("lodash");
const Student = require("../models/students");
const Class = require("../models/classes");
const classes = require("../models/classes");

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLSchema,
  GraphQLID,
  GraphQLInt,
  GraphQLList,
  GraphQLNonNull,
} = graphql;

const Mutation = new GraphQLObjectType({
  name: "Mutation",
  fields: {
    addStudent: {
      type: StudentType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        age: { type: new GraphQLNonNull(GraphQLString) },
        test1: { type: new GraphQLNonNull(GraphQLString) },
        classId: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve(parent, args) {
        let student = new Student({
          name: args.name,
          age: args.age,
          test1: args.test1,
          classId: args.classId,
        });
        console.log(student);
        return student.save();
      },
    },
    addClass: {
      type: ClassType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        year: { type: new GraphQLNonNull(GraphQLString) },
      },
      resolve(parent, args) {
        let newclass = new Class({
          name: args.name,
          year: args.year,
        });
        return newclass.save();
      },
    },
    editStudent: {
      type: StudentType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID)},
        name: { type: GraphQLString },
        age: { type: GraphQLString },
        test1: { type: GraphQLString },
        classId: { type: GraphQLID },
      },
      resolve(parent, args) {
        //logic that finds the relevant data object by id, then updates that object//
        }
      }
    }
  },
});

module.exports = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation,
});

Would I be correct in saying that inside the resolve function I need to first find the relevant object in the database by id and then return the args of whatever arg has been updated/changed?我说在解析函数中我需要首先通过 id 在数据库中找到相关对象,然后返回已更新/更改的任何 arg 的 args 是否正确? I'm not really sure how to find an object by id on MongoDB though.不过,我不太确定如何在 MongoDB 上通过 id 查找对象。

Any pointers would definitely be appreciated.任何指针肯定会受到赞赏。

Found what I was looking for.找到了我要找的东西。

editStudent: {
      type: StudentType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID)},
        name: { type: GraphQLString },
        age: { type: GraphQLString },
        test1: { type: GraphQLString },
        classId: { type: GraphQLID },
      },
      resolve(parent, args) {
        return Student.findOneAndUpdate({_id: args.id}, { test1: args.test1, name: args.name}, { new: true})
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM