【问题标题】:GraphQL Nexus asNexusMethod showing type errorGraphQL Nexus asNexus 方法显示类型错误
【发布时间】:2022-10-02 14:52:41
【问题描述】:

我将 DateTime 实现为一个 nexus 方法,如我的 Next.js 项目中的 docs 所示。

// types/DateTime.ts
import { GraphQLDate, GraphQLDateTime, GraphQLTime } from \'graphql-iso-date\';
import { asNexusMethod } from \'nexus\';

export const GQLDate = asNexusMethod(GraphQLDate, \'date\');
export const GQLTime = asNexusMethod(GraphQLTime, \'time\');
export const GQLDateTime = asNexusMethod(GraphQLDateTime, \'datetime\');

该文件也包含在 GraphQL 模式中的类型中

// Other imports
import * as types from \'./types\';

export const schema = makeSchema({
  types: types,
  // ...
});

但是,我收到以下 TypeScript 错误:

Argument of type \'GraphQLScalarType\' is not assignable to parameter of type \'GraphQLNamedType\'.
  Type \'GraphQLScalarType\' is missing the following properties from type \'GraphQLScalarType<unknown, unknown>\': specifiedByURL, [Symbol.toStringTag]ts(2345)

如何解决此错误?

    标签: typescript next.js graphql apollo-server


    【解决方案1】:

    我使用了graphql-scalars,效果很好,如图here

    import { DateTimeResolver } from 'graphql-scalars';
    
    const DateTime = asNexusMethod(DateTimeResolver, 'datetime');
    
    export const schema = makeSchema({
      types: [
        DateTime,
        /** Queries, mutations, and other scalar types here */
      ],
      /** more config options here */
    })
    
    // Inside type file
    t.nonNull.datetime('createdAt');
    

    如果您使用任何 linting 工具,只需确保在添加类型后点击 GraphQL API 端点。否则,它会一直在 IDE 中显示类型错误。

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2022-09-23
      • 1970-01-01
      • 2018-10-26
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      相关资源
      最近更新 更多