Last active
May 29, 2020 04:36
-
-
Save Wonder2210/c014ca374ce4d6ccd7e94c4726c96771 to your computer and use it in GitHub Desktop.
resolvers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Pet,User} from '../../database/models'; | |
import {Resolvers} from '../../__generated__/generated-types'; | |
import {UserInputError} from 'apollo-server-express'; | |
const resolvers : Resolvers = { | |
Query:{ | |
pet:async (parent,args,ctx)=>{ | |
const pet:Pet= await Pet.query().findById(args.id); | |
return pet; | |
}, | |
pets: async (parent,args,ctx)=>{ | |
const pets:Pet[]= await Pet.query(); | |
return pets; | |
} | |
}, | |
Pet:{ | |
owner:async(parent,args,ctx)=>{ | |
const owner : User = await Pet.relatedQuery("owner").for(parent.id).first(); | |
return owner; | |
} | |
}, | |
Mutation:{ | |
createPet:async (parent,args,ctx)=>{ | |
let pet: Pet; | |
try { | |
pet = await Pet.query().insert({...args.pet}); | |
} catch (error) { | |
throw new UserInputError("Bad user input fields required",{ | |
invalidArgs: Object.keys(args), | |
}); | |
} | |
return pet; | |
}, | |
updatePet:async (parent,{pet:{id,...data}},ctx)=>{ | |
const pet : Pet = await Pet.query() | |
.patchAndFetchById(id,data); | |
return pet; | |
}, | |
deletePet:async (parent,args,ctx)=>{ | |
const pet = await Pet.query().deleteById(args.id); | |
return "Successfully deleted" | |
}, | |
} | |
} | |
export default resolvers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Resolvers} from '../../__generated__/generated-types'; | |
import {User,Pet} from '../../database/models'; | |
import {UserInputError} from 'apollo-server-express'; | |
interface assertion { | |
[key: string]:string | number ; | |
} | |
type StringIndexed<T> = T & assertion; | |
const resolvers : Resolvers ={ | |
Query:{ | |
users: async (parent,args,ctx)=>{ | |
const users : User[] = await User.query(); | |
return users; | |
}, | |
user:async (parent,args,ctx)=>{ | |
const user :User = await await User.query().findById(args.id); | |
return user; | |
}, | |
}, | |
User:{ | |
pets:async (parent,args,ctx)=>{ | |
const pets : Pet[] = await User.relatedQuery("pets").for(parent.id); | |
return pets; | |
} | |
}, | |
Mutation:{ | |
createUser:async (parent,args,ctx)=>{ | |
let user : User; | |
try { | |
user = await User.query().insert({...args.user}); | |
} catch (error) { | |
console.log(error); | |
throw new UserInputError('Email Invalido', { | |
invalidArgs: Object.keys(args), | |
}); | |
} | |
return user; | |
}, | |
updateUser:async (parent,{user:{id,...data}},ctx)=>{ | |
let user : User = await User.query().patchAndFetchById(id,data); | |
return user; | |
}, | |
deleteUser:async (parent,args,ctx)=>{ | |
const deleted = await User.query().deleteById(args.id); | |
return "Succesfull deleted"; | |
}, | |
} | |
} | |
export default resolvers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment