Skip to content

Commit d533f3f

Browse files
committed
Use enums as types in the GraphQL codegen
1 parent ab86947 commit d533f3f

14 files changed

+133
-165
lines changed

frontend/codegen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const config: CodegenConfig = {
1515
preset: "client",
1616
config: {
1717
useTypeImports: true,
18+
enumsAsTypes: true,
1819
// By default, unknown scalars are generated as `any`. This is not ideal for catching potential bugs.
1920
defaultScalarType: "unknown",
2021
scalars: {

frontend/src/components/BrowserSession.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { useTranslation } from "react-i18next";
1414
import { useMutation } from "urql";
1515

1616
import { type FragmentType, graphql, useFragment } from "../gql";
17-
import { DeviceType } from "../gql/graphql";
17+
import type { DeviceType } from "../gql/graphql";
1818

1919
import DateTime from "./DateTime";
2020
import EndSessionButton from "./Session/EndSessionButton";
@@ -97,7 +97,7 @@ const BrowserSession: React.FC<Props> = ({ session, isCurrent }) => {
9797

9898
const onSessionEnd = useEndBrowserSession(data.id, isCurrent);
9999

100-
const deviceType = data.userAgent?.deviceType ?? DeviceType.Unknown;
100+
const deviceType = data.userAgent?.deviceType ?? "UNKNOWN";
101101

102102
let deviceName: string | null = null;
103103
let clientName: string | null = null;

frontend/src/components/CompatSession.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const CompatSession: React.FC<{
8686
? simplifyUrl(data.ssoLogin.redirectUri)
8787
: undefined;
8888

89-
const deviceType = data.userAgent?.deviceType ?? DeviceType.Unknown;
89+
const deviceType = data.userAgent?.deviceType ?? "UNKNOWN";
9090

9191
const deviceName =
9292
data.userAgent?.model ??

frontend/src/components/OAuth2Session.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { beforeAll, describe, expect, it } from "vitest";
1212
import { never } from "wonka";
1313

1414
import { makeFragmentData } from "../gql";
15-
import { Oauth2ApplicationType } from "../gql/graphql";
15+
import type { Oauth2ApplicationType } from "../gql/graphql";
1616
import { mockLocale } from "../test-utils/mockLocale";
1717
import { DummyRouter } from "../test-utils/router";
1818

@@ -34,7 +34,7 @@ describe("<OAuth2Session />", () => {
3434
clientId: "test-client-id",
3535
clientName: "Element",
3636
clientUri: "https://element.io",
37-
applicationType: Oauth2ApplicationType.Web,
37+
applicationType: "WEB" as Oauth2ApplicationType,
3838
},
3939
};
4040

@@ -80,7 +80,7 @@ describe("<OAuth2Session />", () => {
8080
finishedAt,
8181
client: {
8282
...defaultSession.client,
83-
applicationType: Oauth2ApplicationType.Native,
83+
applicationType: "NATIVE",
8484
},
8585
},
8686
FRAGMENT,

frontend/src/components/OAuth2Session.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useTranslation } from "react-i18next";
99
import { useMutation } from "urql";
1010

1111
import { type FragmentType, graphql, useFragment } from "../gql";
12-
import { DeviceType, Oauth2ApplicationType } from "../gql/graphql";
12+
import type { DeviceType, Oauth2ApplicationType } from "../gql/graphql";
1313
import { getDeviceIdFromScope } from "../utils/deviceIdFromScope";
1414

1515
import DateTime from "./DateTime";
@@ -58,13 +58,13 @@ export const END_SESSION_MUTATION = graphql(/* GraphQL */ `
5858
const getDeviceTypeFromClientAppType = (
5959
appType?: Oauth2ApplicationType | null,
6060
): DeviceType => {
61-
if (appType === Oauth2ApplicationType.Web) {
62-
return DeviceType.Pc;
61+
if (appType === "WEB") {
62+
return "PC";
6363
}
64-
if (appType === Oauth2ApplicationType.Native) {
65-
return DeviceType.Mobile;
64+
if (appType === "NATIVE") {
65+
return "MOBILE";
6666
}
67-
return DeviceType.Unknown;
67+
return "UNKNOWN";
6868
};
6969

7070
type Props = {
@@ -88,7 +88,7 @@ const OAuth2Session: React.FC<Props> = ({ session }) => {
8888
: undefined;
8989

9090
const deviceType =
91-
(data.userAgent?.deviceType === DeviceType.Unknown
91+
(data.userAgent?.deviceType === "UNKNOWN"
9292
? null
9393
: data.userAgent?.deviceType) ??
9494
getDeviceTypeFromClientAppType(data.client.applicationType);

frontend/src/components/Session/DeviceTypeIcon.stories.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ const meta = {
1515
component: DeviceTypeIcon,
1616
tags: ["autodocs"],
1717
args: {
18-
deviceType: DeviceType.Unknown,
18+
deviceType: "UNKNOWN",
1919
},
2020
argTypes: {
2121
deviceType: {
2222
control: "select",
23-
options: [
24-
DeviceType.Unknown,
25-
DeviceType.Pc,
26-
DeviceType.Mobile,
27-
DeviceType.Tablet,
28-
],
23+
options: ["UNKNOWN", "PC", "MOBILE", "TABLET"],
2924
},
3025
},
3126
} satisfies Meta<typeof DeviceTypeIcon>;
@@ -37,16 +32,16 @@ export const Unknown: Story = {};
3732

3833
export const Pc: Story = {
3934
args: {
40-
deviceType: DeviceType.Pc,
35+
deviceType: "PC",
4136
},
4237
};
4338
export const Mobile: Story = {
4439
args: {
45-
deviceType: DeviceType.Mobile,
40+
deviceType: "MOBILE",
4641
},
4742
};
4843
export const Tablet: Story = {
4944
args: {
50-
deviceType: DeviceType.Tablet,
45+
deviceType: "TABLET",
5146
},
5247
};

frontend/src/components/Session/DeviceTypeIcon.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import IconBrowser from "@vector-im/compound-design-tokens/assets/web/icons/web-
1111
import type { FunctionComponent, SVGProps } from "react";
1212
import { useTranslation } from "react-i18next";
1313

14-
import { DeviceType } from "../../gql/graphql";
14+
import type { DeviceType } from "../../gql/graphql";
1515

1616
import styles from "./DeviceTypeIcon.module.css";
1717

1818
const deviceTypeToIcon: Record<
1919
DeviceType,
2020
FunctionComponent<SVGProps<SVGSVGElement> & { title?: string | undefined }>
2121
> = {
22-
[DeviceType.Unknown]: IconUnknown,
23-
[DeviceType.Pc]: IconComputer,
24-
[DeviceType.Mobile]: IconMobile,
25-
[DeviceType.Tablet]: IconBrowser,
22+
UNKNOWN: IconUnknown,
23+
PC: IconComputer,
24+
MOBILE: IconMobile,
25+
TABLET: IconBrowser,
2626
};
2727

2828
const DeviceTypeIcon: React.FC<{ deviceType: DeviceType }> = ({
@@ -33,10 +33,10 @@ const DeviceTypeIcon: React.FC<{ deviceType: DeviceType }> = ({
3333
const Icon = deviceTypeToIcon[deviceType];
3434

3535
const deviceTypeToLabel: Record<DeviceType, string> = {
36-
[DeviceType.Unknown]: t("frontend.device_type_icon_label.unknown"),
37-
[DeviceType.Pc]: t("frontend.device_type_icon_label.pc"),
38-
[DeviceType.Mobile]: t("frontend.device_type_icon_label.mobile"),
39-
[DeviceType.Tablet]: t("frontend.device_type_icon_label.tablet"),
36+
UNKNOWN: t("frontend.device_type_icon_label.unknown"),
37+
PC: t("frontend.device_type_icon_label.pc"),
38+
MOBILE: t("frontend.device_type_icon_label.mobile"),
39+
TABLET: t("frontend.device_type_icon_label.tablet"),
4040
};
4141

4242
const label = deviceTypeToLabel[deviceType];

frontend/src/components/SessionCard/SessionCard.stories.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import IconSignOut from "@vector-im/compound-design-tokens/assets/web/icons/sign
99
import { Button } from "@vector-im/compound-web";
1010
import { useTranslation } from "react-i18next";
1111

12-
import { DeviceType } from "../../gql/graphql";
12+
import type { DeviceType } from "../../gql/graphql";
1313

1414
import * as Card from "./SessionCard";
1515

@@ -51,10 +51,13 @@ const meta = {
5151
disabled: false,
5252
deviceName: "MacBook Pro 16",
5353
clientName: "Firefox",
54-
deviceType: DeviceType.Pc,
54+
deviceType: "PC",
5555
},
5656
argTypes: {
57-
deviceType: { control: "select", options: Object.values(DeviceType) },
57+
deviceType: {
58+
control: "select",
59+
options: ["PC", "MOBILE", "TABLET", "UNKNOWN"],
60+
},
5861
disabled: { control: "boolean" },
5962
deviceName: { control: "text" },
6063
clientName: { control: "text" },

frontend/src/components/UserGreeting/UserGreeting.stories.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import { Provider } from "urql";
99
import { delay, fromValue, pipe } from "wonka";
1010

1111
import { makeFragmentData } from "../../gql";
12-
import {
13-
type SetDisplayNameMutation,
14-
SetDisplayNameStatus,
15-
} from "../../gql/graphql";
12+
import type { SetDisplayNameMutation } from "../../gql/graphql";
1613

1714
import UserGreeting, { CONFIG_FRAGMENT, FRAGMENT } from "./UserGreeting";
1815

@@ -31,7 +28,7 @@ const Template: React.FC<{
3128
fromValue({
3229
data: {
3330
setDisplayName: {
34-
status: SetDisplayNameStatus.Set,
31+
status: "SET",
3532
user: { id: userId, matrix: { displayName } },
3633
},
3734
},

frontend/src/components/UserGreeting/UserGreeting.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const UserGreeting: React.FC<Props> = ({ user, siteConfig }) => {
107107

108108
const result = await setDisplayName({ displayName, userId: data.id });
109109

110-
if (result.data?.setDisplayName.status === SetDisplayNameStatus.Set) {
110+
if (result.data?.setDisplayName.status === "SET") {
111111
setOpen(false);
112112
}
113113
};
@@ -163,8 +163,7 @@ const UserGreeting: React.FC<Props> = ({ user, siteConfig }) => {
163163
<Form.Field
164164
name="displayname"
165165
serverInvalid={
166-
setDisplayNameResult.data?.setDisplayName.status ===
167-
SetDisplayNameStatus.Invalid
166+
setDisplayNameResult.data?.setDisplayName.status === "INVALID"
168167
}
169168
>
170169
<Form.Label>

0 commit comments

Comments
 (0)