代码之家  ›  专栏  ›  技术社区  ›  Zied Hamdi

graphql compose mongoose生成错误:期望[object object]是graphql模式

  •  0
  • Zied Hamdi  · 技术社区  · 7 年前

    我是新手

    我试图在一个简单的Mongoose模式上启动第一个服务:

    图ql.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)"
          ]
        }
      ]
    }
    

    我会错过什么????

    抱歉,我试图用graphql compose mongoose标记这个问题,但是这个标记不存在,所以我用graphql js标记了它

    1 回复  |  直到 7 年前
        1
  •  0
  •   Zied Hamdi    7 年前

    实际上问题就在这里:

    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