3
votes

Thanks to the package @nestjs/graphql I can create separate .graphql files in each module of my application and the Nestjs system gathers them all and introduces them to Apolo server well, but the problem comes when I try to make custom scalars, I do them as is his tutorial says:

https://docs.nestjs.com/graphql/scalars

But I can not find a way to import them into a .graphql file so I could use it within that file. and since the file is .graphql the import does not work. Do you know how I can import a custom scalar to be used within a .graphql file? Something that can serve as an example is an example that they use:

https://github.com/nestjs/nest/tree/master/sample/12-graphql-apollo

As you can see them in their example they expose that they have a module called cats, where inside the module they have their cats.resolvers.ts and their cats.graphql; and on the other hand, they have their date.scalar.ts defined. The question using this example:

What should I do to use the DateScalar in a property of cats.resolvers.ts?

1

1 Answers

1
votes

I had that problem and solved it in the following way:

  • First, I made the common module to set the DateScalar as provider and also to export it:


    import { Module } from '@nestjs/common';
    import { DateScalar } from './scalars/date.scalar';

    @Module({
        providers: [DateScalar],
        exports: [DateScalar]
    })
    export class CommonModule {}

  • Later, I imported the common module to a cats module so that the DateScalar is available throughout the cats' module.

  • And finally I established the following line inside cats.graphql:



    scalar Date

    type Cat {
      id: Int
      name: String
      age: Int
      birthday: Date
    }

Put attention that the Scale Date value within the cats.graphql file must match the value set in @Scalar ('Date') in the date.scalar.ts file