Simplifying GraphQL Queries for HTTP Caching

When it comes to using HTTP caching with GraphQL, we’re currently limited to servers that support persisted queries. But what if we could extend this functionality to the GraphQL single endpoint, allowing users to write queries directly in the browser’s address bar?

The Problem with GraphQL Syntax

The issue lies in the GraphQL syntax itself. The nesting behavior of fields in a GraphQL query makes it difficult to read and write in a single line. This is because the nesting can advance and retreat throughout the query, making it hard to grasp when written in a single line.

Designing an Alternative Syntax

To overcome this challenge, I’ve designed an alternative syntax called PQL (PoP Query Language). PQL ensures that the query flow is always forward, eliminating the need for retreating nested fields. This makes it possible to write queries in a single line that are easy to read and understand.

Key Features of PQL

  • Fields are written with the alias after the field name, making it a natural flow.
  • Directives are identified with a surrounding syntax <...>, allowing for nested directives.
  • Fields are separated using the | character, making it easy to compose queries.
  • Connections are defined using the . character, which always advances the query.
  • Bookmarks can be used to remove verbosity and provide shortcuts to already-traversed paths.

Making Queries Visually Appealing

PQL allows for line breaks and spaces to be added to the query, making it easier to visualize and understand. The query can be copied and pasted into the browser’s address bar, and all line breaks will be automatically removed, creating the equivalent single-line query.

Accepting Only Advancing Fields

PQL uses the , character to join elements, allowing the query to start again from the root. This ensures that the query always advances, never retreats.

Simplifying Field Arguments

PQL omits string quotes when composing the query, making it easier to write. Additionally, field arguments can be made implicit when the field has only one argument.

Variables and Fragments

PQL uses HTTP standard inputs to pass variables via $_GET or $_POST. Fragments are defined as inputs in $_GET or $_POST and are referenced with --.

Converting Queries between GraphQL and PQL

PQL is a superset of the GraphQL query syntax, making it possible to convert queries between the two. However, not every query written in PQL can be written using the GraphQL syntax due to PQL’s additional features.

Conclusion

In conclusion, PQL offers a simplified syntax for GraphQL queries, enabling users to write queries directly in the browser’s address bar while supporting HTTP caching. By understanding the challenges of the GraphQL syntax, we can design alternative solutions that make querying easier and more efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *