【问题标题】:Type resolution for GraphQLObjectType with different nested args具有不同嵌套参数的 GraphQLObjectType 的类型解析
【发布时间】: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


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多