grpc/protobuffer请求特定字段 [英] grpc/protobuffer ask for specific fields

查看:93
本文介绍了grpc/protobuffer请求特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GraphQL允许您要求特定的字段,响应仅包含您要求的字段.例如:一个graphql查询,例如:

GraphQL lets you ask for specific fields, the response contains only the fields that you had asked for. For example: a graphql query like:

{
  hero {
    name
  }
}

将返回:

{
  "data": {
    "hero": {
      "name": "R2-D2"
    }
  }
}

其中的graphQl查询如下:

where as a graphQl query like:

{
  hero {
    name
    
    friends {
      name
    }
  }
}

将返回:

{
  "data": {
    "hero": {
      "name": "R2-D2",
      "friends": [
        {
          "name": "Luke"
        },
        {
          "name": "Han Solo"
        },
        {
          "name": "Leia"
        }
      ]
    }
  }
}

在gRPC中是否可以使用类似的机制/库/模式来实现相同的功能?

Is there a similar mechanism/library/pattern that can be used in gRPC to achieve the same?

推荐答案

FieldMask 在protobuf中类似.这是要保留的字段列表,因此第一个示例将是 paths:"hero.name" ,第二个示例将是 paths:["hero.name","hero.friends.name"]] .

FieldMask is similar in protobuf. It is a list of fields to retain, so the first example would be paths: "hero.name" and the second would be paths: ["hero.name", "hero.friends.name"].

它可能最常用于指定在更新中应更改哪些字段.但这同样可以用来指定应返回的字段.

It is probably most frequently used to specify which fields should be changed in an update. But it can equally be used to specify the fields that should be returned.

服务器可以直接处理FieldMask(例如,仅使用SELECT SQL查询中列出的字段),也可以使用

The server can either process the FieldMask directly (e.g., only using the listed fields in a SELECT SQL query), or it can retrieve all the information and filter the result using FieldMaskUtil.merge() to copy just the requested fields into a new proto message to return to the client.

这篇关于grpc/protobuffer请求特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