graphql-compose-mongoose生成错误:期望[object Object]成为GraphQL架构

时间:2018-05-30 14:32:11

标签: graphql-js

我是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标记了

1 个答案:

答案 0 :(得分:0)

实际上问题出在这里:

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