duanhuan1147 2019-06-02 07:01
浏览 313
已采纳

我应该为每个查询或其在GraphQL中的所有字段创建解析器吗?

I'm interested in using GraphQL and I've just started experimenting with it.

In a GraphQL tutorial, one can see the following quote:

Each field in a GraphQL schema is backed by a resolver.

But if you look at gqlgen (which is a golang library for building GraphQL servers) todo example that uses the following schema:

...
type MyQuery {
    todo(id: ID!): Todo
    lastTodo: Todo
    todos: [Todo!]!
}

type MyMutation {
    createTodo(todo: TodoInput!): Todo!
    updateTodo(id: ID!, changes: Map!): Todo
}

type Todo {
    id: ID!
    text: String!
    done: Boolean!
}
...

it actually uses 3 autogenerated resolvers (i.e., 1 for each query, not a field):

func (r *QueryResolver) Todo(ctx context.Context, id int) (*Todo, error) {
func (r *QueryResolver) LastTodo(ctx context.Context) (*Todo, error) {
func (r *QueryResolver) Todos(ctx context.Context) ([]*Todo, error) {

Is it the expected behavior not to autogenerate resolvers for each field (but for each query instead)?

  • 写回答

1条回答 默认 最新

  • doubo1711 2019-06-02 08:00
    关注

    When entire type is backed by a resolver then it's still true that each field is backed by resolver ;)

    Comparing to SQL - usually you don't ask for each (single) field separately when you want entire record/row.

    Field level resolvers are needed when type resolver don't return data for all required (by query) fields - f.e. for relations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了