Skip to content

Commit

Permalink
refactor: change DateTime types to Timestamp in schema (#3501)
Browse files Browse the repository at this point in the history
* refactor: change DateTime types to Timestamp in schema

* fix: dashboard timestamp

---------

Co-authored-by: Sandipan Dey <[email protected]>
  • Loading branch information
vindard and sandipndev authored Nov 3, 2023
1 parent a169051 commit a41b6dc
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 41 deletions.
4 changes: 2 additions & 2 deletions apps/dashboard/components/api-keys/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import RevokeKey from "./revoke"
import { apiKeys } from "@/services/graphql/queries/api-keys"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"

const formatDate = (dateStr: string): string => {
const formatDate = (timestamp: string): string => {
const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "2-digit",
}
return new Date(dateStr).toLocaleDateString(undefined, options)
return new Date(parseInt(timestamp) * 1000).toLocaleDateString(undefined, options)
}

const ApiKeysList = async () => {
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/services/graphql/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export type ApiKey = {
};

export type ApiKeyCreateInput = {
readonly expireInDays?: InputMaybe<Scalars['Int']['input']>;
readonly name: Scalars['String']['input'];
};

Expand Down
6 changes: 3 additions & 3 deletions core/api-keys/src/graphql/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ impl From<IdentityApiKey> for ApiKey {
name: key.name,
revoked: key.revoked,
expired: key.expired,
last_used_at: key.last_used_at,
created_at: key.created_at,
expires_at: key.expires_at,
last_used_at: key.last_used_at.map(Into::into),
created_at: key.created_at.into(),
expires_at: key.expires_at.into(),
}
}
}
Expand Down
44 changes: 40 additions & 4 deletions core/api-keys/src/graphql/schema.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
use async_graphql::*;
use chrono::{DateTime, Utc};
use chrono::{DateTime, TimeZone, Utc};

use crate::{app::ApiKeysApp, identity::IdentityApiKeyId};

pub struct AuthSubject {
pub id: String,
}

#[derive(Clone, Copy)]
pub struct Timestamp(DateTime<Utc>);

impl From<DateTime<Utc>> for Timestamp {
fn from(dt: DateTime<Utc>) -> Self {
Timestamp(dt)
}
}

impl Into<DateTime<Utc>> for Timestamp {
fn into(self) -> DateTime<Utc> {
self.0
}
}

#[Scalar(name = "Timestamp")]
impl ScalarType for Timestamp {
fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
let epoch = match &value {
async_graphql::Value::Number(n) => n
.as_i64()
.ok_or_else(|| async_graphql::InputValueError::expected_type(value)),
_ => Err(async_graphql::InputValueError::expected_type(value)),
}?;

Utc.timestamp_opt(epoch, 0)
.single()
.map(Timestamp)
.ok_or_else(|| async_graphql::InputValueError::custom("Invalid timestamp"))
}

fn to_value(&self) -> async_graphql::Value {
async_graphql::Value::Number(self.0.timestamp().into())
}
}

pub struct Query;

#[Object]
Expand All @@ -21,11 +57,11 @@ impl Query {
pub(super) struct ApiKey {
pub id: ID,
pub name: String,
pub created_at: DateTime<Utc>,
pub created_at: Timestamp,
pub revoked: bool,
pub expired: bool,
pub last_used_at: Option<DateTime<Utc>>,
pub expires_at: DateTime<Utc>,
pub last_used_at: Option<Timestamp>,
pub expires_at: Timestamp,
}

#[derive(SimpleObject)]
Expand Down
15 changes: 5 additions & 10 deletions core/api-keys/subgraph/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type ApiKey {
id: ID!
name: String!
createdAt: DateTime!
createdAt: Timestamp!
revoked: Boolean!
expired: Boolean!
lastUsedAt: DateTime
expiresAt: DateTime!
lastUsedAt: Timestamp
expiresAt: Timestamp!
}

input ApiKeyCreateInput {
Expand All @@ -23,13 +23,6 @@ input ApiKeyRevokeInput {
}


"""
Implement the DateTime<Utc> scalar
The input/output is a string in RFC3339 format.
"""
scalar DateTime




Expand All @@ -40,6 +33,8 @@ type Mutation {



scalar Timestamp

extend type User @key(fields: "id") {
id: ID! @external
apiKeys: [ApiKey!]!
Expand Down
15 changes: 4 additions & 11 deletions core/api/dev/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ type ApiKey
{
id: ID!
name: String!
createdAt: DateTime!
createdAt: Timestamp!
revoked: Boolean!
expired: Boolean!
lastUsedAt: DateTime
expiresAt: DateTime!
lastUsedAt: Timestamp
expiresAt: Timestamp!
}

input ApiKeyCreateInput
Expand Down Expand Up @@ -398,14 +398,6 @@ type Currency
symbol: String!
}

"""
Implement the DateTime<Utc> scalar
The input/output is a string in RFC3339 format.
"""
scalar DateTime
@join__type(graph: API_KEYS)

type DepositFeesInformation
@join__type(graph: PUBLIC)
{
Expand Down Expand Up @@ -1537,6 +1529,7 @@ type SuccessPayload
Timestamp field, serialized as Unix time (the number of seconds since the Unix epoch)
"""
scalar Timestamp
@join__type(graph: API_KEYS)
@join__type(graph: PUBLIC)

"""A time-based one-time password"""
Expand Down
15 changes: 4 additions & 11 deletions dev/config/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ type ApiKey
{
id: ID!
name: String!
createdAt: DateTime!
createdAt: Timestamp!
revoked: Boolean!
expired: Boolean!
lastUsedAt: DateTime
expiresAt: DateTime!
lastUsedAt: Timestamp
expiresAt: Timestamp!
}

input ApiKeyCreateInput
Expand Down Expand Up @@ -398,14 +398,6 @@ type Currency
symbol: String!
}

"""
Implement the DateTime<Utc> scalar
The input/output is a string in RFC3339 format.
"""
scalar DateTime
@join__type(graph: API_KEYS)

type DepositFeesInformation
@join__type(graph: PUBLIC)
{
Expand Down Expand Up @@ -1537,6 +1529,7 @@ type SuccessPayload
Timestamp field, serialized as Unix time (the number of seconds since the Unix epoch)
"""
scalar Timestamp
@join__type(graph: API_KEYS)
@join__type(graph: PUBLIC)

"""A time-based one-time password"""
Expand Down

0 comments on commit a41b6dc

Please sign in to comment.