具有不同嵌套参数的GraphQLObjectType的类型解析

时间:2020-10-02 04:04:32

标签: typescript graphql-js

我可能正在处理这种错误,但是似乎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的解析将导致打字稿错误

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; }>'

1 个答案:

答案 0 :(得分:0)