The author uses GraphQL with Strawberry for their VueJS frontend and Django backend to build SPAs. They encounter duplication of query logic between the GraphQL API and Django background tasks. To solve this, they execute GraphQL queries directly from Python within Django, avoiding the full request/response cycle. They generate a machine-readable schema using @graphql-codegen/cli, which is also used for frontend assets. A GraphQLSchema class is created to load and execute these queries within the Django environment. This class uses persisted documents, identified by query name, to execute queries via Strawberry. This approach allows them to reuse the same query logic defined for the frontend in their backend processes. They execute the desired GraphQL operation by name, passing in any required variables. This helps to reduce code duplication, improve code maintainability, and reduce cognitive burden. The solution ensures consistency in data querying across the frontend and backend.
wedgworth.dev
wedgworth.dev
