SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1) declare dependencies
  2. const graphql = require("graphql");
  3. const {
  4.     GraphQLObjectType,
  5.     GraphQLString,
  6.     GraphQLID,
  7.     GraphQLBoolean,
  8.     GraphQLList,
  9.     GraphQLSchema,
  10.     GraphQLNonNull
  11. } = graphql;
  12. const graphqlISODate = require ("graphql-iso-date");
  13. const{
  14.     GraphQLDateTime
  15. } = graphqlISODate;
  16.  
  17. //2) declare mongoose models
  18. const Member = require("./models/member");
  19.  
  20. //3) create types and update GraphQL classes in #1
  21. //to define and model our records from MongoDB
  22. const MemberType = new GraphQLObjectType({
  23.     name: "Member",
  24.     fields: () => ({
  25.         id: { type: GraphQLID },
  26.         firstName: { type: GraphQLString },
  27.         lastName: { type: GraphQLString },
  28.         position: { type: GraphQLString },
  29.         createdAt: { type: GraphQLDateTime },
  30.         updatedAt: { type: GraphQLDateTime}
  31.     })
  32. });
  33.  
  34. //4) create entry point (i..e., index, show)
  35. /*
  36.     field -defines the possible queries
  37. */
  38. const RootQuery = new GraphQLObjectType({
  39.     name: "Query",
  40.     fields: {
  41.         members: {
  42.             type: new GraphQLList(MemberType),
  43.             resolve:(parent, args)=> {
  44.                 return Member.find({});
  45.             }
  46.         },
  47.         member: {
  48.             type: MemberType,
  49.             args: {
  50.                 id: { type: GraphQLID }
  51.             },
  52.             resolve: (parent, args) => {
  53.                 return Member.findById(args.id);
  54.             }
  55.         },
  56.  
  57.     }
  58. });
  59. //6) create mutations (i.e., store, update, destroy, etc)
  60. const Mutation = new GraphQLObjectType({
  61.     name: "Mutation",
  62.     fields: {
  63.  
  64.         storeMember: {
  65.             type: MemberType,
  66.             args: {
  67.                 firstName: { type: new GraphQLNonNull(GraphQLString) },
  68.                 lastName: { type: new GraphQLNonNull(GraphQLString) },
  69.                 position: { type: new GraphQLNonNull(GraphQLString) }
  70.             },
  71.             resolve: (parent, args) => {
  72.                 let member = new Member({
  73.                     firstName: args.firstName,
  74.                     lastName: args.lastName,
  75.                     position: args.position
  76.                 });
  77.                 return member.save();
  78.             }
  79.         },
  80.         updateMember: {
  81.             type: MemberType,
  82.             args: {
  83.                 id: { type: new GraphQLNonNull(GraphQLID) },
  84.                 firstName: { type: GraphQLString },
  85.                 lastName: { type: GraphQLString },
  86.                 position: { type: GraphQLString}
  87.             },
  88.             resolve: (parent, args) => {
  89.                 let condition = { _id: args.id };
  90.                 let updates = {
  91.                     firstName: args.firstName,
  92.                     lastName: args.lastName,
  93.                     position: args.position
  94.                 }
  95.                 return Member.findOneAndUpdate(condition, updates, (member) => {
  96.                 return member;
  97.                 })
  98.             }
  99.         },
  100.         destroyMember: {
  101.             type: MemberType,
  102.             args: {
  103.                 id: { type: new GraphQLNonNull(GraphQLID) }
  104.             },
  105.             resolve: (parent, args) => {
  106.                 let condition = { _id: args.id };
  107.                 return Member.findByIdAndRemove(condition);
  108.             }
  109.         },
  110. });
  111.  
  112.     //5) export GraphQL schema
  113. module.exports = new GraphQLSchema({
  114.     query: RootQuery,
  115.     mutation:  Mutation
  116. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top