Skip to content

Commit

Permalink
Merge pull request #1070 from visualize-admin/perf/only-send-gql-exte…
Browse files Browse the repository at this point in the history
…nsions-when-needed

perf: Only send GQL extensions to the client when needed
  • Loading branch information
bprusinowski authored Jun 20, 2023
2 parents 8e13b1c + 146dfef commit 4080885
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

## Unreleased

Nothing yet.
- Performance
- The debug data is now only sent along with the payload when the debug mode is enabled

# [3.20.1] - 2023-06-19

Expand Down
16 changes: 11 additions & 5 deletions app/graphql/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ export const client = createClient({
url: GRAPHQL_ENDPOINT,
exchanges: [...devtoolsExchanges, ...defaultExchanges],
fetchOptions: {
headers: {
"x-visualize-cache-control": flag("server-side-cache.disable")
? "no-cache"
: "",
},
headers: getHeaders(),
},
});

function getHeaders() {
const debug = flag("debug");
const disableCache = flag("server-side-cache.disable");

return {
"x-visualize-debug": debug ? "true" : "",
"x-visualize-cache-control": disableCache ? "no-cache" : "",
};
}
6 changes: 6 additions & 0 deletions app/graphql/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ const shouldUseServerSideCache = (req: IncomingMessage) => {
return req.headers["x-visualize-cache-control"] !== "no-cache";
};

const isDebugMode = (req: IncomingMessage) => {
return req.headers["x-visualize-debug"] === "true";
};

const createContextContent = async ({
sourceUrl,
locale,
Expand Down Expand Up @@ -157,9 +161,11 @@ const createContextContent = async ({
};

export const createContext = ({ req }: { req: IncomingMessage }) => {
const debug = isDebugMode(req);
let setupping: ReturnType<typeof createContextContent>;

const ctx = {
debug,
// Stores meta information on queries that have been made during the request
queries: [] as RequestQueryMeta[],
timings: undefined as Timings | undefined,
Expand Down
14 changes: 9 additions & 5 deletions app/pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApolloServer } from "apollo-server-micro";
import "global-agent/bootstrap";
import configureCors from "cors";
import "global-agent/bootstrap";
import { NextApiRequest, NextApiResponse } from "next";

import { setupFlamegraph } from "../../gql-flamegraph/resolvers";
Expand All @@ -24,10 +24,14 @@ const server = new ApolloServer({
},
formatResponse: (response, reqCtx) => {
const context = reqCtx.context as VisualizeGraphQLContext;
response.extensions = {
queries: context.queries,
timings: context.timings,
};

if (context.debug) {
response.extensions = {
queries: context.queries,
timings: context.timings,
};
}

return response;
},
context: createContext,
Expand Down

1 comment on commit 4080885

@vercel
Copy link

@vercel vercel bot commented on 4080885 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app

Please sign in to comment.