Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1) declare dependencies
- const graphql = require("graphql");
- const {
- GraphQLObjectType,
- GraphQLString,
- GraphQLID,
- GraphQLBoolean,
- GraphQLList,
- GraphQLSchema,
- GraphQLNonNull
- } = graphql;
- const graphqlISODate = require ("graphql-iso-date");
- const{
- GraphQLDateTime
- } = graphqlISODate;
- //2) declare mongoose models
- const Member = require("./models/member");
- //3) create types and update GraphQL classes in #1
- //to define and model our records from MongoDB
- const MemberType = new GraphQLObjectType({
- name: "Member",
- fields: () => ({
- id: { type: GraphQLID },
- firstName: { type: GraphQLString },
- lastName: { type: GraphQLString },
- position: { type: GraphQLString },
- createdAt: { type: GraphQLDateTime },
- updatedAt: { type: GraphQLDateTime}
- })
- });
- //4) create entry point (i..e., index, show)
- /*
- field -defines the possible queries
- */
- const RootQuery = new GraphQLObjectType({
- name: "Query",
- fields: {
- members: {
- type: new GraphQLList(MemberType),
- resolve:(parent, args)=> {
- return Member.find({});
- }
- },
- member: {
- type: MemberType,
- args: {
- id: { type: GraphQLID }
- },
- resolve: (parent, args) => {
- return Member.findById(args.id);
- }
- },
- }
- });
- //6) create mutations (i.e., store, update, destroy, etc)
- const Mutation = new GraphQLObjectType({
- name: "Mutation",
- fields: {
- storeMember: {
- type: MemberType,
- args: {
- firstName: { type: new GraphQLNonNull(GraphQLString) },
- lastName: { type: new GraphQLNonNull(GraphQLString) },
- position: { type: new GraphQLNonNull(GraphQLString) }
- },
- resolve: (parent, args) => {
- let member = new Member({
- firstName: args.firstName,
- lastName: args.lastName,
- position: args.position
- });
- return member.save();
- }
- },
- updateMember: {
- type: MemberType,
- args: {
- id: { type: new GraphQLNonNull(GraphQLID) },
- firstName: { type: GraphQLString },
- lastName: { type: GraphQLString },
- position: { type: GraphQLString}
- },
- resolve: (parent, args) => {
- let condition = { _id: args.id };
- let updates = {
- firstName: args.firstName,
- lastName: args.lastName,
- position: args.position
- }
- return Member.findOneAndUpdate(condition, updates, (member) => {
- return member;
- })
- }
- },
- destroyMember: {
- type: MemberType,
- args: {
- id: { type: new GraphQLNonNull(GraphQLID) }
- },
- resolve: (parent, args) => {
- let condition = { _id: args.id };
- return Member.findByIdAndRemove(condition);
- }
- },
- });
- //5) export GraphQL schema
- module.exports = new GraphQLSchema({
- query: RootQuery,
- mutation: Mutation
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement