我应该在客户端将 GraphQL ID 作为字符串处理吗?

Should I handle a GraphQL ID as a string on the client?(我应该在客户端将 GraphQL ID 作为字符串处理吗?)

本文介绍了我应该在客户端将 GraphQL ID 作为字符串处理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法构建应用程序:

I am building an application using:

  • MySQL 作为后端数据库
  • Apollo GraphQL 服务器作为该数据库的查询层
  • Sequelize 作为 GraphQL 和 MySQL 之间的 ORM 层

在构建 GraphQL 架构时,我使用 GraphQL ID 数据类型来唯一标识记录.这是一个示例架构及其 MySQL 解析器/连接器

As I am building out my GraphQL schema I'm using the GraphQL ID data type to uniquely identify records. Here's an example schema and its MySQL resolver/connector

Graphql 类型:

type Person {
  id: ID!
  firstName: String
  middleName: String
  lastName: String
  createdAt: String
  updatedAt: String
}

续集连接器

export const Person = sequelize.define('person', {
  firstName: { type: Sequelize.STRING },
  middleName: { type: Sequelize.STRING },
  lastName: { type: Sequelize.STRING },
});

GraphQL 解析器:

Query: {
  person(_, args) {
    return Person.findById(args.id);
}

这样一切正常.这是我的问题.GraphQL 似乎将 ID 类型视为字符串.虽然 ID 值被 Sequelize 作为 INT 存储在 MySQL 数据库中.我可以使用 GraphQL 使用与数据库中的 ID 值匹配的字符串或整数来查询 MySQL 数据库.但是,GraphQL 将始终以字符串形式返回 ID 值.

So that all works. Here's my question. GraphQL seems to treat the ID type as a string. While the ID value gets stored in the MySQL database as an INT by Sequelize. I can use GraphQL to query the MySQL db with either a string or a integer that matches the ID value in the database. However, GraphQL will always return the ID value as a string.

我应该如何在客户端处理这个值?我是否应该在从 GraphQL 获取它后立即将其转换为整数?我应该修改我的续集代码以将 ID 值存储为字符串吗?像这样使用 GraphQL ID 时,是否有正确的处理方法?

How should I be handling this value in the client? Should I always convert it to an integer as soon as I get it from GraphQL? Should I modify my sequelize code to store the ID value as a string? Is there a correct way to proceed when using GraphQL IDs like this?

推荐答案

ID 是 GraphQL 规范(2016 年 10 月工作草案):

ID is a scalar type described in the GraphQL specification (working draft October 2016):

ID 类型的序列化方式与 String 相同;然而,它并不是人类可读的.虽然它通常是数字,但它应该始终序列化为字符串.

The ID type is serialized in the same way as a String; however, it is not intended to be human‐readable. While it is often numeric, it should always serialize as a String.


你的观察


Your observation

我可以使用 GraphQL 使用与数据库中的 ID 值匹配的字符串或整数来查询 MySQL 数据库.但是,GraphQL 将始终以字符串形式返回 ID 值.

I can use GraphQL to query the MySQL db with either a string or a integer that matches the ID value in the database. However, GraphQL will always return the ID value as a string.

符合关于结果强制的规范:

GraphQL 与 ID 格式无关,并序列化为字符串以确保 ID 可以表示的多种格式的一致性

GraphQL is agnostic to ID format, and serializes to string to ensure consistency across many formats ID could represent

输入强制:

当期望作为输入类型时,任何字符串(例如4")或整数(例如 4)输入值都应根据给定的 GraphQL 服务器期望的 ID 格式强制转换为 ID

When expected as an input type, any string (such as "4") or integer (such as 4) input value should be coerced to ID as appropriate for the ID formats a given GraphQL server expects


我应该如何在客户端处理这个值?

  • 使用 ID results 时,将它们视为字符串.
  • 当使用 ID inputs 时(在 GraphQL 变量或用于突变或查询的输入参数中),您可以使用整数或字符串.
  • When working with ID results, treat them as strings.
  • When using ID inputs (in GraphQL variables or input parameters for mutations or queries), you can either use integers or strings.

我是否应该在从 GraphQL 中获取后立即将其转换为整数?

这在很大程度上取决于您的应用程序.没有明确规定是"的一般规则.或否"在这里.

That's highly depending on your application. There is no general rule that dictates a clear "yes" or "no" here.

我是否应该修改我的 sequelize 代码以将 ID 值存储为字符串?

不,这不是必需的.

关于 ID 类型的 GraphQL 规范没有涵盖你如何存储id,只涵盖了 GraphQL 服务器应该如何处理 ID输入和输出.由 GraphQL 层来确保这种行为.实际存储中如何处理 id 由存储层决定.

The GraphQL specification about the ID type does not cover how you store the ids, only how a GraphQL server is expected to treat ID input and output. It's up to the GraphQL layer to ensure this behaviour. How ids are handled in the actual storage is up to the storage layer.

在使用像这样的 GraphQL ID 时,是否有正确的处理方法?

我希望上面的答案也回答了这个问题:)

I hope the above answers also answer this question :)

这篇关于我应该在客户端将 GraphQL ID 作为字符串处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我应该在客户端将 GraphQL ID 作为字符串处理吗?

基础教程推荐