【问题标题】:Why is my Graphql project throwing Error: field type must be Output Type but got: undefined为什么我的 Graphql 项目抛出错误:字段类型必须是输出类型但得到:未定义
【发布时间】:2018-04-09 11:11:47
【问题描述】:

Github Repo

我正在从this espn endpoint 获取数据 我也是 GraphQL 的新手,对获取嵌套信息不是很熟悉。

在上面的 github 存储库中,我目前正在处理“schema1.js”文件,当我运行它时,控制台会抛出错误:Boxscore.teams field type must be Output Type but got: undefined.

通常我会在下面发布 schema1.js,但它太大而无法正确格式化。

感谢任何帮助!

【问题讨论】:

    标签: node.js express graphql graphql-js apollo


    【解决方案1】:

    您当前正在将字段编写为

    ...
    teams: new GraphQLObjectType({ ... })
    ...
    

    正确的语法是

    ...
    teams: {
        type: new GraphQLObjectType({ ... })
    }
    ...
    

    另外,我强烈建议您不要像以前那样定义嵌套类型;相反,独立定义它们:

    const TeamType = new GraphQLObjectType({ ... });
    
    const BoxscoreType = new GraphQLObjectType({
      name: 'Boxscore',
      fields: () => ({
        teams: {
          type: TeamType
        },
        ...
      })
    });
    

    【讨论】:

    • 谢谢!当我在构建它时,感觉有些不对劲,我会尝试分解它
    • 如果是 boxscore > 团队 > 列表,你会怎么做?在“团队”字典之后,我得到了两个列表,那么我是否将 TeamType 字段定义为两个列表,然后需要创建 List0Type 和 List1Type?
    猜你喜欢
    • 2019-09-19
    • 2017-08-16
    • 2018-06-30
    • 2017-09-03
    • 2018-06-25
    • 2019-12-23
    • 2017-03-07
    • 2016-06-29
    • 2017-12-12
    相关资源
    最近更新 更多