好的...我正在尝试开发一个 GraphQL 解决方案,将多个类型定义附加到express-graphql graphqlHTTP()
typeDef
属性,而不必担心为每种类型或嵌套字符串创建单个模式。
我的问题是每当我将参数数组传递给函数时,createSchema()
它会在数组的第二项上显示错误(是的,不是第一项): 注意:不确定第二个问题是实际错误还是只是警告。我想这是警告。
所以,这是函数本身:
static createSchema(typeDefs: GQLSchema | [GQLSchema]): string {
let schema = '';
if (Array.isArray(typeDefs)) {
typeDefs.forEach((typeDef) => {
schema += GQLHTTPTools.addSchema(typeDef);
});
} else {
schema = GQLHTTPTools.addSchema(typeDefs);
}
return schema;
}
// just in case:
private static addSchema(typeDef: GQLSchema): string {
const { types } = typeDef;
let schema = '';
if (types.Query !== undefined) {
schema += `type Query {\n\t${types.Query}\n}\n`;
delete types.Query;
}
if (types.Mutation !== undefined) {
schema += `type Mutation {\n\t${types.Mutation}\n}\n`;
delete types.Mutation;
}
if (types.Custom !== undefined) {
schema += types.Custom.map((type) => {
const key = Object.keys(type)[0];
const val = Object.values(type)[0];
return `type ${key} {\n\t${val}\n}`;
});
}
return schema;
}
不确定它是否重要,但这是GQLSchema
类定义:
export default class GQLSchema {
constructor(public totalTypes: number, public totalCustomTypes: number, public types: GQLType) {}
}
如果我确实传递了一个GQLSchema
对象,它会起作用……但如果我使用GQLSchema
数组则不会。不知道为什么不起作用。有什么线索吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句