Javascript 如何向“中的字段添加说明?”;GraphQL模式语言“;
我有一个graphql模式,其片段如下所示:Javascript 如何向“中的字段添加说明?”;GraphQL模式语言“;,javascript,graphql,apollo-server,Javascript,Graphql,Apollo Server,我有一个graphql模式,其片段如下所示: type User { username: String! password: String! } 在graphiql中,有一个描述字段,但它总是表示“自描述性”。如何向架构添加描述 如果您使用的是GraphQL.js版本0.7.0或更高版本,只需在要描述的字段、类型或参数之前直接添加注释即可。例如: # A type that describes the user type User { # The user's use
type User {
username: String!
password: String!
}
在graphiql中,有一个描述字段,但它总是表示“自描述性”。如何向架构添加描述 如果您使用的是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开始您应该使用字符串文本
"""
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
repo上有多个问题、讨论和请求,试图讨论可能的语法,因为社区的许多成员都认为这是必要的。多亏了Lee Byron和,我们实际上可以通过使用传统注释向模式语言添加描述
比如说,
// 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"
}
]
},
]
}
}
}
并向我们展示了#
注释是作为字段/注释的描述合并在一起的
希望有帮助 如果您使用的是Java实现 对于采用模式优先方法的
graphql java
Version7.0(本文撰写时的最新版本),可以在字段、类型或参数上方使用注释
字符串文本从7.0版开始是无效语法。PS散列您的密码!非常有帮助的感谢-我确实花了很长时间寻找答案,并且在许多老问题中苦苦挣扎-当答案如此简单的时候!:)是的,我也花了一段时间才找到。太好了!我正在使用GraphQL0.12.3,这对我来说不起作用。使用上面的代码,Description始终为null。这不再是默认值,请参阅:--应该是类似于
“My Description”
的字符串文字,因此字符串文字是截至2018年2月的当前默认值。规范的相关部分:如果有人正在寻找如何在TypeGraphQL中实现它,只需在decorator选项中使用description
属性即可。例如,@ObjectType({description:'Here'})
。对于@字段({description:…}、@Arg和@Query)也一样