【问题标题】:graphql-compose-mongoose generating an error: Expected [object Object] to be a GraphQL schemagraphql-compose-mongoose 生成错误:预期 [object Object] 是 GraphQL 模式
【发布时间】:2018-11-09 11:09:46
【问题描述】:

我是 graphql-compose 的新手

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

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 标记了它

【问题讨论】:

    标签: graphql-js


    【解决方案1】:

    其实问题出在这里:

    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

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2020-06-07
      • 1970-01-01
      • 2019-05-11
      • 2023-03-25
      • 2016-12-28
      • 2021-12-23
      • 2019-02-16
      • 2018-10-14
      相关资源
      最近更新 更多