Javascript 如何向“GraphQL 模式语言”中的字段添加描述
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39962867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a description to a field in "GraphQL schema language"
提问by derekdreery
I have a graphql schema, a fragment of which looks like this:
我有一个 graphql 模式,其中的一个片段如下所示:
type User {
username: String!
password: String!
}
In graphiql, there is a description field, but it always says "self-descriptive". How do I add descriptions to the schema?
在graphiql中,有一个描述字段,但它总是说“自我描述”。如何向架构添加描述?
回答by davidyaha
If you're using GraphQL.js version 0.7.0 or above, you can simply add a comment directly before the field, type, or argument you want to describe. For example:
如果您使用 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!
}
Below version 0.7.0 it is not possible to add descriptions inside the schema language.
在 0.7.0 版本以下,无法在模式语言中添加描述。
UPDATE: since version v0.12.3you should use string literals
更新:从v0.12.3版本开始,您应该使用字符串文字
"""
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!
}
回答by Josh Black
This is a great question! And actually has a great history in graphql
world.
这是一个很好的问题!实际上在graphql
世界上有着悠久的历史。
There were multiple issues, discussions, and Pull Requests on the graphql-js
repo that tried to discuss possible syntax for this, as it was something that a lot of members of the community felt were needed. Thanks to Lee Byron and this Pull Request, we can actually add descriptions to a schema language by using traditional comments.
存储库上有多个问题、讨论和 Pull Requestsgraphql-js
试图讨论可能的语法,因为社区的许多成员认为这是需要的。感谢 Lee Byron 和这个 Pull Request,我们实际上可以通过使用传统注释向模式语言添加描述。
For example,
例如,
// 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!
}
`);
And, if we're using graphql
that's newer than 0.7.0
, the comments are actually turned into the description for the fields or types. We can verify this by running an introspection query on our schema:
而且,如果我们使用graphql
的比 新0.7.0
,注释实际上会变成字段或类型的描述。我们可以通过对我们的架构运行自省查询来验证这一点:
const query = `
{
__schema {
types {
name
description,
fields {
name
description
}
}
}
}
`;
graphql(schema, query)
.then((result) => console.log(result));
Which would give us a result that looks like:
这会给我们一个看起来像这样的结果:
{
"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"
}
]
},
]
}
}
}
And shows us that the #
comments were incorporated as the descriptions for the fields/comments that we put them on.
并向我们展示了这些#
评论被合并为我们放置它们的字段/评论的描述。
Hope that helps!
希望有帮助!
回答by Fabian
In case you're using a Javaimplementation ....
如果您使用的是Java实现....
For graphql-java
version 7.0 (the latest version as of this writing) with a schema first approach, you can use commentsabove the field, type, or argument.
对于graphql-java
采用模式优先方法的 7.0 版(撰写本文时的最新版本),您可以在字段、类型或参数上方使用注释。
String literalsare notvalid syntax as of version 7.0.
字符串文字是不是有效的语法为7.0版本。