DEV Community

Hasura
Hasura

Posted on • Originally published at hasura.io on

Boosting database interactivity and developer productivity with Hasura Native Queries and Logical Models

Boosting database interactivity and developer productivity with Hasura Native Queries and Logical Models

API development has seen a significant surge in demand as organizations across the globe strive to harness the power of data. The Hasura GraphQL engine automates API creation from existing database objects like tables, views, functions, and stored procedures.

For developers, this automation leads to significant time savings, freeing them from the hassles of manually developing APIs from scratch. It also brings in the benefits of GraphQL, such as type safety, real-time subscriptions, and performance enhancements.

Why Use Native Queries and Logical Models

The Hasura GraphQL Engine has gained popularity for its ability to automatically generate a GraphQL API around database objects, providing seamless querying, mutating, and subscribing to data changes. However, there are instances when you require more custom or advanced functionality that goes beyond the out-of-the-box capabilities. For example:

  • ​​Use the full power of SQL that Hasura might not provide access to through the typical table API, such as GROUP BY, window functions, or scalar functions.
  • Provide custom arguments to the users of your API to greatly expand its flexibility.
  • Encapsulate sophisticated filtering with a query, allowing your users to provide a single argument rather than having to understand how to manipulate the data.
  • Work with the advanced features of your database to improve performance.
  • Write a compatibility layer around tables, making it easier to change your API without breaking existing clients.
  • Reduce duplication by moving common data manipulation into one place.

Hasura's Native Queries feature provides a powerful tool for enhancing your GraphQL API with the flexibility and control of raw SQL queries. By leveraging Native Queries, you can create custom and advanced behavior in your Hasura-generated GraphQL schema without the need for additional database objects or DDL privileges. This enables you to unlock the full potential of SQL while building robust and efficient applications with Hasura.

How Native Queries work

The Hasura GraphQL Engine integrates seamlessly with Native Queries. Here's an overview of how it works:

Defining Native Queries

To create a Native Query, you write raw SQL statements directly within Hasura. This query like the one shown below can use arguments using the syntax {{argument_name}}. These queries can be as simple or complex as needed, incorporating SQL features such as joins, aggregations, and custom business logic.

Boosting database interactivity and developer productivity with Hasura Native Queries and Logical Models

Binding to GraphQL Schema using Logical Models

After defining a Native Query, you can bind it to your Hasura-generated GraphQL schema. This automatically exposes the Native Query as a GraphQL API endpoint, making it accessible to your application.

Boosting database interactivity and developer productivity with Hasura Native Queries and Logical Models

Executing Native Queries: When a request is made to the Native Query endpoint via GraphQL, the Hasura GraphQL Engine translates the GraphQL query into an SQL query and executes it against the database.

The results are then transformed into GraphQL response format and returned to the client.

<root field name>(
[args: {"<argument name>": <argument value>, ...},]
[where: ...,]
[order_by: ..., distinct_on: ...,]
[limit: ..., offset: ...]
) {
<field 1>
<field 2>
...
}
}```




Enter fullscreen mode Exit fullscreen mode

Developers can leverage Native Queries using the Hasura GraphQL API and all the features that come with it – this includes pagination, filtering, sorting, aggregations, caching, etc.

Benefits

This is where Native Queries come into play, empowering you to leverage the full power of SQL within Hasura while maintaining flexibility and control over your GraphQL schema. Native Queries enable you to automatically generate a GraphQL API around raw SQL queries, offering a range of benefits:

  1. Flexibility: By utilizing Native Queries, you can incorporate custom SQL logic into your GraphQL schema, allowing for complex database operations and tailored responses. This flexibility enables you to meet specific requirements that may not be achievable through standard GraphQL queries.
  2. Control: Native Queries give you greater control over your Hasura-generated GraphQL schema. Instead of relying solely on the automatic generation of GraphQL API from the database schema, you can shape the GraphQL schema around your SQL queries, providing a more fine-tuned interface for interacting with your data.
  3. Avoiding DDL Privileges: With Native Queries, you no longer need to create additional database objects that require Data Definition Language (DDL) privileges. This simplifies the development process by reducing the dependencies and permissions required for deploying and maintaining your application.
  4. Abstraction: By defining the data model in Hasura, it allows you to define a data schema that best fits the needs of your GraphQL API, decoupling it from the underlying database schema. This enables independent evolution of the API and the database structure.
  5. Cross-Database Compatibility: Modeling data in the middleware layer can facilitate cross-database compatibility. The middleware can act as an abstraction layer, translating GraphQL queries into the appropriate database-specific queries for different target databases. This allows for greater flexibility in choosing and changing the underlying database technology.

In the realm of GraphQL development, choosing the right approach for modeling data is crucial for building efficient, scalable, and flexible applications. Traditionally, developers have relied on creating database artifacts like views, user-defined functions (UDFs), and stored procedures.

However, solutions like Hasura represent a paradigm shift in the way data is leveraged by app developers and other data consumers across organizations. Native Queries and Logical Models are a game-changer, they empower developers to unlock unprecedented agility and flexibility in their GraphQL solutions.

Limitations

Native Queries currently supports read-only query capabilities for Postgres, SQL Server, and BigQuery. Support for more databases and mutations will be coming soon.

Conclusion

Modeling data in Hasura represents a paradigm shift in GraphQL development, enabling developers to embrace agility, flexibility, and efficiency. By leveraging Hasura's automatic API generation, flexibility in schema definition, rapid development capabilities, access control mechanisms, performance optimizations, and integration capabilities, developers can build scalable and resilient applications without being bound by the limitations of traditional database-centric approaches.

As the GraphQL ecosystem continues to evolve, embracing tools like Hasura will undoubtedly become the preferred choice for developers seeking to unlock the full potential of GraphQL.

πŸ“š Documentation and Resources

To help you get started, we've prepared detailed documentation, guides, and examples:

πŸš€ Get Started Today!

We can't wait to see the amazing applications you'll build using Hasura and Native Queries. Get started today by signing up for Hasura Cloud and connecting to one of the supported databases.

If you have any questions or need assistance, feel free to reach out to our team on Discord or GitHub.

Happy building! πŸŽ‰

Top comments (0)