You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用graphql-compose-mongoose报错:期望[object Object]为GraphQL Schema

问题:GraphQL-Compose 搭建服务时出现「Expected [object Object] to be a GraphQL schema」错误

我是graphql-compose新手,尝试基于简单的Mongoose Schema搭建首个服务:

GraphQL 代码

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

请问我遗漏了什么?


解决方案

这个问题的核心是模块导入导出的格式不匹配,导致express-graphql拿到的不是一个有效的GraphQL Schema对象。

你看,你的graphql.js用的是ES Module的export default导出Schema,但服务器代码里用的是CommonJS的require去导入。在Node.js环境中,用require导入ES Module的默认导出时,需要通过.default属性来获取实际的Schema对象。

具体修复步骤:

修改服务器代码里的导入语句:

// 原来的写法
const GraphQLSchema = require('./app_api/routes/graphql')

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

另外还有一种可选的调整方式:如果你的项目想统一用ES Module,可以在package.json里添加"type": "module",然后把服务器代码改成ES Module的语法:

import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import GraphQLSchema from './app_api/routes/graphql';

两种方式选一种即可,核心就是保证导入导出的模块系统一致,让express-graphql能拿到真正的GraphQL Schema实例,而不是一个包含默认导出的包装对象。

内容的提问来源于stack exchange,提问作者Zied Hamdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:18:57