Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scalar DateTime from graphql-scalars #8412

Closed
go-aegian opened this issue Sep 29, 2022 · 1 comment
Closed

scalar DateTime from graphql-scalars #8412

go-aegian opened this issue Sep 29, 2022 · 1 comment
Assignees
Labels
kind/question Improvements or additions to documentation

Comments

@go-aegian
Copy link

Describe the bug

how to define on typescript client side graphql-scalars.GraphQLDateTime. Using KitQL instead of Apollo

schema contains scalar definition

scalar Time

type List {
from: Time!
to: Time
}

codegen configured as
...
config:
scalars:
Time: import('graphql-scalars').GraphQLDateTime


npm run generate - generates the codegen.ts

import type { GraphQLDateTime } from 'graphql-scalars';

export type Scalars = {
/// TS2749: 'GraphQLDateTime' refers to a value, but is being used as a type here. Did you mean 'typeof GraphQLDateTime'?
Time: GraphQLDateTime;
};

What am I missing in order to use client side typescript datetime scalar?

Your Example Website or App

don't have one

Steps to Reproduce the Bug or Issue

see bug description for the question

Expected behavior

Expecting to be able to get a json response with fields typed as Time their value to be converted to JS Date object instead of string

current response example:
...
"from": "2021-01-27T14:00:00-04:00",
"to": "2021-01-29T15:00:00-04:00"
...

Screenshots or Videos

No response

Platform

"@graphql-codegen/cli": "^2.6.2",
"@graphql-codegen/near-operation-file-preset": "^2.2.12",
"@graphql-codegen/typed-document-node": "^2.2.8",
"@graphql-codegen/typescript": "^2.4.11",
"@graphql-codegen/typescript-operations": "^2.3.5",
"@kitql/client": "^0.5.2",
"@kitql/graphql-codegen": "^0.5.1",
"graphql": "^15.8.0",
"graphql-request": "^4.2.0",
"graphql-scalars": "^1.18.0",
"graphql-upload": "^13.0.0",

Codegen Config File

schema:

overwrite: true

documents: './src/graphql/**/*.gql'

generates:
./src/graphql/codegen.ts:
plugins:
- typescript
- typescript-operations
- typed-document-node
- '@kitql/graphql-codegen'

config:
useTypeImports: true
scalars:
Time: import('graphql-scalars').GraphQLDateTime

hooks:
afterAllFileWrite:
- prettier --write

Additional context

No response

@charlypoly
Copy link
Contributor

charlypoly commented Sep 29, 2022

Hi @go-aegian,

Here, GraphQLDateTime refers to a class, not a type.
Instead, you should refer to GraphQLScalarType (from graphql), which is not exactly the type that you want to manipulate on the client side since the response will be returned by this scalar's serialize() function (which returns a Date type).
For this reason, please know that graphql-scalar is not meant to be used on the client side.
The only way to currently integrate graphql-scalars with codegen is to provide the mapping manually: https://www.the-guild.dev/graphql/scalars/docs/quick-start#graphql-code-generator-integration.

We are working on providing a better integration; you can follow the progress here: #8296

@charlypoly charlypoly added the kind/question Improvements or additions to documentation label Sep 29, 2022
@charlypoly charlypoly self-assigned this Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants