fieldName:(root, args, context, info) => { result }
//resolver function with no parameters and returning string greeting:() => { return "hello from TutorialsPoint !!!" } //resolver function with no parameters and returning list students:() => db.students.list() //resolver function with arguments and returning object studentById:(root,args,context,info) => { return db.students.get(args.id); }
Sr.No. | 参数和描述 |
1 |
root
包含从父字段上的解析器返回的结果的对象。
|
2 |
args
带有传递到查询字段中的参数的对象。
|
3 |
context
这是一个特定查询中所有解析器共享的对象。
|
4 |
info
它包含有关查询执行状态的信息,包括字段名称、从根到字段的路径。
|
Sr.No. | 参数和描述 |
1 |
null 或 undefined
这表示找不到对象
|
2 |
array
这仅在架构指示字段的结果应为列表时有效
|
3 |
promise
解析器通常会执行异步操作,例如从数据库或后端 API 中获取数据,因此它们可以返回承诺
|
4 |
scalar or object
解析器也可以返回其他值
|
type Query { greeting:String students:[Student] studentById(id:ID!):Student } type Student { id:ID! firstName:String lastName:String password:String collegeId:String }
const db = require('./db') const Query = { //resolver function for greeting greeting:() => { return "hello from TutorialsPoint !!!" }, //resolver function for students returns list students:() => db.students.list(), //resolver function for studentbyId studentById:(root,args,context,info) => { //args will contain parameter passed in query return db.students.get(args.id); } } module.exports = {Query}
{
studentById(id:"S1001") {
id
firstName
lastName
}
}
{ "data": { "studentById": { "id": "S1001", "firstName": "Mohtashim", "lastName": "Mohammad" } } }