[英]Type GQLSchema is not assignable to type undefined
Okay... I am trying to develop a GraphQL solution to attach several type definitions to express-graphql graphqlHTTP()
typeDef
property without worrying about creating a single schema for each type or nesting strings.好的...我正在尝试开发一个 GraphQL 解决方案,将多个类型定义附加到express-graphql
graphqlHTTP()
typeDef
属性,而无需担心为每种类型或嵌套字符串创建单个模式。
My issue here is whenever I pass an array of arguments to function createSchema()
it displays an error on the second item of the array (yes, not in the first):我的问题是,每当我将参数数组传递给函数
createSchema()
它都会在数组的第二项(是的,不是第一项createSchema()
上显示错误: Note: Not sure if the second problem is an actual error or just a warning.
注意:不确定第二个问题是实际错误还是警告。 I guess it is warning.
我想这是警告。
So, this is the function itself:所以,这是函数本身:
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;
}
Not sure if it's important, but this is the GQLSchema
class definition:不确定它是否重要,但这是
GQLSchema
类定义:
export default class GQLSchema {
constructor(public totalTypes: number, public totalCustomTypes: number, public types: GQLType) {}
}
If I do pass a single GQLSchema
object it works... but not if I use a GQLSchema
array.如果我确实传递了一个
GQLSchema
对象,它会起作用……但如果我使用GQLSchema
数组,则不会。 Not sure why isn't working.不知道为什么不起作用。 Any clues?
有什么线索吗?
In TypeScript [X]
is a Tuple在 TypeScript 中
[X]
是一个元组
Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same.
元组类型允许你用固定数量的元素来表达数组,这些元素的类型是已知的,但不必相同。
So [X]
means an array with a length of exactly one where the first element is of the type X
.所以
[X]
表示一个长度正好为 1 的数组,其中第一个元素的类型为X
。 [X, Y]
means an array with a length of exactly two where the first element is of type X
and the second element is of type Y
. [X, Y]
表示长度正好为 2 的数组,其中第一个元素的类型为X
,第二个元素的类型为Y
。 And so on.等等。
If your intent is to type an array of any length where every element is a specific type, use an Array instead: X[]
or Array<X>
(these are equivalent).如果您的意图是键入任何长度的数组,其中每个元素都是特定类型,请改用Array :
X[]
或Array<X>
(它们是等效的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.