Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   GraphQLID as ID,
  3.   GraphQLString as StringType,
  4.   GraphQLList as List,
  5.     GraphQLNonNull,
  6. } from 'graphql';
  7. import User from '../models/User';
  8. import SopStep from '../models/SopStep';
  9. import UserType from '../types/UserType';
  10. import SopStepInputType from '../types/SopStepInputType';
  11.  
  12. const upsertUser = {
  13.   type: UserType,
  14.   args: {
  15.     id: { type: ID },
  16.     email: { type: new GraphQLNonNull(StringType) },
  17.     steps: { type: new List(SopStepInputType) },
  18.   },
  19.   async resolve(...params) {
  20.     const args = params[1];
  21.     // this function takes ({request}, args)
  22.     // request is the http reqest object from express
  23.     // the object its in is the root object defined when graphql is started
  24.     // args is the arguments to the mutator from graphql
  25.     // they are defined in the args field of this object
  26.  
  27.     // this is will return the user object it creates
  28.     // if no id is provided it will create a new record
  29.     // it should
  30.  
  31.         await User.upsert({
  32.             ...(args.email && {email: args.email}),
  33.             ...(args.id && {id: args.id})
  34.         });
  35.        
  36.        
  37.       const data = await User.findOne({
  38.             where: {email: args.email},
  39.         });
  40.        
  41.         let UserID = data.get('id');
  42.         console.log(args.steps)
  43.  
  44.         if (args.steps) {
  45.             await args.steps.map ( e => SopStep.upsert({
  46.                 ...(e.id && e.id: e.id),
  47.                 ...(e.key && e.key: e.key),
  48.                 ...(e.title && e.title: e.title),
  49.                 })
  50.             )
  51.  
  52.             await args.steps.map ( e => SopStep.findOne({ where: { id: e.id } }).setUser(UserID)
  53.             )
  54.         }
  55.            
  56.         return { email: args.email, id: data.get('id') }
  57.   },
  58. };
  59.  
  60. export default upsertUser;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement