Simple Express GraphQL积垢

Simple Express GraphQL积垢,express,mongoose,graphql,Express,Mongoose,Graphql,我是GraphQL新手。我使用一个简单的CRUD样板来理解这些概念。除更新外,所有功能都正常工作。 文档放置在远程MongoDB云中。我可以查询文档列表,创建一个新的文档,然后按id删除。问题只是更新。 可能解析程序或模式中有错误? 谢谢大家 下面是模式代码(放在schema.js中): 解析程序(放置在resolvers.js中): 为了便于说明,我放了一个app.js代码: const express = require('express'); const bodyParser = requ

我是GraphQL新手。我使用一个简单的CRUD样板来理解这些概念。除更新外,所有功能都正常工作。 文档放置在远程MongoDB云中。我可以查询文档列表,创建一个新的文档,然后按id删除。问题只是更新。 可能解析程序或模式中有错误? 谢谢大家

下面是模式代码(放在schema.js中):

解析程序(放置在resolvers.js中):

为了便于说明,我放了一个app.js代码:

const express = require('express');
const bodyParser = require('body-parser');
const graphqlHttp = require('express-graphql');
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv/config');

const graphQlSchema = require('./schema');
const graphQlResolvers = require('./resolvers');

const app = express();

app.use(bodyParser.json());
app.use(cors());

app.use(
  '/graphql',
  graphqlHttp({
    schema: graphQlSchema,
    rootValue: graphQlResolvers,
    graphiql: true
  })
);

mongoose.connect(
  `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0-vuauc.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`,
  { useNewUrlParser: true }
).then(() => {
  console.log('Connection to database established...')
  app.listen(5555);
}).catch(err => {
  console.log(err);
});
在这里,newHero将返回更新的文档,假设“hero_id”是动态的

hero.title = args.heroInput.title;
hero.description = args.heroInput.description;
不需要像英雄。title英雄。description,只需传递所需的新参数,如

{title: 'new_title'}

我编辑了我的代码并检查了它的工作原理。如果有人感兴趣,请参阅下面更新的模式和解析器

以下是模式:

const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type Hero {
  _id: ID!
  title: String!
  description: String
  date: String!
}
input HeroInput {
  title: String!
  description: String!
  date: String!
}
input HeroUpdate {
  _id: ID!
  title: String!
  description: String
  date: String!
} 
input HeroRemove {
  _id: ID! 
} 
type RootQuery {
    heroes: [Hero!]!
}
type RootMutation {
    createHero(heroInput: HeroInput): Hero
    deleteHero(heroRemove: HeroRemove): Hero
    updateHero(heroUpdate: HeroUpdate): Hero
}
schema {
    query: RootQuery
    mutation: RootMutation
}
`);
解析程序:

const Hero = require('./models/hero');

module.exports = {
  heroes: () => {
    return Hero.find()
      .then(heroes => {
        return heroes.map(hero => {
          return { ...hero._doc, _id: hero.id };
        });
      })
      .catch(err => {
        throw err;
      });
  },
  createHero: args => {
    const hero = new Hero({
      title: args.heroInput.title,
      description: args.heroInput.description,
      date: new Date(args.heroInput.date)
    });
    return hero
      .save()
      .then(result => {
        console.log(result);
        return { ...result._doc, _id: result._doc._id.toString() };
      })
      .catch(err => {
        console.log(err);
        throw err;
      });
  },
  deleteHero: async (args) => {
    try {
      const hero = await Hero.findById(args.heroRemove._id);
      hero.remove();
    } catch (err) {
      throw err;
    }
  },
  updateHero: async (args) => {
    try {
      const newHero = await Hero.findByIdAndUpdate(
        args.heroUpdate._id,
        {
          title: args.heroUpdate.title,
          description: args.heroUpdate.description,
          date: new Date(args.heroUpdate.date)
        },
        { new: true }
      );
      return newHero;
    } catch (err) {
      console.log(err);
      throw err;
    }
  }
};

谢谢我真的很喜欢GraphQL
{title: 'new_title'}
const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type Hero {
  _id: ID!
  title: String!
  description: String
  date: String!
}
input HeroInput {
  title: String!
  description: String!
  date: String!
}
input HeroUpdate {
  _id: ID!
  title: String!
  description: String
  date: String!
} 
input HeroRemove {
  _id: ID! 
} 
type RootQuery {
    heroes: [Hero!]!
}
type RootMutation {
    createHero(heroInput: HeroInput): Hero
    deleteHero(heroRemove: HeroRemove): Hero
    updateHero(heroUpdate: HeroUpdate): Hero
}
schema {
    query: RootQuery
    mutation: RootMutation
}
`);
const Hero = require('./models/hero');

module.exports = {
  heroes: () => {
    return Hero.find()
      .then(heroes => {
        return heroes.map(hero => {
          return { ...hero._doc, _id: hero.id };
        });
      })
      .catch(err => {
        throw err;
      });
  },
  createHero: args => {
    const hero = new Hero({
      title: args.heroInput.title,
      description: args.heroInput.description,
      date: new Date(args.heroInput.date)
    });
    return hero
      .save()
      .then(result => {
        console.log(result);
        return { ...result._doc, _id: result._doc._id.toString() };
      })
      .catch(err => {
        console.log(err);
        throw err;
      });
  },
  deleteHero: async (args) => {
    try {
      const hero = await Hero.findById(args.heroRemove._id);
      hero.remove();
    } catch (err) {
      throw err;
    }
  },
  updateHero: async (args) => {
    try {
      const newHero = await Hero.findByIdAndUpdate(
        args.heroUpdate._id,
        {
          title: args.heroUpdate.title,
          description: args.heroUpdate.description,
          date: new Date(args.heroUpdate.date)
        },
        { new: true }
      );
      return newHero;
    } catch (err) {
      console.log(err);
      throw err;
    }
  }
};