Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add headers and onEditHeaders to Ruru object #2184

Open
danzho opened this issue Sep 26, 2024 · 1 comment
Open

Add headers and onEditHeaders to Ruru object #2184

danzho opened this issue Sep 26, 2024 · 1 comment

Comments

@danzho
Copy link

danzho commented Sep 26, 2024

Feature description

Ruru supports programmatically setting and reading the query and variables UI panes:

/**
* The query to prepopulate the editor with.
*/
query?: string;
/**
* The variables to prepopulate the editor with.
*/
variables?: string;
/**
* Callback executed when the current query changes.
*/
onEditQuery?: GraphiQLProps["onEditQuery"];
/**
* Callback executed when the variables change.
*/
onEditVariables?: GraphiQLProps["onEditVariables"];

But it'd be nice if it also let you read/write the headers pane too.

Motivating example

Like you showed on Discord, you can write a grafserv plugin that sets the query and variables from ruru url:

const RuruQueryParamsPlugin: GraphileConfig.Plugin = {
name: "RuruQueryParamsPlugin",
version: "0.0.0",
grafserv: {
middleware: {
ruruHTMLParts(next, event) {
const { htmlParts } = event;
htmlParts.headerScripts += `
<script>
{
const currentUrl = new URL(document.URL);
const query = currentUrl.searchParams.get("query");
const variables = currentUrl.searchParams.get("variables");
if (query) {
RURU_CONFIG.query = query;
RURU_CONFIG.variables = variables;
}
}
</script>
`;
return next();
},
},
},
};
/**
* Update the URL search params with the current query and variables
*/
const RuruQueryParamsUpdatePlugin: GraphileConfig.Plugin = {
name: "RuruQueryParamsUpdatePlugin",
version: "0.0.0",
grafserv: {
middleware: {
ruruHTMLParts(next, event) {
const { htmlParts } = event;
htmlParts.headerScripts += `
<script>
{
const currentUrl = new URL(document.URL);
RURU_CONFIG.onEditQuery = (query) => {
currentUrl.searchParams.set("query", query);
window.history.replaceState(null, "", currentUrl);
};
RURU_CONFIG.onEditVariables = (variables) => {
currentUrl.searchParams.set("variables", variables);
window.history.replaceState(null, "", currentUrl);
};
}
</script>
`;
},
},
},
};

I'd like to also be able to set the headers too. That would let me create clickable query links that also set {"Authorization": "Bearer {token}"} so that I can easily run authenticated queries without having to paste the auth token myself for debugging.

Of course, the plugin would look something like this:

const url = new URL(document.URL)
const params = new URLSearchParams(url.hash.slice(1));

const query = params.get("query");
const variables = params.get("variables");
const headers = params.get("headers"); // yay

if (query) {
  RURU_CONFIG.query = query
}
if (variables) {
  RURU_CONFIG.variables = variables
}
if (headers) {
  RURU_CONFIG.headers = headers // yay
}
@benjie
Copy link
Member

benjie commented Sep 26, 2024

I think specifying auth tokens via query params is very risky; but that’s by-the-bye - I have no issue with someone implementing control of headers (I’m surprised we don’t already have it infact). I’m having to be ruthless with tasks pre-v5.0 so this will be a post 5.0 feature unless someone else builds it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants