Extend the graphql schema more

This commit is contained in:
Jordan Eldredge 2022-02-20 01:41:19 -08:00
parent 77ad5ce70a
commit 55094d8041
3 changed files with 51 additions and 2 deletions

View file

@ -3,7 +3,9 @@ import { graphqlHTTP } from "express-graphql";
import { buildSchema } from "graphql";
import SkinModel from "../../data/SkinModel";
import TweetModel from "../../data/TweetModel";
import SkinResolver from "./resolvers/SkinResolver";
import TweetResolver from "./resolvers/TweetResolver";
import UserResolver from "./resolvers/UserResolver";
import SkinsConnection from "./SkinsConnection";
import TweetsConnection from "./TweetsConnection";
@ -11,7 +13,6 @@ import TweetsConnection from "./TweetsConnection";
const router = Router();
const schema = buildSchema(`
"""A classic Winamp skin"""
type Skin {
"""Database ID of the skin"""
@ -97,11 +98,29 @@ type Review {
type ArchiveFile {
"""Filename of the file within the archive"""
filename: String,
"""
A URL to download the file. **Note:** This is powered by a little
serverless Cloudflare function which tries to exctact the file on the fly.
It may not work for all files.
"""
url: String
"""
The date on the file inside the archive. Given in simplified extended ISO
format (ISO 8601).
"""
date: String
}
"""A tweet made by @winampskins mentioning a Winamp skin"""
type Tweet {
"""URL of the tweet"""
"""
URL of the tweet. **Note:** Early on in the bot's life we just recorded
_which_ skins were tweeted, not any info about the actual tweet. This means we
don't always know the URL of the tweet.
"""
url: String
"""Number of likes the tweet has received. Updated nightly. (Note: Recent likes on older tweets may not be reflected here)"""
@ -178,6 +197,9 @@ type Query {
"""Get a skin by its MD5 hash"""
fetch_skin_by_md5(md5: String!): Skin
"""Get a tweet by its URL"""
fetch_tweet_by_url(url: String!): Tweet
"""
All skins in the database
@ -208,6 +230,13 @@ const root = {
}
return new SkinResolver(skin);
},
async fetch_tweet_by_url({ url }, { ctx }) {
const tweet = await TweetModel.fromAnything(ctx, url);
if (tweet == null) {
return null;
}
return new TweetResolver(tweet);
},
async skins({ first, offset, sort, filter }) {
if (first > 1000) {
throw new Error("Maximum limit is 1000");

View file

@ -8,4 +8,10 @@ export default class ArchiveFileResolver {
filename() {
return this._model.getFileName();
}
url() {
return this._model.getUrl();
}
date() {
return this._model.getFileDate().toISOString();
}
}

View file

@ -2,6 +2,7 @@ import UserContext, { ctxWeakMapMemoize } from "./UserContext";
import { ArchiveFileRow } from "../types";
import DataLoader from "dataloader";
import { knex } from "../db";
import SkinModel from "./SkinModel";
export type ArchiveFileDebugData = {
row: ArchiveFileRow;
@ -18,6 +19,10 @@ export default class ArchiveFileModel {
return rows.map((row) => new ArchiveFileModel(ctx, row));
}
getMd5(): string {
return this.row.skin_md5;
}
getFileName(): string {
return this.row.file_name;
}
@ -26,6 +31,15 @@ export default class ArchiveFileModel {
return new Date(this.row.file_date);
}
getUrl(): string {
const filename = encodeURIComponent(this.getFileName());
return `https://zip-worker.jordan1320.workers.dev/zip/${this.getMd5()}/${filename}`;
}
async getSkin(): Promise<SkinModel> {
return SkinModel.fromMd5Assert(this.ctx, this.getMd5());
}
async debug(): Promise<ArchiveFileDebugData> {
return {
row: this.row,