Document a GraphQL API

后端 未结 5 1336
梦谈多话
梦谈多话 2021-02-02 05:02

With REST we can use Swagger, RAML or other technologies to document our API and generate an HTML documentation that our consumers can read without any need of interaction with

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 06:02

    To my knowledge there is no tool yet that automatically generates HTML documentation for a GraphQL API, but I've found GraphiQL to be even more useful than any API documentation in HTML that I've seen.

    GraphiQL lets you interactively explore the schema of a GraphQL server and run queries against it at the same time. It has syntax highlighting, autocompletion, and it even tells you when your query is invalid without executing it.

    If you're looking for static documentation, I've found it pretty convenient to read the schema in GraphQL schema language. Thanks to another great feature of GraphQL - schema introspection - you can easily print the schema for any server you have access to. Simply run the introspection query against the server and then print the resulting introspection schema like so (using graphql-js):

    var graphql = require('graphql');
    var introspectionSchema = {}; // paste schema here
    console.log(graphql.printSchema(graphql.buildClientSchema(introspectionSchema)));
    

    The result will look something like this:

    # An author
    type Author {
      id: ID!
    
      # First and last name of the author
      name: String
    }
    
    # The schema's root query type
    type Query {
    
      # Find an author by name (must match exactly)
      author(name: String!): Author
    }
    

提交回复
热议问题