1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

javascript timestampでgraphql int型はエラーだった話

Posted at

nodejs timestampでgraphql int型はエラーだった話

背景

nextjs examplesを試していた時に、timestamp値をGraphql Intで定義してあり、エラーとなったための対応。

原因

GraphqlのInt型は、A signed 32‐bit integer.となっており、Date.now()を利用した場合に32-bitを超えてしまい、下記のようなエラーがでる。

スクリーンショット 2024-10-29 11.08.52.png

"message": "Int cannot represent non 32-bit signed integer value: XXX",
% Date.now()
1730166391048

1. 対応手段

1-1. Int型からFloat型に変更する

schema.graphql
type User {
  id: ID!
  email: String!
  createdAt: Int!
}

schema.graphql
type User {
  id: ID!
  email: String!
  createdAt: Float!
}

に変更する

1-2. timestamp型からString型に変更する

schema.graphql
type User {
  id: ID!
  email: String!
  createdAt: String!
}
sample.ts
new Date(now).toISOString()

1-3. Cutom Scalar型を作成する

DateTime型を利用なども可能です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?