Eric
Eric

Reputation: 1210

'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?

Upvotes: 1

Views: 2765

Answers (2)

Nikita Goloburda
Nikita Goloburda

Reputation: 11

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 "..."

Upvotes: 1

Daniel Rearden
Daniel Rearden

Reputation: 84657

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'

Upvotes: 4

Related Questions