From 501c687945de6165f51dc5c5c084d35bb909b871 Mon Sep 17 00:00:00 2001 From: veryCrunchy Date: Fri, 10 May 2024 00:23:14 +0200 Subject: [PATCH] feat: getTwitchUser --- src/gql/resolvers/getTwitchUser.ts | 32 ++++++++++++++++++++++++++++++ src/gql/resolvers/index.ts | 2 ++ src/gql/schema.ts | 14 +++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/gql/resolvers/getTwitchUser.ts diff --git a/src/gql/resolvers/getTwitchUser.ts b/src/gql/resolvers/getTwitchUser.ts new file mode 100644 index 0000000..86d1923 --- /dev/null +++ b/src/gql/resolvers/getTwitchUser.ts @@ -0,0 +1,32 @@ +import axios from "axios"; +import { type Resolvers } from "."; +import { getClientToken } from "~/auth"; + +const watchtimeResolver: Resolvers["Query"] = { + getTwitchUser: async (_, query) => { + const queryString = `${isNaN(Number(query.user)) ? "login" : "id"}=${query.user}`; + + const res = await axios.get( + "https://api.twitch.tv/helix/users?" + queryString, + { + headers: { + Authorization: `Bearer ${await getClientToken()}`, + "Client-Id": process.env.TWITCH_CLIENT_ID, + }, + } + ), + userData = res.data.data as { + id: string; + login: string; + display_name: string; + type: string; + broadcaster_type: string; + description: string; + profile_image_url: string; + view_count: number; + created_at: string; + }[]; + return userData; + }, +}; +export default watchtimeResolver; diff --git a/src/gql/resolvers/index.ts b/src/gql/resolvers/index.ts index a9cdc5c..79b5676 100644 --- a/src/gql/resolvers/index.ts +++ b/src/gql/resolvers/index.ts @@ -2,6 +2,7 @@ import { mutationType, queryType } from "../schema"; import { contentQuery, contentMutation } from "./content"; import { whitelistQuery, whitelistMutation } from "./whitelist"; import watchtime from "./watchtime"; +import getTwitchUser from "./getTwitchUser"; import me from "./me"; import { InferResolvers } from "garph"; @@ -16,6 +17,7 @@ export const resolvers = { ...contentQuery, ...me, ...watchtime, + ...getTwitchUser, ...whitelistQuery, }, diff --git a/src/gql/schema.ts b/src/gql/schema.ts index a846488..77a489e 100644 --- a/src/gql/schema.ts +++ b/src/gql/schema.ts @@ -21,6 +21,17 @@ const UserType = g.type("User", { createdAt: dateType, updatedAt: dateType, }); +const TwitchUserType = g.type("TwitchUser", { + id: g.id(), + login: g.string(), + display_name: g.string(), + type: g.string(), + broadcaster_type: g.string(), + description: g.string(), + profile_image_url: g.string(), + view_count: g.int(), + created_at: g.string(), +}); const WatchtimeType = g.type("Watchtime", { time: g.int(), @@ -40,6 +51,9 @@ export const queryType = g.type("Query", { .args({ limit: g.int().default(10), }), + getTwitchUser: g.ref(TwitchUserType).list().args({ + user: g.id(), + }), checkWhitelist: g.boolean(), checkWhitelistByUUID: g.boolean().args({