GraphQL Record Reference Class

Introduction

The Record Reference Class is a utility class designed for use in GraphQL APIs to identify and return a single database record. This class is particularly useful when you need to query a database for a specific record using various properties, such as id, idnumber, name, or any other attributes, depending on the type of record you are looking for. The class can handle different types of records and properties which can be achieved by extending the base_record_reference class.

The primary purpose of the Record Reference Class is to ensure that it retrieves exactly one record that matches the selection criteria. If multiple records match the selection criteria or none are found, the class throws an error, preventing ambiguity in your GraphQL queries.

The instance of the class behaves similarly to the entity class as it allows you to work with the database without thinking of additional things like capability checks. The instance of the class is not aware of the user that is currently logged in.

Additionally, the class has a static method load_for_viewer() that acts as a factory method for it. This method should be used to run all the needed capability checks for the current user before returning the instance of the class. This ensures that sensitive data is only accessible to authorized users, enhancing the security and integrity of your GraphQL API.

Key Features

  1. Fine-Tuned for GraphQL: The Record Reference Class is optimized for working with GraphQL input parameters. It allows you to specify the properties and values you want to use for record identification, making it easy to integrate with your GraphQL schema.

  2. Single Record Guarantee: It ensures that only one record is returned, eliminating the possibility of ambiguous results. If multiple records match the selection criteria, an error is raised, prompting clarity in your GraphQL queries.

  3. Capability Checks: The class automatically checks the required capabilities for the current user to view the returned record when it is created via load_for_viewer() factory method. This helps maintain data security and control access to sensitive information.

  4. Flexibility: While the class is designed for GraphQL, it can be adapted for various other flows where single-record retrieval is a common requirement. It offers the flexibility needed to work seamlessly with different use cases.