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

feat: Add Headers Editor to GraphiQL #1543

Merged
merged 29 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b6539d9
Add a header editor to the GraphiQL Interface.
connorshea May 21, 2020
d8acb7d
Move the header and variable editors into tabs.
connorshea May 22, 2020
7521185
Include header contents in prettification and history.
connorshea May 22, 2020
6a25678
Pass headers as a parameter to the fetcher.
connorshea May 22, 2020
c0e68da
Fix a linting error.
connorshea May 22, 2020
2dcd46d
Rename "Query Headers" tab to "Request Headers".
connorshea May 22, 2020
9f29e6a
Add the old variable editor CSS class again.
connorshea May 22, 2020
e7cfcab
Rename extra editor to secondary editor.
connorshea May 22, 2020
7679ddf
Bring back defaultVariableEditorOpen prop, bring back variable classes.
connorshea May 22, 2020
a4cc907
Trigger edit headers on first render.
connorshea May 23, 2020
6c98764
Toggle the display of the editors rather than removing them entirely.
connorshea May 23, 2020
40c29b5
Cursed fix for the gutter getting squashed when both editors were sid…
connorshea May 23, 2020
de470ae
Open the secondary editor when clicking on either of the editor tab n…
connorshea May 23, 2020
2e54fb9
Add headerEditorEnabled prop for enabling/disabling the header editor.
connorshea May 23, 2020
ea990e1
Remove unnecessary props from GraphiQLProps.
connorshea May 23, 2020
dd0cbca
Update GraphiQL README with new props for the header editor.
connorshea May 23, 2020
403e9d0
Add header submission to renderExample.js.
connorshea May 23, 2020
1ef963f
Enable variable editor by default if neither tab is set as active.
connorshea May 23, 2020
a01dc1a
Appease the linters.
connorshea May 23, 2020
3a67fa6
Update GraphiQL tests with mock headers and fix a bug in history.
connorshea May 23, 2020
0fe587a
Add a test for the history queue saving when changing headers.
connorshea May 23, 2020
5e5ee11
Fix a test that depended on a specific ID.
connorshea May 23, 2020
d9a5118
chore: fix linter errors
connorshea May 23, 2020
a9c9bd5
Merge branch 'master' into add-headers-editor
acao May 27, 2020
4c1c747
fix: change the second argument to opts so it can be expanded later
connorshea May 27, 2020
d771071
fix: headers needs to actually be defined
connorshea May 27, 2020
f699127
improvement: change the active/inactive tabs to grey and black
connorshea May 27, 2020
6ebabc8
fix: pass the headers as part of an object
connorshea May 27, 2020
ff8b7d9
improvement: set the enableHeaderEditor prop to false by default
connorshea May 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/graphiql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ GraphiQL supports customization in UI and behavior by accepting React props and

- `variables`: an optional GraphQL string to use as the initial displayed query variables, if `undefined` is provided, the stored variables will be used.

- `headers`: an optional GraphQL string to use as the initial displayed request headers, if `undefined` is provided, the stored headers will be used.

- `operationName`: an optional name of which GraphQL operation should be executed.

- `response`: an optional JSON string to use as the initial displayed response. If not provided, no response will be initially shown. You might provide this if illustrating the result of the initial query.
Expand All @@ -171,12 +173,16 @@ GraphiQL supports customization in UI and behavior by accepting React props and

- `defaultQuery`: an optional GraphQL string to use when no query is provided and no stored query exists from a previous session. If `undefined` is provided, GraphiQL will use its own default query.

- `defaultVariableEditorOpen`: an optional boolean that sets whether or not to show the variables pane on startup. If not defined, it will be based off whether or not variables are present.
- `defaultVariableEditorOpen`: an optional boolean that sets whether or not to show the variables pane on startup. If not defined, it will be based off whether or not variables are present. (**deprecated** in favor of `defaultSecondaryEditorOpen`)

- `defaultSecondaryEditorOpen`: an optional boolean that sets whether or not to show the variables/headers pane on startup. If not defined, it will be based off whether or not variables and/or headers are present.

- `onEditQuery`: an optional function which will be called when the Query editor changes. The argument to the function will be the query string.

- `onEditVariables`: an optional function which will be called when the Query variable editor changes. The argument to the function will be the variables string.

- `onEditHeaders`: an optional function which will be called when the request headers editor changes. The argument to the function will be the headers string.

- `onEditOperationName`: an optional function which will be called when the operation name to be executed changes.

- `onToggleDocs`: an optional function which will be called when the docs will be toggled. The argument to the function will be a boolean whether the docs are now open or closed.
Expand Down Expand Up @@ -232,6 +238,7 @@ class CustomGraphiQL extends React.Component {
// GraphQL artifacts
query: '',
variables: '',
headers: '',
response: '',

// GraphQL Schema
Expand Down
33 changes: 30 additions & 3 deletions packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ if (parameters.variables) {
}
}

// If headers was provided, try to format it.
if (parameters.headers) {
try {
parameters.headers = JSON.stringify(
JSON.parse(parameters.headers),
null,
2,
);
} catch (e) {
// Do nothing, we want to display the invalid JSON as a string, rather
// than present an error.
}
}

// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
Expand All @@ -48,6 +62,11 @@ function onEditVariables(newVariables) {
updateURL();
}

function onEditHeaders(newHeaders) {
parameters.headers = newHeaders;
updateURL();
}

function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
Expand All @@ -72,20 +91,26 @@ function updateURL() {
// Defines a GraphQL fetcher using the fetch API. You're not required to
// use fetch, and could instead implement graphQLFetcher however you like,
// as long as it returns a Promise or Observable.
function graphQLFetcher(graphQLParams) {
function graphQLFetcher(graphQLParams, headers = {}) {
connorshea marked this conversation as resolved.
Show resolved Hide resolved
// When working locally, the example expects a GraphQL server at the path /graphql.
// In a PR preview, it connects to the Star Wars API externally.
// Change this to point wherever you host your GraphQL server.
const isDev = window.location.hostname.match(/localhost$/);
const api = isDev
? '/graphql'
: 'https://swapi-graphql.netlify.app/.netlify/functions/index';

// Convert headers to an object.
if (typeof headers === 'string') {
headers = JSON.parse(headers);
}

return fetch(api, {
method: 'post',
headers: {
headers: Object.assign({
Accept: 'application/json',
'Content-Type': 'application/json',
},
}, headers),
body: JSON.stringify(graphQLParams),
credentials: 'omit',
})
Expand All @@ -110,9 +135,11 @@ ReactDOM.render(
fetcher: graphQLFetcher,
query: parameters.query,
variables: parameters.variables,
headers: parameters.headers,
operationName: parameters.operationName,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditHeaders: onEditHeaders,
defaultVariableEditorOpen: true,
onEditOperationName: onEditOperationName,
}),
Expand Down
Loading