我定义的架构如下:
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
此错误来自何处?
答案 0 :(得分:3)
如果您使用export default 'someString'
,则实际上exports
值的最终值为{ default: 'someString' }
。这就是您可以声明默认导出和命名导出的原因。导入模块
// like this
const typeDefs = require('./data/typedefs').default
// or like this
import typedefs from './data/typedefs'
答案 1 :(得分:0)
我有完全相同的错误。 解决方案:
const { typeDefs } = require('./data/typedefs');
const { resolvers } = require('./data/resolvers ');
需要括号,因为我们不导出默认值。 与前端一样:
export default Component -> import Component from "..."
export const Component -> import { Component } from "..."