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

查看:120
本文介绍了如何以及何时生成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?

提出了三个建议:

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

并且推荐第三个.但是我不确定在哪里以及如何实现它.另外,为什么graphql ID设为String?答案可能会影响该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在涉及互操作性时是特定的,但在其他方面则通常是通用的,以适应不同的用例.如果您查看链接到的 answer 中GraphQL规范的引用部分,您会发现它与有关数据的通信方式("ID类型与字符串的序列化方式相同"),而对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都这样做的方式.)在不了解您的应用程序的情况下,我不能对 where 进行过多的序列化说明,但这是如何,您可以对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天全站免登陆