Comments (1)
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)
- Authentication HOT 3
- Request more documentations HOT 3
- JS syntax errors in resolvers can be hard to track down HOT 2
- Fatal error when graphql.options is undefined HOT 1
- Wrong in naming schema file HOT 1
- Adding middleware HOT 3
- Is this for apollo server 1 or 2? HOT 1
- npm repo must be updated
- EBADCSRFTOKEN: Invalid CSRF token HOT 1
- Error: Specified query type "Query" not found in document. HOT 13
- Upload Unknown type "Upload". Did you mean "Float"? HOT 1
- Receiving unexpected "[Object: null prototype]" HOT 1
- Error: Specified query type "Query" not found in document. HOT 1
- Security questions.
- [Feature request]: file upload scalar HOT 1
- Feature: GraphQL Subscriptions HOT 7
- Question: how can I enable autorefresh server when changed *.graphql file? HOT 2
- Wrong file extension when make schema HOT 2
- Creating own graphql directive HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adonis-graphql.