From 991defffa1996dfd74d8b8241a7124e7f060985a Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 12 Nov 2021 21:53:07 -0800 Subject: [PATCH] /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. --- pages/api/outfitImage.js | 74 ++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/pages/api/outfitImage.js b/pages/api/outfitImage.js index 5e67f00..d1386b1 100644 --- a/pages/api/outfitImage.js +++ b/pages/api/outfitImage.js @@ -35,11 +35,12 @@ const beeline = require("honeycomb-beeline")({ sampleRate: 10, }); -import fetch from "node-fetch"; 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 { config as graphqlConfig } from "../../src/server"; import { renderOutfitImage } from "../../src/server/outfit-images"; import getVisibleLayers, { petAppearanceFragmentForGetVisibleLayers, @@ -143,50 +144,35 @@ async function handle(req, res) { return res.send(image); } -const GRAPHQL_ENDPOINT = process.env.VERCEL_URL - ? `https://${process.env.VERCEL_URL}/api/graphql` - : 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); +// Check out this scrappy way of making a query against server code ^_^` +const graphqlClient = createTestClient(new ApolloServer(graphqlConfig)); async function loadLayerUrlsForSavedOutfit(outfitId, size) { - const { errors, data } = await fetch(GRAPHQL_ENDPOINT, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: GRAPHQL_QUERY_STRING, - variables: { outfitId, size: `SIZE_${size}` }, - }), - }).then((res) => res.json()); + const { errors, data } = await graphqlClient.query({ + 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} + `, + variables: { outfitId, size: `SIZE_${size}` }, + }); if (errors && errors.length > 0) { throw new Error(