Optimizing Strawberry GraphQL API Performance: How to bypass object Instantiation for Pre-Formatted JSON from PostgreSQL?

125 views Asked by At

I'm developing a GraphQL API using Strawberry and FastAPI, where I directly extract and shape data from a PostgreSQL database into JSON, formatted as per the GraphQL schema.

The data extraction is performed with SQL queries that utilize the selected fields as well as PostgreSQL's JSON capabilities, allowing the data to be shaped exactly as needed for the GraphQL response.

My goal now is to bypass the Python object validation in Strawberry for this pre-formatted JSON to improve performance.

In my current setup, I have various GraphQL types defined in Strawberry, resembling the following:

import strawberry

@strawberry.type
class Player:
    name: str
    age: int
    # ... more fields ...

@strawberry.type
class Team:
    city: str
    players: list[Player]

I have resolvers that are supposed to return instances of these types. However, given that the data retrieved from PostgreSQL is already structured appropriately (thanks to SQL's JSON shaping features), I am looking for a way to bypass the conversion and validation of these JSON objects into Strawberry instances.

Example resolver structure:

@strawberry.type
class Query:
    @strawberry.field
    def teams_with_player(self, info) -> list[Team]:
        formatted_json = query_postgresql_for_formatted_json(info.selected_fields)
        # The above function returns JSON directly in the structure expected by the GraphQL schema
        return formatted_json

The query_postgresql_for_formatted_json function fetches the JSON data to align with the GraphQL schema and the selected fields.

for instance, with the following query:

query {
  teamsWithPlayer {
    city
    players {
      name
    }
  }
}

the function parses the selected fields and the database returns the following data:

[
  {
    "city": "Abuja",
    "players": [
      {"name": "Player1"},
      {"name": "Player2"}
    ]
  },
  {
    "city": "Djakarta",
    "players": [
      {"name": "Player3"},
      {"name": "Player4"}
    ]
  }
  // ... more teams ...
]

how can I return this json without instanciating the Strawberry objects ?

0

There are 0 answers