Mikro ORM
GQLoom's foundation for Mikro ORM is currently in an experimental stage. Some common features may be missing. Existing features are not well-tested. Please feel free to report any issues on GitHub.
MikroORM is an excellent TypeScript ORM with support for a variety of databases, such as MySQL, PostgreSQL, SQLite, and so on.
@gqloom/mikro-orm
provides integration of GQLoom with MikroORM:
- Using MikroORM's Entity Schema as silk;
- Weaving silk into MikroORM's Entity Schema;
- Generate GraphQL operations from MikroORM's Entity Schema.
Installation
You can find more information about the installation in the Mikro ORM documentation.
Using MikroORM's Entity Schema as a Silk Thread
Use the mikroSilk
method to use MikroORM's Entity Schema as a silk:
Customizing type mappings
To accommodate more database column types, we can extend GQLoom to add more type mappings.
First we use MikroWeaver.config
to configure the type mappings. Here we import the GraphQLDateTime
and GraphQLJSONObject
scalars from graphql-scalars
, and when we encounter the date
, jsonb
types, we map them to the corresponding GraphQL scalars.
Weaving Silk into MikroORM's Entity Schema
GQLoom's ability to weave silk into MikroORM's Entity Schema allows us to define Entity Schema with less code.
In the following example, we will use Valibot
Schema as the silk to define MikroORM's Entity Schema.
Creating the weaver
First, we need to create a function that weaves the Valibot
Schema into MikroORM's Entity Schema:
Defining MikroORM's Entity Schema
In the above code, we created the weaveEntitySchema
function to weave the Valibot
silk into the Entity Schema of MikroORM.
Now we will use the weaveEntitySchema
function to define the Mikro Entity Schema.
In the above code, we take the object of Valibot
as the first parameter of weaveEntitySchema
and pass the configuration of the EntitySchema in the second parameter. Here, we have defined an Author
entity with id
and name
attributes, where id
is the primary key of the entity, and in this case, we have also added an index to the name
attribute.
Defining Relationships
In MikroORM, a relationship is a way to link multiple entities together. In GQLoom, we can use weaveEntitySchema.withRelations
to define relationships for entities.
In the code above, we used weaveEntitySchema.withRelations
to define a many-to-one relationship on author
for the Book
entity, which points to the Author
entity, and the relationship is optional, and you can define more information about the configuration of the author
relationship in the second parameter of manyToOne
. configuration of the relationship.
Resolver Factory
GQLoom provides the ability to generate GraphQL operations directly from MikroORM's Entity Schema, which greatly simplifies the development process.
First, we need to create the Resolver Factory from the Entity Schema.
In the code above, we first define a Giraffe
entity and then create a resolver factory using MikroResolverFactory
. From the factory, we can generate GraphQL operations directly.
With the above simple code, we can generate GraphQL operations from Giraffe
entities and weave them into a GraphQL Schema using the weave
function.
The resulting complete GraphQL Schema looks like this: