Skip to content

Commit

Permalink
Merge pull request #95 from gingermusketeer/add-support-for-header-ed…
Browse files Browse the repository at this point in the history
…itor

Add support for the header editor
  • Loading branch information
Robert Mosolgo committed Oct 15, 2021
2 parents 90f58db + 21d4c55 commit 6b34eb1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions app/assets/javascripts/graphiql/rails/graphiql_show.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ document.addEventListener("DOMContentLoaded", function(event) {

// Defines a GraphQL fetcher using the fetch API.
var graphQLEndpoint = graphiqlContainer.dataset.graphqlEndpointPath;
function graphQLFetcher(graphQLParams) {
var providedHeaders = JSON.parse(graphiqlContainer.dataset.headers)
function graphQLFetcher(graphQLParams, options) {
return fetch(graphQLEndpoint, {
method: 'post',
headers: JSON.parse(graphiqlContainer.dataset.headers),
headers: {...providedHeaders, ...options.headers},
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(function(response) {
Expand All @@ -76,7 +77,7 @@ document.addEventListener("DOMContentLoaded", function(event) {


// Render <GraphiQL /> into the body.
var elementProps = { fetcher: graphQLFetcher, defaultQuery: defaultQuery, };
var elementProps = { fetcher: graphQLFetcher, defaultQuery: defaultQuery, headerEditorEnabled: graphiqlContainer.dataset.headerEditorEnabled === 'true' };

Object.assign(elementProps, { query: parameters.query, variables: parameters.variables })
if (queryParams === 'true') {
Expand Down
3 changes: 2 additions & 1 deletion app/views/graphiql/rails/editors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
initial_query: GraphiQL::Rails.config.initial_query,
logo: GraphiQL::Rails.config.logo,
headers: GraphiQL::Rails.config.resolve_headers(self),
query_params: GraphiQL::Rails.config.query_params
query_params: GraphiQL::Rails.config.query_params,
header_editor_enabled: GraphiQL::Rails.config.header_editor_enabled
} %>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/graphiql/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Config
# @return [Hash<String => Proc>] Keys are headers to include in GraphQL requests, values are `->(view_context) { ... }` procs to determin values
attr_accessor :headers

attr_accessor :query_params, :initial_query, :csrf, :title, :logo
attr_accessor :query_params, :initial_query, :csrf, :title, :logo, :header_editor_enabled

DEFAULT_HEADERS = {
'Content-Type' => ->(_) { 'application/json' },
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ You can override `GraphiQL::Rails.config` values in an initializer (eg, `config/
- `title` (string, default `nil`): if provided, it will be rendered in the page <title> tag
- `logo` (string, default `nil`): if provided, it will be the text logo
- `csrf` (boolean, default `true`): include `X-CSRF-Token` in GraphiQL's HTTP requests
- `header_editor_enabled` (boolean, default `false`): if provided, the header editor will be rendered
- `headers` (hash, `String => Proc`): procs to fetch header values for GraphiQL's HTTP requests, in the form `(view_context) -> { ... }`. For example:


Expand Down

0 comments on commit 6b34eb1

Please sign in to comment.