/api/outfitImage makes direct GQL queries

Previously we were using HTTP queries to keep individual function bundle sizes small, but that doesn't matter in a server where all the code is shared!

The immediate motivation is that I want /api/outfitImage requesting against the same server, not impress-2020.openneo.net. For other stuff I'm probably gonna fix this by replacing VERCEL_URL with something else, but here I figured this was a change worth making anyway.
This commit is contained in:
Emi Matchu 2021-11-12 21:53:07 -08:00
parent afd23fb4dd
commit 991defffa1

View file

@ -35,11 +35,12 @@ const beeline = require("honeycomb-beeline")({
sampleRate: 10, sampleRate: 10,
}); });
import fetch from "node-fetch";
import gql from "graphql-tag"; import gql from "graphql-tag";
import { print as graphqlPrint } from "graphql/language/printer"; import { ApolloServer } from "apollo-server";
import { createTestClient } from "apollo-server-testing";
import connectToDb from "../../src/server/db"; import connectToDb from "../../src/server/db";
import { config as graphqlConfig } from "../../src/server";
import { renderOutfitImage } from "../../src/server/outfit-images"; import { renderOutfitImage } from "../../src/server/outfit-images";
import getVisibleLayers, { import getVisibleLayers, {
petAppearanceFragmentForGetVisibleLayers, petAppearanceFragmentForGetVisibleLayers,
@ -143,50 +144,35 @@ async function handle(req, res) {
return res.send(image); return res.send(image);
} }
const GRAPHQL_ENDPOINT = process.env.VERCEL_URL // Check out this scrappy way of making a query against server code ^_^`
? `https://${process.env.VERCEL_URL}/api/graphql` const graphqlClient = createTestClient(new ApolloServer(graphqlConfig));
: process.env.NODE_ENV === "development"
? "http://localhost:3000/api/graphql"
: "https://impress-2020.openneo.net/api/graphql";
// NOTE: Unlike in-app views, we only load PNGs here. We expect this to
// generally perform better, and be pretty reliable now that TNT is
// generating canonical PNGs for every layer!
const GRAPHQL_QUERY = gql`
query ApiOutfitImage($outfitId: ID!, $size: LayerImageSize) {
outfit(id: $outfitId) {
petAppearance {
layers {
id
imageUrl(size: $size)
}
...PetAppearanceForGetVisibleLayers
}
itemAppearances {
layers {
id
imageUrl(size: $size)
}
...ItemAppearanceForGetVisibleLayers
}
}
}
${petAppearanceFragmentForGetVisibleLayers}
${itemAppearanceFragmentForGetVisibleLayers}
`;
const GRAPHQL_QUERY_STRING = graphqlPrint(GRAPHQL_QUERY);
async function loadLayerUrlsForSavedOutfit(outfitId, size) { async function loadLayerUrlsForSavedOutfit(outfitId, size) {
const { errors, data } = await fetch(GRAPHQL_ENDPOINT, { const { errors, data } = await graphqlClient.query({
method: "POST", query: gql`
headers: { query ApiOutfitImage($outfitId: ID!, $size: LayerImageSize) {
"Content-Type": "application/json", outfit(id: $outfitId) {
}, petAppearance {
body: JSON.stringify({ layers {
query: GRAPHQL_QUERY_STRING, id
variables: { outfitId, size: `SIZE_${size}` }, imageUrl(size: $size)
}), }
}).then((res) => res.json()); ...PetAppearanceForGetVisibleLayers
}
itemAppearances {
layers {
id
imageUrl(size: $size)
}
...ItemAppearanceForGetVisibleLayers
}
}
}
${petAppearanceFragmentForGetVisibleLayers}
${itemAppearanceFragmentForGetVisibleLayers}
`,
variables: { outfitId, size: `SIZE_${size}` },
});
if (errors && errors.length > 0) { if (errors && errors.length > 0) {
throw new Error( throw new Error(