Graphql: How do we import a custom scalar into a .graphql file in NestJs?

Created on 19 Jan 2019  路  2Comments  路  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I do them as is your 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. Missing explain how to import a custom scalar to be used within a .graphql file.

Expected behavior

I propose that we use your own example:

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

In your example you expose that you have a module called cats, where inside the module you have your cats.resolvers.ts and your cats.graphql; and on the other hand, you have your date.scalar.ts defined. The question using this example:

What should we do to use the DateScalar in a property of cats.graphql?

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: X.Y.Z


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Most helpful comment

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

All 2 comments

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

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings