簡體   English   中英

如何從graphql模式獲取所有類型的列表?

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

在客戶端上,我通過introspectSchema獲取模式。

在我的應用程序中,我想獲取架構中所有類型的列表,但是解析現有對象是一項艱巨的任務。

有什么好辦法嗎?

GraphQL具有內置的強大自省功能。要獲取架構中類型的名稱,可以運行以下GraphQL查詢:

{
  __schema {
    types {
      name
    }
  }
}

您可以在此處閱讀有關GraphQL中自省的更多信息:

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

對於解析constructor object type ,您需要printSchema ,結果將是string type

這是一個代碼片段:

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

  const schema = await introspectSchema(link);

  const typeDefs = printSchema(schema);

並且,您可以使用merge-graphql-schemas合並您的string type

源代碼: https : //github.com/mrdulin/apollo-server-express-starter/blob/master/src/remote-schema/helper.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM