0
votes

I've been searching for this error, but without achieving a successfull solution. I'm studing graphql, so I build a little application, here's my package.json:

"dependencies": {
    "apollo-server-express": "^1.3.6",
    "babel-watch": "^2.0.7",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "graphql": "^0.13.2",
    "graphql-tools": "^3.0.2",
    "mongoose": "^5.1.3",
    "node-uuid": "^1.4.8"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0"
  }

My schema.js:

import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'
import resolvers from './resolvers'

const typeDefs = `
    type Author {
        id: String
        name: String
        age: Int
        books: [String]
    }

    type Query {
        authors: [Author]
        author(id: String): Author
    }

    type Mutation {
        addAuthor(name: String!, age: Int!, books: [String]!):Author
    }
`
const schema = makeExecutableSchema({typeDefs, resolvers});

export default schema;

My resolvers.js:

import mongoose from 'mongoose';
import authorModel from './model/author';

const resolvers = {
    Query: {
        authors: () => {
            //return fakeData;
        },
        // root is never used, and args holds our filter params
        author: (root, args) => {
            //const id = args.id;
            //return fakeData.find((author) => author.id === id);
        }
    },
    // mutations changes the data states
    Mutation: {
        addAuthor: (root, {name, age, books}) => {
            console.log(name, age, books);
            const author = new authorModel({name: name, age: age, books: books});
            return author.save();
        }
    }
};

export default resolvers;

And finally, my model author.js:

import mongoose from 'mongoose';
// to generate our unique IDs
import uuid from 'node-uuid';
const schema = mongoose.Schema;

// this schema must be compatible with schema from graphQl
const authorSchema = new schema({
    id: {type: String, default: uuid.v1},
    name: String,
    age: Number,
    books: [String]
});

export default authorSchema;

All works ok, my express works ok with apollo-server-express, and when I use the addAuthor mutation, the function addAuthor in resolver is called, but an error occurs on the creation of author objetc model, the error:

TypeError: _author2.default is not a constructor
    at addAuthor (/home/desinv04/estudos/graphql/worldcup-graphql/resolvers.js:46:28)
    at resolveFieldValueOrError (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:531:18)
    at resolveField (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:495:16)
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:339:18
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:25:10
    at Array.reduce (<anonymous>)
    at promiseReduce (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/jsutils/promiseReduce.js:22:17)
    at executeFieldsSerially (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:336:38)
    at executeOperation (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:289:55)
    at executeImpl (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:154:14)
    at Object.execute (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/graphql/execution/execute.js:131:229)
    at doRunQuery (/home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:194:7)
    at /home/desinv04/estudos/graphql/worldcup-graphql/node_modules/apollo-server-core/src/runQuery.ts:79:39
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)

I can't see why this error occurs, thanks in advance.

2
I think in mongoose a schema is not a model. You have to create the model first: export default mongoose.model(authorSchema). See in the docs: mongoosejs.com/docs/populate.html - Herku

2 Answers

0
votes

try this:

import mongoose from 'mongoose';
import uuid from 'node-uuid';
const schema = mongoose.Schema;

const authorSchema = new schema({
    id: {type: String, default: uuid.v1},
    name: String,
    age: Number,
    books: [String]
})

const model = mongoose.model('author', authorSchema);
export default model;
0
votes

Referencing your resolvers.js file import authorModel from './model/author'; means that authorModel has to be exported from the file author.js. But looks like it doesn't. Work on exporting authorModel from author.js file.