简体   繁体   中英

'Got Object' exception in GraphQL schema definition

The schema that I defined is like so:

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]
    }
`;

At the end I bring it all together with:

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

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

But when I try to run the application, I get this error: Error: typeDef array must contain only strings and functions, got object

Where is this error coming from?

If you use export default 'someString' , under the hood, the resulting value for exports value ends up being { default: 'someString' } . That's what allows you do declare both a default export and named exports. Import your module

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

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

I had exactly the same error. Solution:

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

parentheses needed because we don't export default value. As on frontend:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM