問(wèn)題描述
我正在使用以下方法構(gòu)建應(yīng)用程序:
I am building an application using:
- MySQL 作為后端數(shù)據(jù)庫(kù)
- Apollo GraphQL 服務(wù)器作為該數(shù)據(jù)庫(kù)的查詢(xún)層
- Sequelize 作為 GraphQL 和 MySQL 之間的 ORM 層
在構(gòu)建 GraphQL 架構(gòu)時(shí),我使用 GraphQL ID 數(shù)據(jù)類(lèi)型來(lái)唯一標(biāo)識(shí)記錄.這是一個(gè)示例架構(gòu)及其 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 類(lèi)型:
type Person {
id: ID!
firstName: String
middleName: String
lastName: String
createdAt: String
updatedAt: String
}
續(xù)集連接器
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);
}
這樣一切正常.這是我的問(wèn)題.GraphQL 似乎將 ID
類(lèi)型視為字符串.雖然 ID 值被 Sequelize 作為 INT
存儲(chǔ)在 MySQL 數(shù)據(jù)庫(kù)中.我可以使用 GraphQL 使用與數(shù)據(jù)庫(kù)中的 ID 值匹配的字符串或整數(shù)來(lái)查詢(xún) MySQL 數(shù)據(jù)庫(kù).但是,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.
我應(yīng)該如何在客戶(hù)端處理這個(gè)值?我是否應(yīng)該在從 GraphQL 獲取它后立即將其轉(zhuǎn)換為整數(shù)?我應(yīng)該修改我的續(xù)集代碼以將 ID 值存儲(chǔ)為字符串嗎?像這樣使用 GraphQL ID 時(shí),是否有正確的處理方法?
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 規(guī)范(2016 年 10 月工作草案):
ID
is a scalar type described in the GraphQL specification (working draft October 2016):
ID 類(lèi)型的序列化方式與 String 相同;然而,它并不是人類(lèi)可讀的.雖然它通常是數(shù)字,但它應(yīng)該始終序列化為字符串.
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 使用與數(shù)據(jù)庫(kù)中的 ID 值匹配的字符串或整數(shù)來(lái)查詢(xún) MySQL 數(shù)據(jù)庫(kù).但是,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.
符合關(guān)于結(jié)果強(qiáng)制的規(guī)范:
GraphQL 與 ID 格式無(wú)關(guān),并序列化為字符串以確保 ID 可以表示的多種格式的一致性
GraphQL is agnostic to ID format, and serializes to string to ensure consistency across many formats ID could represent
和輸入強(qiáng)制:
當(dāng)期望作為輸入類(lèi)型時(shí),任何字符串(例如4")或整數(shù)(例如 4)輸入值都應(yīng)根據(jù)給定的 GraphQL 服務(wù)器期望的 ID 格式強(qiáng)制轉(zhuǎn)換為 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
我應(yīng)該如何在客戶(hù)端處理這個(gè)值?
- 使用
ID
results 時(shí),將它們視為字符串. - 當(dāng)使用
ID
inputs 時(shí)(在 GraphQL 變量或用于突變或查詢(xún)的輸入?yún)?shù)中),您可以使用整數(shù)或字符串.
- 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.
我是否應(yīng)該在從 GraphQL 中獲取后立即將其轉(zhuǎn)換為整數(shù)?
這在很大程度上取決于您的應(yīng)用程序.沒(méi)有明確規(guī)定是"的一般規(guī)則.或否"在這里.
That's highly depending on your application. There is no general rule that dictates a clear "yes" or "no" here.
我是否應(yīng)該修改我的 sequelize 代碼以將 ID 值存儲(chǔ)為字符串?
不,這不是必需的.
關(guān)于 ID
類(lèi)型的 GraphQL 規(guī)范沒(méi)有涵蓋你如何存儲(chǔ)id,只涵蓋了 GraphQL 服務(wù)器應(yīng)該如何處理 ID
輸入和輸出.由 GraphQL 層來(lái)確保這種行為.實(shí)際存儲(chǔ)中如何處理 id 由存儲(chǔ)層決定.
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 時(shí),是否有正確的處理方法?
我希望上面的答案也回答了這個(gè)問(wèn)題:)
I hope the above answers also answer this question :)
這篇關(guān)于我應(yīng)該在客戶(hù)端將 GraphQL ID 作為字符串處理嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!