-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Aeliot-Tm/override_rendering
Define blocks for easy overriding of the page rendering
- Loading branch information
Showing
10 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
phpunit.xml | ||
/build | ||
/vendor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Custom GraphiQL parameters | ||
========================== | ||
|
||
By default, only the `fetcher` parameter is passed to GraphiQL's React component. | ||
To add more: | ||
|
||
1. Override the default GraphiQL template: | ||
|
||
```yaml | ||
# config/packages/graphiql.yaml or app/config/config.yml for Symfony without Flex | ||
overblog_graphiql: | ||
template: "GraphiQL/index.html.twig" | ||
``` | ||
2. Create a new template: | ||
```twig | ||
{# templates/GraphiQL/index.html.twig #} | ||
{% extends '@OverblogGraphiQL/GraphiQL/index.html.twig' %} | ||
|
||
{% block graphiql_params %} | ||
{{ parent() }}, | ||
defaultQuery: `query SomeQuery($param: String) { | ||
items(param: $param) { | ||
someField | ||
} | ||
}` | ||
{% endblock graphiql_params %} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Custom rendering | ||
=============== | ||
|
||
It may be useful to change template to provide additional fields for more comfortable providing of [custom headers](custom-http-headers.md). | ||
|
||
It can be done the following way: | ||
|
||
1. Override the default GraphiQL template: | ||
|
||
```yaml | ||
# config/packages/graphiql.yaml or app/config/config.yml for Symfony without Flex | ||
overblog_graphiql: | ||
template: "GraphiQL/index.html.twig" | ||
``` | ||
2. Create a new template: | ||
You have to override block `graphiql_render` and soon of all you have to override block `graphql_fetcher_headers`. | ||
|
||
```twig | ||
{# templates/GraphiQL/index.html.twig #} | ||
{% extends '@OverblogGraphiQL/GraphiQL/index.html.twig' %} | ||
{% block graphiql_render %} | ||
ReactDOM.render( | ||
{# add your custom implementation here #} | ||
, | ||
document.body | ||
) | ||
{% endblock graphiql_render %} | ||
``` | ||
|
||
### Example | ||
|
||
See template [@OverblogGraphiQL/GraphiQL/auth.html.twig](../src/Resources/views/GraphiQL/auth.html.twig). How it looks like: | ||
|
||
![This is an image](auth-token.png) | ||
|
||
There is: | ||
|
||
1. Header used for the authorization. | ||
2. Header value (token) for the authorization. | ||
3. Button to load schema when the header value (token) typed. | ||
|
||
So, you can extend base template [@OverblogGraphiQL/GraphiQL/index.html.twig](../src/Resources/views/GraphiQL/index.html.twig) or use that. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Custom GraphQL endpoint | ||
======= | ||
|
||
If your graphql endpoint is not the default one coming with the | ||
[overblog/graphql-bundle](https://github.com/overblog/GraphQLBundle), then you might want to tell graphiql how to | ||
resolve the route depending on a schema name. | ||
|
||
By default it uses `Overblog\GraphiQLBundle\Config\GraphQLEndpoint\Helpers\OverblogGraphQLBundleEndpointResolver`. | ||
|
||
### Configuration | ||
|
||
Just set the `overblog_graphiql.endpoint_resolver` parameter like this: | ||
|
||
```yaml | ||
# in app/config/config.yml | ||
overblog_graphiql: | ||
# ... | ||
endpoint_resolver: \App\GraphiQL\EndpointResolver | ||
``` | ||
### The Resolver | ||
The resolver must be something like this: | ||
```php | ||
<?php | ||
|
||
namespace App\GraphiQL; | ||
|
||
class EndpointResolver | ||
{ | ||
public static function getByName($name) | ||
{ | ||
if ('default' === $name) { | ||
return [ | ||
'overblog_graphql_endpoint', | ||
]; | ||
} | ||
|
||
return [ | ||
'graphql_default', | ||
['schemaName' => $name], | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
It must return an array of packed params which will be passed to `RouterInterface::generate` like this | ||
`$router->generate(...$returnedValueOfTheGetByNameMethod)`. | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{% extends '@OverblogGraphiQL/GraphiQL/index.html.twig' %} | ||
|
||
{% block style %} | ||
{{ parent() }} | ||
<style> | ||
.x-header { | ||
padding: 2px 4px; | ||
} | ||
</style> | ||
{% endblock %} | ||
|
||
{% block graphql_fetcher_headers %} | ||
{{ parent() }} | ||
|
||
{% block graphql_fetcher_headers_extra %} | ||
let headerName = document.getElementById('x_header_name').value; | ||
let token = getToken() || document.token || null; | ||
if (headerName && token) { | ||
headers[headerName] = token; | ||
} | ||
{% endblock graphql_fetcher_headers_extra %} | ||
{% endblock graphql_fetcher_headers %} | ||
|
||
{% block graphiql_render %} | ||
let getToken = function () { | ||
let field = document.getElementById('x_header_value'); | ||
return field ? field.value : null; | ||
}; | ||
let renderPage = function () { | ||
ReactDOM.render( | ||
React.createElement('div', {style: {height: '100%'}}, | ||
React.createElement('div', {style: {background: '#f6f6f6', padding: '5px 15px'}}, | ||
React.createElement('select', { | ||
id: 'x_header_name', | ||
className: 'x-header', | ||
style: {height: '24px'}, | ||
title: 'Header used for the authorization', | ||
}, | ||
React.createElement('option', {value: 'Authorization'}, 'Authorization'), | ||
React.createElement('option', {value: 'X-Auth-Token'}, 'X-Auth-Token'), | ||
), | ||
React.createElement('input', { | ||
id: 'x_header_value', | ||
type: 'text', | ||
className: 'x-header', | ||
placeholder: 'Set token (usually add prefix "Bearer" for "Authorization" header)', | ||
style: {height: '16px', width: '400px'}, | ||
title: 'Header value (token) for the authorization', | ||
value: getToken() || document.token || null, | ||
}), | ||
React.createElement('button', { | ||
className: 'x-header', | ||
onClick: () => { | ||
document.token = getToken(); | ||
document.body.innerHTML = ''; | ||
document.body.outerHTML = 'Loading...'; | ||
renderPage(); | ||
}, | ||
style: {height: '24px'}, | ||
title: 'Click the button to load schema when the token specified', | ||
}, | ||
'Reload schema', | ||
), | ||
), | ||
React.createElement(GraphiQL, { | ||
fetcher: graphQLFetcher | ||
}), | ||
), | ||
document.body, | ||
); | ||
}; | ||
renderPage(); | ||
{% endblock graphiql_render %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters