node.js - 在单独的文件中制作 typedef 以导入
问题描述
我正在使用 GraphQL、NodeJS 开发一个项目,并且我正在尝试创建一个名为的新文件typedef.ts
,我想将其导入我的文件中,schema.ts
但由于某种原因我无法使其工作。我得到的错误如下:
Error: typeDefs must be a string, array or schema AST, got object
到目前为止,我在 schema.ts 中尝试了以下更改:
import { makeExecutableSchema } from 'graphql-tools';
import { TypeDefs } from './typedefs/typedefs';
import { resolvers } from '../graphql/resolvers';
const typeDefs: Array<string> = TypeDefs; // <- Added this
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
logger: { log: e => console.log(e) },
});
typedef.ts:
import { types } from '../../graphql/types';
import { Types } from '../types/types';
const schemaDefinition = `
schema {
query : Query
mutation : Mutation
}
`;
export const TypeDefs = {
schemaDefinition,
// Card Type Def
cardDef: [Types.cardTypes.query, Types.cardTypes.mutation],
// User Type Def
userDef: [Types.userTypes.query, Types.userTypes.mutation],
...types,
};
请向我解释如何解决此问题,因为我找不到解决方案。
解决方案
我不明白我的问题的减号,因为我正在学习这些技术,这是一个合法的猜测。如果我弄错了,我会很高兴得到解释,这样我才能理解。
顺便说一句,我解决了我的问题,如下所示:
架构.ts
import { makeExecutableSchema } from 'graphql-tools';
import TypeDefs from './typedefs/typedefs';
import { resolvers } from '../graphql/resolvers';
export const schema = makeExecutableSchema({
typeDefs: TypeDefs,
resolvers,
logger: { log: e => console.log(e) },
});
类型定义.ts
import { types } from '../../graphql/types';
import { Types } from '../types/types';
const schemaDefinition = `
schema {
query : Query
mutation : Mutation
}
`;
const TypeDefs = [
schemaDefinition,
// Card Type Def
Types.cardTypes.query,
Types.cardTypes.mutation,
// User Type Def
Types.userTypes.query,
Types.userTypes.mutation,
...types,
];
export { TypeDefs as default };
推荐阅读
- python - Python//字典和列表的新功能
- r - 使用 ggplot 标记生存情节线
- linux - 有没有办法在离线的红帽机器上安装“make”命令?
- visual-studio - 查找损坏的 Visual Studio 用户设置
- python - 除了包含特定值的行或列中的值之外,替换矩阵的所有值的有效方法是什么?
- google-apps-script - 无法正确格式化日期谷歌应用脚本
- r - 如何在 Linux 服务器上的 R 中验证 BigQuery (bigrquery)
- python - Python - opencv 在 Canny 边缘图像周围绘制边界框
- .net-core - 放置方法 - CORS 策略阻止了从源获取的访问权限
- java - java - 如何使用Java获取JSON文件中多个对象的相同属性?