简体   繁体   English

如何在“GraphQL模式语言”中向字段添加描述

[英]How do I add a description to a field in “GraphQL schema language”

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". 在graphiql中,有一个描述字段,但它总是说“自我描述”。 How do I add descriptions to the schema? 如何向架构添加描述?

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. 如果您使用的是GraphQL.js 0.7.0或更高版本,则可以直接在要描述的字段,类型或参数之前添加注释。 For example: 例如:

# 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.3 you 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!

}

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. graphql-js repo上有多个问题,讨论和Pull请求试图讨论这种可能的语法,因为这是社区很多成员认为需要的东西。 Thanks to Lee Byron and this Pull Request , we can actually add descriptions to a schema language by using traditional comments. 感谢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. 而且,如果我们使用的是比0.7.0更新的graphql ,则注释实际上会转换为字段或类型的描述。 We can verify this by running an introspection query on our schema: 我们可以通过在我们的模式上运行内省查询来验证这一点:

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! 希望有所帮助!

In case you're using a Java implementation .... 如果你正在使用Java实现....

For graphql-java version 7.0 (the latest version as of this writing) with a schema first approach, you can use comments above the field, type, or argument. 对于使用模式第一种方法的graphql-java版本7.0(撰写本文时的最新版本),您可以在字段,类型或参数上方使用注释

String literals are not valid syntax as of version 7.0. 从7.0版开始, 字符串文字 不是有效语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM