Facebook
From gql-schema.js, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 193
  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. const Task = require("./models/task");
  20. const Team = require("./models/team");
  21.  
  22. //3) create types and update GraphQL classes in #1
  23. //to define and model our records from MongoDB
  24. const TeamType = new GraphQLObjectType({
  25.     name: "Team",
  26.     fields: () => ({
  27.         id: { type: GraphQLID },
  28.         name: { type: GraphQLString },
  29.         createdAt: { type: GraphQLDateTime },
  30.         updatedAt: { type: GraphQLDateTime},
  31.         tasks: {
  32.             type: new GraphQLList(TaskType),
  33.             resolve: (parent, args) => {
  34.                 return Task.find({teamId: parent.id })
  35.             }
  36.         }
  37.     })
  38. });
  39.  
  40. const TaskType = new GraphQLObjectType({
  41.     name: "Task",
  42.     fields: () => ({
  43.         id: { type: GraphQLID },
  44.         description: { type: GraphQLString },
  45.         teamId: { type: GraphQLString },
  46.         isCompleted: { type: GraphQLBoolean },
  47.         createdAt: { type: GraphQLDateTime },
  48.         updatedAt: { type: GraphQLDateTime}
  49.     })
  50. });
  51.  
  52. const MemberType = new GraphQLObjectType({
  53.     name: "Member",
  54.     fields: () => ({
  55.         id: { type: GraphQLID },
  56.         teamId: { type: GraphQLString },
  57.         firstName: { type: GraphQLString },
  58.         lastName: { type: GraphQLString },
  59.         position: { type: GraphQLString },
  60.         createdAt: { type: GraphQLDateTime },
  61.         updatedAt: { type: GraphQLDateTime}
  62.     })
  63. });
  64.  
  65. //4) create entry point (i..e., index, show)
  66. /*
  67.     field -defines the possible queries
  68. */
  69. const RootQuery = new GraphQLObjectType({
  70.     name: "Query",
  71.     fields: {
  72.         teams: {
  73.             type: new GraphQLList(TeamType),
  74.             resolve: (parent, args) => {
  75.                 return Team.find({});
  76.             }
  77.         },
  78.         team: {
  79.             type: TeamType,
  80.             args: {
  81.                 id: { type: GraphQLID }
  82.             },
  83.             resolve: (parent, args) => {
  84.                 return Team.findById(args.id);
  85.             }
  86.         },
  87.         tasks:{
  88.             type: new GraphQLList(TaskType),
  89.             resolve: (parent, args) => {
  90.                 return Task.find({});
  91.             }
  92.         },
  93.         task: {
  94.             type: TaskType,
  95.             args: {
  96.                 id: { type: GraphQLID}
  97.             },
  98.             resolve: (parent, args) => {
  99.                 return Task.findById(args.id);
  100.             }
  101.         },
  102.         members: {
  103.             type: new GraphQLList(MemberType),
  104.             resolve:(parent, args)=> {
  105.                 return Member.find({});
  106.             }
  107.         },
  108.         member: {
  109.             type: MemberType,
  110.             args: {
  111.                 id: { type: GraphQLID }
  112.             },
  113.             resolve: (parent, args) => {
  114.                 return Member.findById(args.id);
  115.             }
  116.         },
  117.  
  118.     }
  119. });
  120.  
  121. //6) create mutations (i.e., store, update, destroy, etc)
  122. const Mutation = new GraphQLObjectType({
  123.     name: "Mutation",
  124.     fields: {
  125.         storeTeam: {
  126.             type: TeamType,
  127.             args: {
  128.                 name: { type: GraphQLNonNull(GraphQLString) }
  129.             },
  130.             resolve: (parent, args) => {
  131.                 let team = new Team({
  132.                     name: args.name
  133.                 });
  134.                 return team.save();
  135.             }
  136.         },
  137.         destroyTeam: {
  138.             type: TeamType,
  139.             args: {
  140.                 id: { type: new GraphQLNonNull(GraphQLID) }
  141.             },
  142.             resolve: (parent, args) => {
  143.                 let condition = { _id: args.id };
  144.                 return Team.findByIdAndRemove(condition);
  145.             }
  146.         },
  147.         updateTeam: {
  148.             type: TeamType,
  149.             args: {
  150.                 id: { type: new GraphQLNonNull(GraphQLID) },
  151.                 name: { type: GraphQLNonNull(GraphQLString) }
  152.             },
  153.             resolve: (parent, args) => {
  154.                 let condition = { _id: args.id };
  155.                 let updates = {
  156.                     name: args.name
  157.                 }
  158.                 return Team.findOneAndUpdate(condition, updates, (team) => {
  159.                 return team;
  160.                 })
  161.             }
  162.         },
  163.         updateTask: {
  164.             type: TaskType,
  165.             args: {
  166.                 id: { type: new GraphQLNonNull(GraphQLID) },
  167.                 description: { type: GraphQLString },
  168.                 teamId: { type: GraphQLString },
  169.                 isCompleted: { type: GraphQLBoolean }
  170.             },
  171.             resolve: (parent, args) => {
  172.                 let condition = { _id: args.id };
  173.                 let updates = {
  174.                     description: args.description,
  175.                     teamId: args.teamId,
  176.                     isCompleted: args.isCompleted
  177.                 }
  178.                 return Task.findOneAndUpdate(condition, updates, (task) => {
  179.                 return task;
  180.                 })
  181.             }
  182.            
  183.         },
  184.         storeTask: {
  185.             type: TaskType,
  186.             args: {
  187.                 description: { type: new GraphQLNonNull(GraphQLString) },
  188.                 teamId: { type: new GraphQLNonNull(GraphQLString) },
  189.                 isCompleted: { type: GraphQLBoolean }
  190.             },
  191.             resolve: (parent, args) => {
  192.                 return Team.exists({ _id: args.teamId })
  193.                     .then(()=> {
  194.                         let task = new Task(args);
  195.                         console.log("saved");
  196.                         return task.save();
  197.                     })
  198.                     .catch(error => {
  199.                         console.log(error)
  200.                     });
  201.             }
  202.         },
  203.         storeMember: {
  204.             type: MemberType,
  205.             args: {
  206.                 teamId: { type: new GraphQLNonNull(GraphQLString) },
  207.                 firstName: { type: new GraphQLNonNull(GraphQLString) },
  208.                 lastName: { type: new GraphQLNonNull(GraphQLString) },
  209.                 position: { type: new GraphQLNonNull(GraphQLString) }
  210.             },
  211.             resolve: (parent, args) => {
  212.                 // let member = new Member({
  213.                 //  firstName: args.firstName,
  214.                 //  lastName: args.lastName,
  215.                 //  position: args.position
  216.                 // });
  217.                 let member = new Member(args);
  218.                 return member.save();
  219.             }
  220.         },
  221.         updateMember: {
  222.             type: MemberType,
  223.             args: {
  224.                 id: { type: new GraphQLNonNull(GraphQLID) },
  225.                 teamId: { type: GraphQLString },
  226.                 firstName: { type: GraphQLString },
  227.                 lastName: { type: GraphQLString },
  228.                 position: { type: GraphQLString}
  229.             },
  230.             resolve: (parent, args) => {
  231.                 let condition = { _id: args.id };
  232.                 let updates = {
  233.                     firstName: args.firstName,
  234.                     lastName: args.lastName,
  235.                     position: args.position
  236.                 }
  237.                 // return Member.findOneAndUpdate(condition, updates, (member) => {
  238.                 // return member;
  239.                 return Member.findOneAndUpdate(condition, args, { new: true});
  240.                 }
  241.            
  242.         },
  243.         destroyMember: {
  244.             type: MemberType,
  245.             args: {
  246.                 id: { type: new GraphQLNonNull(GraphQLID) }
  247.             },
  248.             resolve: (parent, args) => {
  249.                 let condition = { _id: args.id };
  250.                 return Member.findByIdAndRemove(condition);
  251.             }
  252.         }
  253.     }
  254. });
  255.  
  256. // Run http://localhost:4000/graphql to access graphiql
  257.  
  258. //5) export GraphQL schema
  259. module.exports = new GraphQLSchema({
  260.     query: RootQuery,
  261.     mutation:  Mutation
  262. });
  263.  
  264.