Ejemplo n.º 1
0
    def dispatch_request(self):
        try:
            request_method = request.method.lower()
            data = self.parse_body()

            show_graphiql = request_method == "get" and self.should_display_graphiql()
            catch = show_graphiql

            pretty = self.pretty or show_graphiql or request.args.get("pretty")

            all_params: List[GraphQLParams]
            execution_results, all_params = run_http_query(
                self.schema,
                request_method,
                data,
                query_data=request.args,
                batch_enabled=self.batch,
                catch=catch,
                # Execute options
                root_value=self.get_root_value(),
                context_value=self.get_context_value(),
                middleware=self.get_middleware(),
            )
            result, status_code = encode_execution_results(
                execution_results,
                is_batch=isinstance(data, list),
                format_error=self.format_error,
                encode=partial(self.encode, pretty=pretty),  # noqa
            )

            if show_graphiql:
                graphiql_data = GraphiQLData(
                    result=result,
                    query=getattr(all_params[0], "query"),
                    variables=getattr(all_params[0], "variables"),
                    operation_name=getattr(all_params[0], "operation_name"),
                    subscription_url=self.subscriptions,
                    headers=self.headers,
                )
                graphiql_config = GraphiQLConfig(
                    graphiql_version=self.graphiql_version,
                    graphiql_template=self.graphiql_template,
                    graphiql_html_title=self.graphiql_html_title,
                    jinja_env=None,
                )
                source = render_graphiql_sync(
                    data=graphiql_data, config=graphiql_config
                )
                return render_template_string(source)

            return Response(result, status=status_code, content_type="application/json")

        except HttpQueryError as e:
            parsed_error = GraphQLError(e.message)
            return Response(
                self.encode(dict(errors=[self.format_error(parsed_error)])),
                status=e.status_code,
                headers=e.headers,
                content_type="application/json",
            )
Ejemplo n.º 2
0
    async def __call__(self, request):
        try:
            data = await self.parse_body(request)
            request_method = request.method.lower()
            is_graphiql = self.is_graphiql(request)
            is_pretty = self.is_pretty(request)

            # TODO: way better than if-else so better
            #  implement this too on flask and sanic
            if request_method == "options":
                return self.process_preflight(request)

            all_params: List[GraphQLParams]
            execution_results, all_params = run_http_query(
                self.schema,
                request_method,
                data,
                query_data=request.query,
                batch_enabled=self.batch,
                catch=is_graphiql,
                # Execute options
                run_sync=not self.enable_async,
                root_value=self.get_root_value(),
                context_value=self.get_context(request),
                middleware=self.get_middleware(),
            )

            exec_res = (
                [await ex for ex in execution_results]
                if self.enable_async
                else execution_results
            )
            result, status_code = encode_execution_results(
                exec_res,
                is_batch=isinstance(data, list),
                format_error=self.format_error,
                encode=partial(self.encode, pretty=is_pretty),  # noqa: ignore
            )

            if is_graphiql:
                graphiql_data = GraphiQLData(
                    result=result,
                    query=getattr(all_params[0], "query"),
                    variables=getattr(all_params[0], "variables"),
                    operation_name=getattr(all_params[0], "operation_name"),
                    subscription_url=self.subscriptions,
                    headers=self.headers,
                )
                graphiql_config = GraphiQLConfig(
                    graphiql_version=self.graphiql_version,
                    graphiql_template=self.graphiql_template,
                    graphiql_html_title=self.graphiql_html_title,
                    jinja_env=self.jinja_env,
                )
                source = await render_graphiql_async(
                    data=graphiql_data, config=graphiql_config
                )
                return web.Response(text=source, content_type="text/html")

            return web.Response(
                text=result, status=status_code, content_type="application/json",
            )

        except HttpQueryError as err:
            parsed_error = GraphQLError(err.message)
            return web.Response(
                body=self.encode(dict(errors=[self.format_error(parsed_error)])),
                status=err.status_code,
                headers=err.headers,
                content_type="application/json",
            )
