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

support batched queries #1383

Open
Speedy1991 opened this issue Oct 28, 2021 · 7 comments
Open

support batched queries #1383

Speedy1991 opened this issue Oct 28, 2021 · 7 comments
Assignees

Comments

@Speedy1991
Copy link
Contributor

Speedy1991 commented Oct 28, 2021

It would be awesome if strawberry would support batched queries

I already spend some time into this and this is my very first working example
The error handling is garbage but I hope you get the idea:

# strawberry/http.py
def process_result(result: ExecutionResult) -> Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]:
    if isinstance(result, list):
        data: List[GraphQLHTTPResponse] = {"data": [r.data for r in result]}
        data["errors"] = [[format_graphql_error(err) for err in (getattr(r, 'errors', []) or [])] for r in result]
        if not data["errors"]:
            del data["errors"]
        data["extensions"] = [getattr(r, 'extensions', None) for r in result]
        if not data["extensions"]:
            del data["extensions"]
    else:
        data: GraphQLHTTPResponse = {"data": result.data}
        if result.errors:
            data["errors"] = [format_graphql_error(err) for err in result.errors]
        if result.extensions:
            data["extensions"] = result.extensions

    return data

def parse_request_data(data: Union[Dict, List[Dict]]) -> Union[GraphQLRequestData, List[GraphQLRequestData]]:
    if isinstance(data, list):
        if not all([ d.get('query', None) for d in data]):
            raise MissingQueryError()
        results = [
            GraphQLRequestData(query=d["query"], variables=d.get("variables"), operation_name=d.get("operationName"))
            for d in data
        ]
        return results

    if "query" not in data:
        raise MissingQueryError()

    result = GraphQLRequestData(
        query=data["query"],
        variables=data.get("variables"),
        operation_name=data.get("operationName"),
    )

    return result
# strawberry/django.views.py
if isinstance(request_data, list):
    result = [self.schema.execute_sync(
        rd.query,
        root_value=self.get_root_value(request),
        variable_values=rd.variables,
        context_value=context,
        operation_name=rd.operation_name,
    ) for rd in request_data]

else:
    result = self.schema.execute_sync(
        request_data.query,
        root_value=self.get_root_value(request),
        variable_values=request_data.variables,
        context_value=context,
        operation_name=request_data.operation_name,
    )

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@aryaniyaps
Copy link
Contributor

@patrick91 should this be added to the roadmap? Graphene has this feature and it is simple to implement IMO.

@cheradenine
Copy link

+1 looking at integrating with a large react + Apollo app that is using batching, and they're a year in and can't change that now.

@patrick91 patrick91 self-assigned this Apr 6, 2022
@patrick91 patrick91 mentioned this issue Apr 6, 2022
3 tasks
@patrick91
Copy link
Member

@Speedy1991 @cheradenine do you still need support for batching? I was looking into updating my old PR?

There's also potential security implication to consider, since allowing for batches might make it easier to DDOS a server

@Speedy1991
Copy link
Contributor Author

actually yes

Would be an awesome feature :)

@mikoda1995
Copy link

+1 it would be great

@StummeJ
Copy link

StummeJ commented May 26, 2023

There's also potential security implication to consider, since allowing for batches might make it easier to DDOS a server

I think this is a possible issue in GQL in general. There are complexity calculations that can be preformed and reject a request when it becomes abusive. For batch, you could calculate and sum the scores

@Eraldo
Copy link

Eraldo commented Jun 14, 2024

I think this is a possible issue in GQL in general. There are complexity calculations that can be preformed and reject a request when it becomes abusive. For batch, you could calculate and sum the scores

If enabling batching is an optional setting and there is a warning in the docs, I guess might still be an option. :)

My use case:
I have have different services that all fetch some initial data.

Now on startup a get a bunch of request-response-cycles all triggered at the same time. 😅
Not ideal, but it works for now as a workaround. :)

Might be seen as a nice to have. I understand there are many important tickets at the moment.

I'm still amazed at how much progress strawberry is making. Keep up the great work! (And thanks!) 🥳 🍓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants