Introduction
In Graphweaver, there are two fundamental concepts to grasp: data entities and GraphQL entities.
Understanding their relationship and how attributes are mapped between them is crucial for working effectively with Graphweaver.
Data Entities
Data entities represent the underlying data models or entities in your application's data source, such as databases or external APIs.
These entities typically define the structure and properties of the data stored or retrieved. In Graphweaver, data entities are responsible for interacting with the data source and performing CRUD operations.
See here for more on data entities.
GraphQL Entities
GraphQL entities, on the other hand, are representations of data entities in the GraphQL schema.
They define the structure of the data that can be queried or mutated through a GraphQL API.
GraphQL entities in Graphweaver are implemented as classes using decorators and extend the GraphQLEntity
class.
See here for more on GraphQL entities.
How are the Attributes Mapped between Entities
To connect data entities with GraphQL entities, Graphweaver provides mechanisms for attribute mapping.
One essential property of a GraphQL entity is the dataEntity
property, which holds the corresponding data entity instance.
A GraphQL entities properties are mapped by a static method on every GraphQL entity class called the fromBackendEntity
function. This is where attribute mapping takes place.
Here's how it works behind the scenes:
- The function creates a new instance of the GraphQL entity using the
dataEntity
parameter. - The function determines the GraphQL entity's fields.
- It filters the fields, ensuring that only the fields belonging to the current GraphQL entity are considered.
- For each field, the function retrieves the corresponding value from the
dataEntity
using the field name as the key. - It performs some checks to determine whether the field is eligible for attribute mapping. It ensures that the data field is defined, not a collection, not a reference, and not a function property of the entity.
- If the checks pass, it assigns the value from the data field to the corresponding field in the GraphQL entity using
entity[field.name] = dataField
. - Finally, it returns the mapped GraphQL entity.
Conclusion
In Graphweaver, data entities represent the underlying data models, while GraphQL entities define the structure in the GraphQL schema.
The fromBackendEntity
function facilitates the attribute mapping between these entities, connecting the data layer with the GraphQL layer.
Understanding the distinction between data entities and GraphQL entities and how attribute mapping works is essential for working effectively with Graphweaver.
It enables you to seamlessly translate data between the data source and the GraphQL API, providing a smooth and consistent experience for your application.