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

add date formatter example #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/src/graphql/events.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export const schema = gql`
capacity: Int
createdAt: DateTime!
updatedAt: DateTime!

formattedStartAt: String
formattedEndAt: String
formattedRegistrationStartAt: String
formattedRegistrationEndAt: String
formattedCreatedAt: String
formattedUpdatedAt: String

sessions: [EventSession]
speakers: [EventSpeaker]
registrants: [EventRegistrant]
Expand Down
7 changes: 7 additions & 0 deletions api/src/lib/formatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { utcToZonedTime, format } from 'date-fns-tz';

export function formatDateTime(dt: Date, tz?: string): string {
return format(utcToZonedTime(dt, tz), 'MMM d, yyyy hh:mmaaa zzz', {
timeZone: tz || 'Etc/UTC',
});
}
27 changes: 25 additions & 2 deletions api/src/services/events/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { QueryResolvers, MutationResolvers } from 'types/graphql';
import type {
QueryResolvers,
MutationResolvers,
EventRelationResolvers,
} from 'types/graphql';

import { db } from 'src/lib/db';
import { formatDateTime } from 'src/lib/formatters';

export const events: QueryResolvers['events'] = () => {
return db.event.findMany();
Expand Down Expand Up @@ -34,7 +39,25 @@ export const deleteEvent: MutationResolvers['deleteEvent'] = ({ id }) => {
});
};

export const Event = {
export const Event: EventRelationResolvers = {
formattedStartAt: (_obj, { root }) => {
return formatDateTime(root.startAt, root.timezone);
},
formattedEndAt: (_obj, { root }) => {
return formatDateTime(root.endAt, root.timezone);
},
formattedRegistrationStartAt: (_obj, { root }) => {
return formatDateTime(root.registrationStartAt, root.timezone);
},
formattedRegistrationEndAt: (_obj, { root }) => {
return formatDateTime(root.registrationEndAt, root.timezone);
},
formattedCreatedAt: (_obj, { root }) => {
return formatDateTime(root.createdAt, root.timezone);
},
formattedUpdatedAt: (_obj, { root }) => {
return formatDateTime(root.updatedAt, root.timezone);
},
sessions: (_obj, { root }) => {
return db.event.findUnique({ where: { id: root?.id } }).sessions();
},
Expand Down