問題描述
我正在使用以下方法構建應用程序:
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模板網!