如何在Nestjs上创建自定义GraphQl标量? GraphQL标量

发布于 2025-02-01 20:45:50 字数 1914 浏览 2 评论 0 原文

我正在使用Apollo Server上使用GraphQL上的Nestjs实施一个框架,我想使用一些自定义的GraphQl标量。我找到了这个网站,,这有助于导入自定义标量,而无需实际实现 https://docs.nestjs.com/graphql/scalars#create-a-custom-scalar 。要具体来说,我想使用 bigint time url

,我不确定代码在哪里。我应该在 app.module.ts 上对此进行编码?


// or import specific typeDefs only with ES6 Import
import { ScalarNameTypeDefinition } from 'graphql-scalars';
// or import specific typeDefs only with CommonJS
const { ScalarNameTypeDefinition } = require('graphql-scalars');
// or import all typeDefs once with ES6 Import
import { typeDefs as scalarTypeDefs } from 'graphql-scalars';
// or import all typeDefs once with CommonJS
const { typeDefs: scalarTypeDefs } = require('graphql-scalars');

const typeDefs = [
  ...scalarTypeDefs,
  // other typeDefs
];
// or
const typeDefs = [
  ScalarNameTypeDefinition,
  // other typeDefs
];

我当前的graphQlModule:

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  typePaths: ['./**/**/**/*.graphql'],
  definitions: {
    path: join(process.cwd(), 'src/graphql.ts'),
    outputAs: 'class',
  },  
}),

?代码应该在哪里? Assets.Resolver.ts ?我也不明白此代码属于?

简而言之,如何在Apollo服务器上的Nestjs框架中使用 GraphQl-Scalars 软件包?是否有开源的GitHub存储库可以研究?

I am implementing a framework using Nestjs on Apollo Server using GraphQL and I would like to use some custom GraphQL scalars. I found this site, https://www.graphql-scalars.dev/docs/quick-start, which is helpful for importing custom scalars without actually implementing them as written on https://docs.nestjs.com/graphql/scalars#create-a-custom-scalar. To be specific, I would like to use BigInt, Time, and URL.

From the docs on the quick start page, I am uncertain where the code belongs at. Should I code this at app.module.ts?


// or import specific typeDefs only with ES6 Import
import { ScalarNameTypeDefinition } from 'graphql-scalars';
// or import specific typeDefs only with CommonJS
const { ScalarNameTypeDefinition } = require('graphql-scalars');
// or import all typeDefs once with ES6 Import
import { typeDefs as scalarTypeDefs } from 'graphql-scalars';
// or import all typeDefs once with CommonJS
const { typeDefs: scalarTypeDefs } = require('graphql-scalars');

const typeDefs = [
  ...scalarTypeDefs,
  // other typeDefs
];
// or
const typeDefs = [
  ScalarNameTypeDefinition,
  // other typeDefs
];

my current GraphQLModule:

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  typePaths: ['./**/**/**/*.graphql'],
  definitions: {
    path: join(process.cwd(), 'src/graphql.ts'),
    outputAs: 'class',
  },  
}),

How about the resolver map? Where should the code belong at? assets.resolver.ts? I also don't understand where this code belongs to?

In short, how to use graphql-scalars package in the Nestjs framework on Apollo Server? Is there any open-source GitHub repository to look into?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2025-02-08 20:45:50

在这里看看 nestjs导入自定义标量

我的app.module.ts外观:

import { BigIntResolver, DateResolver, DateTimeResolver } from 'graphql-scalars';

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  typePaths: ['./**/*.graphql'],
  definitions: {
    path: join(process.cwd(), 'src/graphql/graphql-types.ts'),
    customScalarTypeMapping: {
      BigInt: 'bigint',
      DateTime: 'Date',
    },
  },
  resolvers: {
    BigInt: BigIntResolver,
    Date: DateResolver,
    DateTime: DateTimeResolver,
  },
  playground: true,
  debug: true,
}),

在我的.graphql文件中,我可以使用这些类型:

scalar BigInt
scalar Date
scalar DateTime

input WorkperiodContent {
  editedAt: DateTime
  startDate: Date
  endDate: Date
}

完成此操作后,我可以使用这些新标量在GraphQl Playground上成功运行查询。

您甚至不需要创建自己的自定义标量。您只能导入所需的三个,就可以了。

Have a look here NestJs Import a custom scalar

This is how my app.module.ts looks:

import { BigIntResolver, DateResolver, DateTimeResolver } from 'graphql-scalars';

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  typePaths: ['./**/*.graphql'],
  definitions: {
    path: join(process.cwd(), 'src/graphql/graphql-types.ts'),
    customScalarTypeMapping: {
      BigInt: 'bigint',
      DateTime: 'Date',
    },
  },
  resolvers: {
    BigInt: BigIntResolver,
    Date: DateResolver,
    DateTime: DateTimeResolver,
  },
  playground: true,
  debug: true,
}),

In my .graphql file I can then use those types:

scalar BigInt
scalar Date
scalar DateTime

input WorkperiodContent {
  editedAt: DateTime
  startDate: Date
  endDate: Date
}

After I did this I could successfully run queries on the GraphQL Playground using those new scalars.

You don't even need to create your own custom scalar. You can just import the three that you need and you are good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文