如何以及何时为 graphql 生成 ID? [英] How and when to generate an ID for graphql?

查看:47
本文介绍了如何以及何时为 graphql 生成 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 graphql 与 SQLite 数据库连接.

I am connecting graphql with an SQLite database.

在 sql 中,id 是整数,但在 graphql 中,id 是字符串.

In sql, ids are integers but in graphql ids are strings.

搜索后,基于这个问题——何时使用GraphQLID而不是GraphQLInt?

After searching, based on this question - When to use GraphQLID instead of GraphQLInt?

提出三个建议:

  • 开始对 PK 使用 UUID.但是,这会影响性能.
  • 忽略在整个应用程序中具有唯一 ID 的隐式要求(源自于 relayjs 生态系统),并在可能的情况下将 ID 转换为数字以供内部使用.
  • 应用程序数据层上的哈希编码 ID,例如UUID base64 派生自表名 & 的串联PK值.

推荐第三个.但我不确定在哪里以及如何实施它.另外,为什么graphql ID 是一个字符串?答案可能会影响该 ID 部分的实现方式.

And it's recommended the third. But i'm not sure where and how to implement it. Also, why is graphql ID made a String? The answer can affect how this ID part should be implemented.

推荐答案

为什么 GraphQL ID 序列化与字符串相同?

作为 API 的数据查询语言,希望 GraphQL 在涉及互操作性的情况下是特定的,但在其他方面是通用的,以适应不同的用例.如果您查看 您链接到的答案 中引用的 GraphQL 规范部分,您会发现它特定于 数据是如何传递的(ID 类型的序列化方式与 String 相同"),同时不具体说明 ID 类型的内容是什么(只说它是什么)代表一个唯一标识符",通常是数字",但并非总是如此,没有详细说明).将 ID 类型序列化为字符串是提供其他未指定标识符数据通信的最直接方式.

Why are GraphQL IDs serialized the same as strings?

As a data query language for APIs, it is desirable that GraphQL be specific where interoperability is concerned, but general otherwise to accommodate differing use cases. If you look at the quoted parts of the GraphQL specification in the answer you linked to, you can see it is specific about how data is communicated ("The ID type is serialized the same way as a String"), while being non-specific about what the content of the ID type is (saying only that it "represents a unique identifier", "often numeric" but not always, without going into detail). Making the ID type serialize as a string is the most straightforward way of providing for the communication of otherwise unspecified identifier data.

因此,虽然您确实需要将 ID 序列化为字符串,但这些 ID 以何种形式进行预序列化取决于您,以最适合您的应用程序.

So while you do need to serialize your IDs as strings, what form those IDs take pre-serialization is up to you, as best fits your application.

如果您要使用 Relay.js 之类的东西,其中 ID 需要全局唯一,那么我建议使用第三种方法,连接表名和主键,然后对结果进行 Base64 编码.(这就是 GraphQL.js 和 Graphene 的做法.)在不了解您的应用程序的更多信息的情况下,我不能说您应该在哪里进行序列化,但这里有几个示例 如何对 ID 进行编码:

If you're going to be talking to something like Relay.js, where the IDs need to be globally unique, then I would suggest the third method, of concatenating the table name and primary key, then Base64 encoding the result. (This is how both GraphQL.js and Graphene do it.) Without knowing more about your application, I can't say much about where you should do the serialization, but here are a couple examples of how you might encode IDs:

# Node.js
var table_name = 'MyTable';
var primary_key = 1234;
var serialized_id = Buffer.from(table_name + ':' + primary_key).toString('base64');

# Python 3
import base64
table_name = 'MyTable'
primary_key = 1234
serialized_id = base64.b64encode((table_name + ':' + str(primary_key)).encode()).decode()

以及如何解码它们:

# Node.js
var serialized_id = 'TXlUYWJsZToxMjM0';
var [table_name, primary_key] = Buffer.from(serialized_id, 'base64').toString('ascii').split(':');

# Python 3
serialized_id = 'TXlUYWJsZToxMjM0'
(table_name, primary_key) = base64.b64decode(serialized_id.encode()).decode().split(':')

希望有帮助!

这篇关于如何以及何时为 graphql 生成 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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