Giter Club home page Giter Club logo

Comments (1)

annymosse avatar annymosse commented on May 18, 2024

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

from adonis-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.