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 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器