ariadne icon indicating copy to clipboard operation
ariadne copied to clipboard

Set default options for GraphQLPlayground

Open kamilglod opened this issue 4 years ago • 5 comments

There was a PR to pass extra options to GraphQLPlayground but it was closed https://github.com/mirumee/ariadne/pull/315/files

Is there an option to support it? We have a problem with authentication and every single person that wants to use playground needs to remember to set

"request.credentials": "include",

on every user agent.

kamilglod avatar Jan 26 '21 13:01 kamilglod

Hi!

That PR's main pitch was disabling playground, and it was closed when other PR solved the problem in question.

I'm not against new PR happening that adds support for Playground options, but I am also unsure if this isn't the case better solved by customizing the GraphQL.get_playground method through the inheritance and using custom template instead.

rafalp avatar Jan 26 '21 15:01 rafalp

So maybe support both options? I imagine that in most cases it will be only GraphQLPlayground options changes, so duplicating whole html template just to add single option is overkill IMO.

kamilglod avatar Jan 26 '21 21:01 kamilglod

Also related: #318

rafalp avatar Mar 01 '21 22:03 rafalp

So maybe support both options? I imagine that in most cases it will be only GraphQLPlayground options changes, so duplicating whole html template just to add single option is overkill IMO.

Right now, my organization is planning on monkey-patching PLAYGROUND_HTML. Adding an argument like "playground_options" to the GraphQL class and formatting that dictionary into the HTML would be pretty slick, not a ton of code, and completely backwards compatible.

I don't know if I'll have cycles to implement that solution as a PR myself, but anyone who wants to steal the idea is welcome to it.

mildewey avatar Mar 24 '21 21:03 mildewey

Hi,

first of all, good work. Thanks!

I was facing the same issue and found a solution by deriving my own GraphQL class:

from ariadne.asgi import GraphQL
[...]
class MyGraphQL(GraphQL):
  
    def __init__(self, schema:GraphQLSchema, **kwargs):
        super(AppGraphQL, self).__init__(schema, **kwargs)

    async def render_playground(self, request: Request) -> Response:
        return FileResponse('app/static/playground.html')

I added a "playground.html" to my static content folder and modified it:

  <script>window.addEventListener('load', function (event) {
      GraphQLPlayground.init(document.getElementById('root'), {
        "settings": { ...GraphQLPlayground.ISettings, "request.credentials": "include" }
      })
    })</script>

and then used that derived MyGraphQL class to do all the normal stuff:

[...]
routes=[Mount("/", MyGraphQL(make_schema(), debug=DEBUG, introspection=INTROSPECT))]
middleware=[Middleware(CORSMiddleware, allow_origins=[ALLOWED_ORIGINS], allow_methods=("GET", "POST", "OPTIONS"))]
app = Starlette(routes=routes, middleware=middleware, debug=DEBUG, on_startup=[startup])
[...]

cons0l3 avatar Apr 15 '21 10:04 cons0l3

We are moving to GraphiQL 2 but we'll keep playground support as an opt-in, and part of this support is support for setting default options for Playground from python code:

class APIExplorerPlayground(APIExplorer):
    def __init__(
        self,
        title: str = "Ariadne GraphQL",
        editor_cursor_shape: Optional[str] = None,
        editor_font_family: Optional[str] = None,
        editor_font_size: Optional[int] = None,
        editor_reuse_headers: Optional[bool] = None,
        editor_theme: Optional[str] = None,
        general_beta_updates: Optional[bool] = None,
        prettier_print_width: Optional[int] = None,
        prettier_tab_width: Optional[int] = None,
        prettier_use_tabs: Optional[bool] = None,
        request_credentials: Optional[str] = None,
        request_global_headers: Optional[Dict[str, str]] = None,
        schema_polling_enable: Optional[bool] = None,
        schema_polling_endpoint_filter: Optional[str] = None,
        schema_polling_interval: Optional[int] = None,
        schema_disable_comments: Optional[bool] = None,
        tracing_hide_tracing_response: Optional[bool] = None,
        tracing_tracing_supported: Optional[bool] = None,
        query_plan_hide_query_plan_response: Optional[bool] = None,
    ):
        ...

rafalp avatar Oct 07 '22 17:10 rafalp

Fixed on master

rafalp avatar Nov 18 '22 15:11 rafalp