【发布时间】:2021-01-17 19:18:55
【问题描述】:
我可能正在接近这个不正确的问题,但似乎 GraphQLObjectType 与不同的 args 他们的字段只会解析到第一个子字段。我在这里缺少更好的约定或结构吗?
在示例中:
import { GraphQLFloat, GraphQLNonNull, GraphQLObjectType, GraphQLString } from 'graphql';
export default new GraphQLObjectType({
name: 'Parent',
fields: {
firstChild: {
type: GraphQLString,
args: {
text: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: (_: never, { text }: { text: string }) => {
return text;
},
},
secondChild: {
type: GraphQLFloat,
args: {
float: { type: new GraphQLNonNull(GraphQLFloat) },
},
resolve: (_: never, { float }: { float: number }) => {
return float;
},
},
},
});
secondChild 的解析将导致 typescript 出现错误
Type '(_: never, { float }: { float: number; }) => number' is not assignable to type 'GraphQLFieldResolver<never, any, { text: string; }>'.
Types of parameters '__1' and 'args' are incompatible.
Property 'float' is missing in type '{ text: string; }' but required in type '{ float: number; }'.ts(2322)
test.ts(21, 40): 'float' is declared here.
definition.d.ts(470, 3): The expected type comes from property 'resolve' which is declared here on type 'GraphQLFieldConfig<never, any, { text: string; }>'
【问题讨论】:
标签: typescript graphql-js