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

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

问题描述

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

I am building an application using:

  • MySQL作为后端数据库
  • Apollo GraphQL服务器作为该数据库的查询层
  • 将其作为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 类型视为字符串.Sequelize将ID值作为 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

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.

与有关结果强制的规范保持一致:

is aligned with the specification about result coercion:

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

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

输入强制:

当期望作为输入类型时,应将任何字符串(例如"4")或整数(例如4)输入值强制转换为ID,以适合给定GraphQL服务器期望的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 结果时,请将它们视为字符串.
  • 使用 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.

我是否应该修改我的续集代码以将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作为字符串处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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