Ejemplo n.º 3
0
    async def dispatch_request(self, request, *args, **kwargs):
        try:
            request_method = request.method.lower()
            data = self.parse_body(request)

            show_graphiql = request_method == "get" and self.should_display_graphiql(
                request
            )
            catch = show_graphiql

            pretty = self.pretty or show_graphiql or request.args.get("pretty")

            if request_method != "options":
                all_params: List[GraphQLParams]
                execution_results, all_params = run_http_query(
                    self.schema,
                    request_method,
                    data,
                    query_data=request.args,
                    batch_enabled=self.batch,
                    catch=catch,
                    # Execute options
                    run_sync=not self.enable_async,
                    root_value=self.get_root_value(),
                    context_value=self.get_context(request),
                    middleware=self.get_middleware(),
                )
                exec_res = (
                    [await ex for ex in execution_results]
                    if self.enable_async
                    else execution_results
                )
                result, status_code = encode_execution_results(
                    exec_res,
                    is_batch=isinstance(data, list),
                    format_error=self.format_error,
                    encode=partial(self.encode, pretty=pretty),  # noqa: ignore
                )

                if show_graphiql:
                    graphiql_data = GraphiQLData(
                        result=result,
                        query=getattr(all_params[0], "query"),
                        variables=getattr(all_params[0], "variables"),
                        operation_name=getattr(all_params[0], "operation_name"),
                        subscription_url=self.subscriptions,
                        headers=self.headers,
                    )
                    graphiql_config = GraphiQLConfig(
                        graphiql_version=self.graphiql_version,
                        graphiql_template=self.graphiql_template,
                        graphiql_html_title=self.graphiql_html_title,
                        jinja_env=self.jinja_env,
                    )
                    graphiql_options = GraphiQLOptions(
                        default_query=self.default_query,
                        header_editor_enabled=self.header_editor_enabled,
                        should_persist_headers=self.should_persist_headers,
                    )
                    source = await render_graphiql_async(
                        data=graphiql_data,
                        config=graphiql_config,
                        options=graphiql_options,
                    )
                    return html(source)

                return HTTPResponse(
                    result, status=status_code, content_type="application/json"
                )

            else:
                return self.process_preflight(request)

        except HttpQueryError as e:
            parsed_error = GraphQLError(e.message)
            return HTTPResponse(
                self.encode(dict(errors=[self.format_error(parsed_error)])),
                status=e.status_code,
                headers=e.headers,
                content_type="application/json",
            )
Ejemplo n.º 4
0
    def dispatch_request(self, request):
        try:
            request_method = request.method.lower()
            data = self.parse_body(request)
            is_graphiql = self.is_graphiql(request)
            is_pretty = self.is_pretty(request)

            all_params: List[GraphQLParams]
            execution_results, all_params = run_http_query(
                self.schema,
                request_method,
                data,
                query_data=request.query_params,
                batch_enabled=self.batch,
                catch=is_graphiql,
                # Execute options
                root_value=self.get_root_value(),
                context_value=self.get_context(request),
                middleware=self.get_middleware(),
            )
            result, status_code = encode_execution_results(
                execution_results,
                is_batch=isinstance(data, list),
                format_error=self.format_error,
                encode=partial(self.encode, pretty=is_pretty),  # noqa
            )

            if is_graphiql:
                graphiql_data = GraphiQLData(
                    result=result,
                    query=getattr(all_params[0], "query"),
                    variables=getattr(all_params[0], "variables"),
                    operation_name=getattr(all_params[0], "operation_name"),
                    subscription_url=self.subscriptions,
                    headers=self.headers,
                )
                graphiql_config = GraphiQLConfig(
                    graphiql_version=self.graphiql_version,
                    graphiql_template=self.graphiql_template,
                    graphiql_html_title=self.graphiql_html_title,
                    jinja_env=None,
                )
                graphiql_options = GraphiQLOptions(
                    default_query=self.default_query,
                    header_editor_enabled=self.header_editor_enabled,
                    should_persist_headers=self.should_persist_headers,
                )
                source = render_graphiql_sync(data=graphiql_data,
                                              config=graphiql_config,
                                              options=graphiql_options)
                return Response(source, headers={'Content-Type': 'text/html'})

            return Response(result,
                            status_code=status_code,
                            headers={'Content-Type': 'application/json'})

        except HttpQueryError as e:
            parsed_error = GraphQLError(e.message)
            headers = {}
            if e.headers is not None:
                headers.update(e.headers)
            return Response(
                self.encode(dict(errors=[self.format_error(parsed_error)])),
                status_code=e.status_code,
                headers=headers.update({'Content-Type': 'application/json'}),
            )