metaforecast/src/graphql/schema/platforms.ts
2022-05-12 18:59:07 +04:00

56 lines
1.6 KiB
TypeScript

import { prisma } from "../../backend/database/prisma";
import { platforms } from "../../backend/platforms/registry";
import { builder } from "../builder";
export const PlatformObj = builder.objectRef<string>("Platform").implement({
description: "Forecasting platform supported by Metaforecast",
fields: (t) => ({
id: t.id({
description: 'Short unique platform name, e.g. "xrisk"',
resolve: (x) => x,
}),
label: t.string({
description:
'Platform name for displaying on frontend etc., e.g. "X-risk estimates"',
resolve: (platformName) => {
if (platformName === "metaforecast") {
return "Metaforecast";
}
if (platformName === "guesstimate") {
return "Guesstimate";
}
// kinda slow and repetitive, TODO - store a map {name => platform} somewhere and `getPlatform` util function?
const platform = platforms.find((p) => p.name === platformName);
if (!platform) {
throw new Error(`Unknown platform ${platformName}`);
}
return platform.label;
},
}),
lastUpdated: t.field({
type: "Date",
nullable: true,
resolve: async (platformName) => {
const res = await prisma.question.aggregate({
where: {
platform: platformName,
},
_max: {
timestamp: true,
},
});
return res._max.timestamp;
},
}),
}),
});
builder.queryField("platforms", (t) =>
t.field({
type: [PlatformObj],
resolve: async (parent, args) => {
return platforms.map((platform) => platform.name);
},
})
);