-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quetion : how to access the parent in resolvers? #14
Comments
reply myself for future references: // app/Resolvers/main.js
const User = use("App/Models/User");
const Post = use("App/Models/Post");
module.exports = {
// The queries are GET the READ part of CRUD
Query: {
/**
* this is the form for all of the Queries & Mutaions functions
*@param {undefined} root Alwyase undefined
*@param {object} arg the arguments which posted by client side
*@param {HttpContext} ctx HTTP Context object :: ctx.request ctx.response ctx.view
*/
hello: (_,arg) => {
return `hello ${arg.msg}`;
},
GetUserByName: async (_, args) => {
try {
return (
await User.query()
.where("name", "like", `%${args.name}%`)
.fetch()
).toJSON();
} catch (err=>err)
}
// the rest of your queries ...
},
// The rest of CRUD except R (Read)
Mutation: {
hello: (root, arg) => `Hello ${arg.user}`,
addUser: async (_, args) => {
try {
return await User.create(args);
} catch (err){
return err
}
},
updateUser: async (_, arg) => {
try {
const user = await User.find(arg.id);
user.fill(arg);
return await user.save();
} catch (err){return err}
},
deleteUser: async (_, arg) => {
try {
const user = await User.find(arg.id);
await user.delete();
return "Done";
} catch (err) {
return err;
}
}
// the rest similar Mutations for your Models Posts , Cars , Books ...
},
// Here we can use root param and the relations bewtween the GQL types
Post:{
// the ${Post} type fields
/**
* @param {object} root the ${Post} fields
* @param {number| string} root.id - Post id
* @param {string} root.body - Post body
* @param {string} root.title - Post title
*/
authors:async root=>{
// return authors related by ${Post} type
try{
// ... your logic to get these authors (you can user Lucid models or your Pure SQL/No-Sql queries)
}catch(err){return err}
}
},
// the rest of types for example you can defined ${User ${Post}s
User:{
posts:async root=>{/* your code logic here*/}
}
} # app/Schema/main.gql
Query{
hello(msg:String!): String!
getUserByName(name:String):User
}
type User{
id:ID
name:String
email:String
posts:[Post]
}
type Post{
id:ID
title:String
body:String
authors:[User]
# ...
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to access the parent in resolvers?
The text was updated successfully, but these errors were encountered: