graphql-compose-mongoose generating an error: Expected [object Object] to be a GraphQL schema

graphql-compose-mongoose generating an error: Expected [object Object] to be a GraphQL schema

我是 graphql-compose 新手

我正在尝试在一个简单的 mongoose 模式上启动第一个服务:

graphql.js :

import mongoose from 'mongoose'
import { composeWithMongoose} from 'graphql-compose-mongoose'
import { schemaComposer } from 'graphql-compose'

const db = require( '../models/db' )
//const mongoose = require('mongoose');
const folderDAO = mongoose.model('folder');

const customizationOptions = {}; // left it empty for simplicity, described below
const folderTC = composeWithMongoose(folderDAO, customizationOptions);

schemaComposer.rootQuery().addFields({
    folderOne: folderTC.getResolver('findOne'),
})

const graphqlSchema = schemaComposer.buildSchema()

console.log("Schema built : ", graphqlSchema )

export default graphqlSchema

现在在我的服务器代码中,我有这个:

const express = require('express');
const graphqlHTTP = require('express-graphql')
const GraphQLSchema = require('./app_api/routes/graphql')
    app.use('/graphql', graphqlHTTP({
        schema: GraphQLSchema,
        graphiql: true,
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack ? error.stack.split('\n') : [],
            path: error.path
        })
    }));

在 graphiql 上,当我尝试以下查询时:

{
  folderOne(filter: {}, sort: _ID_ASC) {
    name
  }
}

我收到以下错误:

{
  "errors": [
    {
      "message": "Expected [object Object] to be a GraphQL schema.",
      "stack": [
        "Error: Expected [object Object] to be a GraphQL schema.",
        "    at invariant (/Users/zied/work/share_place/node_modules/graphql/jsutils/invariant.js:19:11)",
        "    at validateSchema (/Users/zied/work/share_place/node_modules/graphql/type/validate.js:55:60)",
        "    at assertValidSchema (/Users/zied/work/share_place/node_modules/graphql/type/validate.js:80:16)",
        "    at validate (/Users/zied/work/share_place/node_modules/graphql/validation/validate.js:58:35)",
        "    at /Users/zied/work/share_place/node_modules/express-graphql/dist/index.js:139:52",
        "    at <anonymous>",
        "    at process._tickDomainCallback (internal/process/next_tick.js:228:7)"
      ]
    }
  ]
}

我可能遗漏了什么???

p.s:抱歉,我试图用 graphql-compose-mongoose 标记问题,但该标记不存在,所以我用 graphql-js

标记了它

实际上问题出在这里:

const GraphQLSchema = require('./app_api/routes/graphql')

必须替换为

const GraphQLSchema = require('./app_api/routes/graphql').default 

因为我们将其导出为默认值

可在此处找到更多信息:https://github.com/graphql-compose/graphql-compose-mongoose/issues/103