【问题标题】:What is the correct way to work with GraphQL Custom Input types?使用 GraphQL 自定义输入类型的正确方法是什么?
【发布时间】:2020-01-04 03:49:26
【问题描述】:

我是 GraphQL 的新手,正在尝试实现一个 args 类以用作自定义输入类型。

这是我的课的一个例子:

@ArgsType()
export class CreateVariableArgs {
    ... other properties

    @Field(type => String, { nullable: true })
    customClass?: customClassInputType[];
}

这是我的输入类型:

@ArgsType()
export class customClassInputType {
    @Field(type => String)
    name: string;

    @Field(type => Boolean)
    isDefault: boolean;

    ... other properties
}

我想要做的是从 React Typescript UI 传递我的自定义实体数组。我的查询是对我的 GraphQL 服务器的突变请求。我想在解析器中做一些工作,并最终将数据保存在 PostGres 数据库中,并将数据返回或向 UI 抛出错误。

目前我的 graphQL 服务器不知道自定义输入类型是什么。 GraphQL 必须有一种方法可以处理的不仅仅是原始标量类型,但我一直无法找到任何文档,并且已经在这个问题上折腾了一个星期,但没有成功。非常感谢任何帮助。

这是我在调试控制台中遇到的错误。

{"message":"Cannot determine GraphQL input type for customClass","level":"error"}
{"message":"Error: Cannot determine GraphQL input type for customClass\n    at Function.getGraphQLInputType (node_modules/type-graphql/dist/schema/schema-generator.js:415:19)\n    at argumentType.fields.forEach.field (node_modules/type-graphql/dist/schema/schema-generator.js:361:28)\n    at Array.forEach (<anonymous>)\n    at Function.mapArgFields (node_modules/type-graphql/dist/schema/schema-generator.js:357:29)\n    at params.reduce (node_modules/type-graphql/dist/schema/schema-generator.js:350:22)\n    at Array.reduce (<anonymous>)\n    at Function.generateHandlerArgs (node_modules/type-graphql/dist/schema/schema-generator.js:332:23)\n    at handlers.reduce (node_modules/type-graphql/dist/schema/schema-generator.js:285:28)\n    at Array.reduce (<anonymous>)\n    at Function.generateHandlerFields (node_modules/type-graphql/dist/schema/schema-generator.js:278:25)","level":"error"}

是否可以使用自定义类型?或者我是否仅限于对我的自定义类型数组进行字符串化并以这种形式使用它们?

【问题讨论】:

    标签: reactjs typescript graphql typegraphql


    【解决方案1】:

    你应该改变 customClassInputType

    @InputType()
    export class customClassInputType {
        @Field(type => String)
        name: string;
    
        @Field(type => Boolean)
        isDefault: boolean;
    
        ... other properties
    }
    

    并使用 customClassInputType 如下

    @ArgsType()
    export class CreateVariableArgs {
        ... other properties
    
        @Field(() => customClassInputType, {nullable:true})
        customClass?: customClassInputType[];
    }
    

    因为@InputType 将生成真正的 GraphQLInputType,并且应该在您希望在 args 中有嵌套对象时使用

    并且@ArgsType 是虚拟的,它将在架构中被展平:

    更多细节可以在这里找到 https://typegraphql.ml/docs/0.17.0/faq.html

    【讨论】:

    • 谢谢,我会试一试,当我有机会再次处理这部分时,我会回复你。我最终做的是对这些类型的数组进行字符串化,然后在解析器中对其进行解析。不理想,但目前可以使用。
    • 其实,我认为应该是@Field(() =&gt; [customClassInputType], ...),其中类型放在方括号之间以将其定义为数组。
    【解决方案2】:

    显然您可以使用自定义类型。 但我不知道如何让它在打字稿中工作

    我在 javascript 中使用graphql 库,它为查询提供GraphQLObjectType,为突变提供GraphQLInputObjectType,这使您可以创建自己的新类型

    import { GraphQLObjectType, GraphQLString } from 'graphql'
    
    export default new GraphQLObjectType({
      name: 'UserType',
      fields: () => ({
        username: {
          type: GraphQLString,
          resolve: () => 'name',
        },
      }),
    })
    
    

    对不起,帮不上忙

    【讨论】:

    • 试试看这个graphql-typescript
    • 感谢您的建议。起初我确实尝试使用该库,但是我无法让它与 Typescript 一起使用。
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2012-04-07
    • 2021-05-30
    相关资源
    最近更新 更多