1. <tfoot id='bzQxJ'></tfoot>

      <small id='bzQxJ'></small><noframes id='bzQxJ'>

      1. <i id='bzQxJ'><tr id='bzQxJ'><dt id='bzQxJ'><q id='bzQxJ'><span id='bzQxJ'><b id='bzQxJ'><form id='bzQxJ'><ins id='bzQxJ'></ins><ul id='bzQxJ'></ul><sub id='bzQxJ'></sub></form><legend id='bzQxJ'></legend><bdo id='bzQxJ'><pre id='bzQxJ'><center id='bzQxJ'></center></pre></bdo></b><th id='bzQxJ'></th></span></q></dt></tr></i><div id='bzQxJ'><tfoot id='bzQxJ'></tfoot><dl id='bzQxJ'><fieldset id='bzQxJ'></fieldset></dl></div>
          <bdo id='bzQxJ'></bdo><ul id='bzQxJ'></ul>
        <legend id='bzQxJ'><style id='bzQxJ'><dir id='bzQxJ'><q id='bzQxJ'></q></dir></style></legend>

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

        时间:2023-08-20

          <tfoot id='OY0Ar'></tfoot>
          <legend id='OY0Ar'><style id='OY0Ar'><dir id='OY0Ar'><q id='OY0Ar'></q></dir></style></legend>
            <tbody id='OY0Ar'></tbody>
                <bdo id='OY0Ar'></bdo><ul id='OY0Ar'></ul>
                <i id='OY0Ar'><tr id='OY0Ar'><dt id='OY0Ar'><q id='OY0Ar'><span id='OY0Ar'><b id='OY0Ar'><form id='OY0Ar'><ins id='OY0Ar'></ins><ul id='OY0Ar'></ul><sub id='OY0Ar'></sub></form><legend id='OY0Ar'></legend><bdo id='OY0Ar'><pre id='OY0Ar'><center id='OY0Ar'></center></pre></bdo></b><th id='OY0Ar'></th></span></q></dt></tr></i><div id='OY0Ar'><tfoot id='OY0Ar'></tfoot><dl id='OY0Ar'><fieldset id='OY0Ar'></fieldset></dl></div>

              • <small id='OY0Ar'></small><noframes id='OY0Ar'>

                  本文介绍了我应该在客户端将 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 作为字符串处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python:Django:如何根据 order_id 在 order_data 中获取 下一篇:SQLite Titanium 语法错误

                  相关文章

                  最新文章

                  • <bdo id='MxwX5'></bdo><ul id='MxwX5'></ul>
                  1. <legend id='MxwX5'><style id='MxwX5'><dir id='MxwX5'><q id='MxwX5'></q></dir></style></legend>
                  2. <i id='MxwX5'><tr id='MxwX5'><dt id='MxwX5'><q id='MxwX5'><span id='MxwX5'><b id='MxwX5'><form id='MxwX5'><ins id='MxwX5'></ins><ul id='MxwX5'></ul><sub id='MxwX5'></sub></form><legend id='MxwX5'></legend><bdo id='MxwX5'><pre id='MxwX5'><center id='MxwX5'></center></pre></bdo></b><th id='MxwX5'></th></span></q></dt></tr></i><div id='MxwX5'><tfoot id='MxwX5'></tfoot><dl id='MxwX5'><fieldset id='MxwX5'></fieldset></dl></div>

                    <small id='MxwX5'></small><noframes id='MxwX5'>

                    <tfoot id='MxwX5'></tfoot>