简体   繁体   中英

How can I get list of all types from graphql schema?

On client I get schema by introspectSchema .

In my app I want to get list of all types which we have in schema, but parse existing object is very hard task.

Is there some good way to do it?

GraphQL has powerful introspection capabilities built in. To get the names of the types in your schema you can run this GraphQL query:

{
  __schema {
    types {
      name
    }
  }
}

You can read more about introspection in GraphQL here:

https://graphql.org/learn/introspection/

For parsing constructor object type , you need printSchema , the result will be string type

Here is a code snippet:

  const { introspectSchema } = require('graphql-tools');
  const { printSchema } = require('graphql');

  const schema = await introspectSchema(link);

  const typeDefs = printSchema(schema);

And, you can use merge-graphql-schemas to merge your string type .

source code: https://github.com/mrdulin/apollo-server-express-starter/blob/master/src/remote-schema/helper.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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