Skip to content

Commit

Permalink
feat: allow playground consumers to customize headers (wundergraph#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
clayne11 authored Sep 18, 2024
1 parent db59706 commit 80a7755
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions playground/src/components/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ const validateHeaders = (headers: Record<string, string>) => {
}
};

type GraphiQLScripts = {
transformHeaders?: (headers: Record<string, string>) => Record<string, string>;
};

const graphiQLFetch = async (
schema: GraphQLSchema | null,
clientValidationEnabled: boolean,
scripts: GraphiQLScripts | undefined,
onFetch: any,
url: URL,
init: RequestInit,
) => {
try {
const headers: Record<string, string> = {
...(init.headers as Record<string, string>),
};
const initialHeaders = init.headers as Record<string, string>;
const headers: Record<string, string> = scripts?.transformHeaders
? scripts.transformHeaders(initialHeaders)
: { ...initialHeaders };

validateHeaders(headers);

Expand Down Expand Up @@ -263,6 +269,8 @@ export const Playground = (input: {
routingUrl?: string;
hideLogo?: boolean;
theme?: 'light' | 'dark' | undefined;
scripts?: GraphiQLScripts;
fetch?: typeof fetch;
}) => {
const url = input.routingUrl || import.meta.env.VITE_ROUTING_URL || '{{graphqlURL}}';

Expand Down Expand Up @@ -366,7 +374,8 @@ export const Playground = (input: {
});

const getSchema = async () => {
const res = await fetch(url, {
const fetchFunc = input.fetch ? input.fetch : fetch;
const res = await fetchFunc(url, {
body: JSON.stringify({
operationName: 'IntrospectionQuery',
query: getIntrospectionQuery(),
Expand All @@ -390,7 +399,7 @@ export const Playground = (input: {
url: url,
subscriptionUrl: window.location.protocol.replace('http', 'ws') + '//' + window.location.host + url,
fetch: (...args) =>
graphiQLFetch(schema, clientValidationEnabled, onFetch, args[0] as URL, args[1] as RequestInit),
graphiQLFetch(schema, clientValidationEnabled, input.scripts, onFetch, args[0] as URL, args[1] as RequestInit),
});
}, [schema, clientValidationEnabled]);

Expand Down
2 changes: 1 addition & 1 deletion router/internal/graphiql/graphiql.html

Large diffs are not rendered by default.

0 comments on commit 80a7755

Please sign in to comment.