Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- GraphQLID as ID,
- GraphQLString as StringType,
- GraphQLList as List,
- GraphQLNonNull,
- } from 'graphql';
- import User from '../models/User';
- import SopStep from '../models/SopStep';
- import UserType from '../types/UserType';
- import SopStepInputType from '../types/SopStepInputType';
- const upsertUser = {
- type: UserType,
- args: {
- id: { type: ID },
- email: { type: new GraphQLNonNull(StringType) },
- steps: { type: new List(SopStepInputType) },
- },
- async resolve(...params) {
- const args = params[1];
- // this function takes ({request}, args)
- // request is the http reqest object from express
- // the object its in is the root object defined when graphql is started
- // args is the arguments to the mutator from graphql
- // they are defined in the args field of this object
- // this is will return the user object it creates
- // if no id is provided it will create a new record
- // it should
- await User.upsert({
- ...(args.email && {email: args.email}),
- ...(args.id && {id: args.id})
- });
- const data = await User.findOne({
- where: {email: args.email},
- });
- let UserID = data.get('id');
- console.log(args.steps)
- if (args.steps) {
- await args.steps.map ( e => SopStep.upsert({
- ...(e.id && e.id: e.id),
- ...(e.key && e.key: e.key),
- ...(e.title && e.title: e.title),
- })
- )
- await args.steps.map ( e => SopStep.findOne({ where: { id: e.id } }).setUser(UserID)
- )
- }
- return { email: args.email, id: data.get('id') }
- },
- };
- export default upsertUser;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement