【问题标题】:'Got Object' exception in GraphQL schema definitionGraphQL 架构定义中的“得到对象”异常
【发布时间】:2019-06-15 03:01:58
【问题描述】:

我定义的架构是这样的:

import { gql } from 'apollo-server';

export default gql`
    type ProjectEntry {
        ID: Int!
        Name: String
    }

    # The schema allows the following Queries:
    type Query {
        project(id: Int!): ProjectEntry
        projects: [ProjectEntry]
    }
`;

最后,我将所有内容与以下内容结合在一起:

const typeDefs = require('./data/typedefs');
const resolvers = require('./data/resolvers ');

const server = new ApolloServer({ typeDefs, resolvers });

但是当我尝试运行应用程序时,我得到了这个错误:Error: typeDef array must contain only strings and functions, got object

这个错误来自哪里?

【问题讨论】:

  • 你是如何导入你的 typeDefs 的?
  • @DanielRearden,我已经用你要求的信息更新了帖子。

标签: node.js graphql apollo-server


【解决方案1】:

如果您使用export default 'someString',在后台,exports 值的结果值最终会是{ default: 'someString' }。这就是允许您同时声明默认导出和命名导出的原因。导入你的模块

// like this
const typeDefs = require('./data/typedefs').default

// or like this
import typedefs from './data/typedefs'

【讨论】:

    【解决方案2】:

    我遇到了完全相同的错误。 解决方案:

    const { typeDefs } = require('./data/typedefs');
    const { resolvers } = require('./data/resolvers ');
    

    需要括号,因为我们不导出默认值。 在前端:

    export default Component -> import Component from "..."
    export const Component -> import { Component } from "..."
    

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2019-04-11
      • 2012-08-08
      • 2022-01-23
      相关资源
      最近更新 更多