Skip to content
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

Closed
annymosse opened this issue Oct 27, 2018 · 1 comment
Closed

Quetion : how to access the parent in resolvers? #14

annymosse opened this issue Oct 27, 2018 · 1 comment

Comments

@annymosse
Copy link

how to access the parent in resolvers?

@annymosse
Copy link
Author

annymosse commented Mar 25, 2020

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]
  # ...
}

@annymosse annymosse changed the title quetion Quetion : how to access the parent in resolvers? Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant