如何向 "GraphQL schema language" 中的字段添加说明
How do I add a description to a field in "GraphQL schema language"
我有一个 graphql 模式,其中的一个片段如下所示:
type User {
username: String!
password: String!
}
在 graphiql 中,有一个描述字段,但它总是显示 "self-descriptive"。如何向模式添加描述?
如果您使用的是 GraphQL.js 版本 0.7.0 或更高版本,您只需在要描述的字段、类型或参数之前直接添加注释即可。例如:
# A type that describes the user
type User {
# The user's username, should be typed in the login field.
username: String!
# The user's password.
password: String!
}
0.7.0 以下版本无法在模式语言中添加描述。
更新:从版本 v0.12.3 开始你应该使用 string literals
"""
A type that describes the user. Its description might not
fit within the bounds of 80 width and so you want MULTILINE
"""
type User {
"The user's username, should be typed in the login field."
username: String!
"The user's password."
password: String!
}
这是一个很好的问题!实际上在 graphql
世界上有着伟大的历史。
graphql-js
存储库中存在多个问题、讨论和合并请求,试图讨论可能的语法,因为社区的许多成员都认为需要这样做。感谢 Lee Byron 和 this Pull Request,我们实际上可以通过使用传统注释向模式语言添加描述。
例如,
// Grab some helpers from the `graphql` project
const { buildSchema, graphql } = require('graphql');
// Build up our initial schema
const schema = buildSchema(`
schema {
query: Query
}
# The Root Query type
type Query {
user: User
}
# This is a User in our project
type User {
# This is a user's name
name: String!
# This is a user's password
password: String!
}
`);
而且,如果我们使用比 0.7.0
更新的 graphql
,注释实际上会变成字段或类型的描述。我们可以通过 运行 对我们的模式进行自省查询来验证这一点:
const query = `
{
__schema {
types {
name
description,
fields {
name
description
}
}
}
}
`;
graphql(schema, query)
.then((result) => console.log(result));
这会给我们一个看起来像这样的结果:
{
"data": {
"__schema": {
"types": [
{
"name": "User",
"description": "This is a User in our project",
"fields": [
{
"name": "name",
"description": "This is a user's name"
},
{
"name": "password",
"description": "This is a user's password"
}
]
},
]
}
}
}
并向我们展示 #
评论已合并为我们放置它们的 fields/comments 的描述。
希望对您有所帮助!
如果您使用的是 Java 实现....
对于采用模式优先方法的 graphql-java
7.0 版(撰写本文时的最新版本),您可以在字段、类型或参数上方使用 comments .
字符串文字 不是 版本 7.0 的有效语法。
我有一个 graphql 模式,其中的一个片段如下所示:
type User {
username: String!
password: String!
}
在 graphiql 中,有一个描述字段,但它总是显示 "self-descriptive"。如何向模式添加描述?
如果您使用的是 GraphQL.js 版本 0.7.0 或更高版本,您只需在要描述的字段、类型或参数之前直接添加注释即可。例如:
# A type that describes the user
type User {
# The user's username, should be typed in the login field.
username: String!
# The user's password.
password: String!
}
0.7.0 以下版本无法在模式语言中添加描述。
更新:从版本 v0.12.3 开始你应该使用 string literals
"""
A type that describes the user. Its description might not
fit within the bounds of 80 width and so you want MULTILINE
"""
type User {
"The user's username, should be typed in the login field."
username: String!
"The user's password."
password: String!
}
这是一个很好的问题!实际上在 graphql
世界上有着伟大的历史。
graphql-js
存储库中存在多个问题、讨论和合并请求,试图讨论可能的语法,因为社区的许多成员都认为需要这样做。感谢 Lee Byron 和 this Pull Request,我们实际上可以通过使用传统注释向模式语言添加描述。
例如,
// Grab some helpers from the `graphql` project
const { buildSchema, graphql } = require('graphql');
// Build up our initial schema
const schema = buildSchema(`
schema {
query: Query
}
# The Root Query type
type Query {
user: User
}
# This is a User in our project
type User {
# This is a user's name
name: String!
# This is a user's password
password: String!
}
`);
而且,如果我们使用比 0.7.0
更新的 graphql
,注释实际上会变成字段或类型的描述。我们可以通过 运行 对我们的模式进行自省查询来验证这一点:
const query = `
{
__schema {
types {
name
description,
fields {
name
description
}
}
}
}
`;
graphql(schema, query)
.then((result) => console.log(result));
这会给我们一个看起来像这样的结果:
{
"data": {
"__schema": {
"types": [
{
"name": "User",
"description": "This is a User in our project",
"fields": [
{
"name": "name",
"description": "This is a user's name"
},
{
"name": "password",
"description": "This is a user's password"
}
]
},
]
}
}
}
并向我们展示 #
评论已合并为我们放置它们的 fields/comments 的描述。
希望对您有所帮助!
如果您使用的是 Java 实现....
对于采用模式优先方法的 graphql-java
7.0 版(撰写本文时的最新版本),您可以在字段、类型或参数上方使用 comments .
字符串文字 不是 版本 7.0 的有效语法。