首页 > 解决方案 > typeDefs 必须只包含字符串、文档、模式或函数,得到对象

问题描述

我正在开发一个 express apollo 服务器,并构建了所有 graphql 类型,如下所示:

在此处输入图像描述

我正在使用“graphql-tools/load-files”和“graphql-tools/merge”来合并所有类型。我的 index.js 文件如下所示:

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');
const types = loadFilesSync(path.join(__dirname, '.'), { recursive: true })
const typeDefs = mergeTypeDefs(types);
module.exports = { typeDefs };

当我运行我的项目时,出现以下错误:

Error: typeDefs must contain only strings, documents, schemas, or functions, got object

该代码在以前的项目中正常工作。但是当我开始一个新项目删除以前的节点模块和新的 npm 安装时,我遇到了这个奇怪的问题。我怎样才能摆脱它?

标签: node.jsexpressgraphqlexpress-graphql

解决方案


我找到了解决问题的方法。我不得不删除 { recursive: true } 并添加 { extensions: ['gql'] 。这里是:

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');
const typesArray = loadFilesSync(path.join(__dirname, '.'), { extensions: ['gql'] });
const typeDefs = mergeTypeDefs(types);
module.exports = { typeDefs };

推荐阅读