【问题标题】:How to get the type names of the query in graphql如何在graphql中获取查询的类型名称
【发布时间】:2018-08-05 13:11:13
【问题描述】:

您好,我想在 graphql 中获取查询的类型名称。它不应该有标量和枚举类型。

schema {
    query: Query
}

type LearningResource{
    id: ID
    name: String
    type: String
    children: [LearningResource]
}

type Query {
    fetchLearningResource: LearningResource
}

当我运行fetchLearningResource 查询时,我想像这里LearningResource 一样获取它的类型。在 GraphQL 中有什么方法可以做到这一点?

【问题讨论】:

    标签: javascript node.js graphql graphql-js


    【解决方案1】:

    您应该在响应中找到作为字符串的查询类型,搜索__typename

    例如

    response =>
    
    {
       fetchLearningResource: {
          id: <id>
          name: "name"
          type: "type"
          children: [...]
          __typename: "LearningResource"
       }
    }

    编辑:

    如果您想在查询之前获取查询定义。您可以从服务器获取自省查询。以下是一些链接:

    introspection query error

    retrieve whole schema question

    【讨论】:

    • 我没有回复,在获得回复之前,我需要获取该查询的类型。我需要做点什么
    • 请更好地解释您想要做什么以及为什么需要查询响应类型
    • 我想提取查询的类型然后想执行一些操作,比如在数据库中查找针对该类型的映射
    • 我认为这不容易。我会用一些链接更新我的答案
    猜你喜欢
    • 2019-08-07
    • 2012-07-03
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多