Skip to content

Exposing the Entity on GraphQL

The Suite Framework favors GraphQL over HTTP/Rest APIs. The framework provides several integration points to facilitate the creation of GraphQL queries and mutations.

Exposing Entity

The GraphQL Module provides a way to expose an entity trough a GraphQL query with a simple configuration on the GraphQL Module Options. In fact, the microservice template adds this configuration for us to expose the main entity of our microservice. Lets check the EquipmentsApplicationModule class.

C#
public class EquipmentsApplicationModule : SuiteAspNetApplicationModule
{
    public override void SetupModule(IModuleBuilder builder)
    {
        base.SetupModule(builder);
        builder.DependsOn<AspNetPersistenceModule>();
        builder.DependsOn<AutoMapperModule>();

        builder.DependsOn<EquipmentsPersistenceModule>();
        builder.DependsOn<EquipmentsSqlServerProviderModule>();

        builder.DependsOn<MassTransitModule>();

        builder.DependsOn<GraphQLModule, GraphQLModuleOptions>(opts =>
        {
            // TODO: Replace the name "sampleDomainEntities" with the corresponding name.
            // TODO: REMOVE THESE COMMENTS!!!
            // IMPORTANT: There is no suffix on the data transfer object, this is intentional,
            // always use the namespace to avoid confusion with the entity.
            opts.ExposeInPublicApi<Equipment, GraphQL.Equipment>("sampleDomainEntities");
        });
    }
}

The method ExposeInPublicApi allows us to expose the entity as a query on GraphQL. It takes the entity class and the DTO class as generic parameters, and the name for the query as string parameter. As mentioned in the comments left by the template, lets change the name of the query with something appropriate and lets remove the comments.

C#
public class EquipmentsApplicationModule : SuiteAspNetApplicationModule
{
    public override void SetupModule(IModuleBuilder builder)
    {
        base.SetupModule(builder);
        builder.DependsOn<AspNetPersistenceModule>();
        builder.DependsOn<AutoMapperModule>();

        builder.DependsOn<EquipmentsPersistenceModule>();
        builder.DependsOn<EquipmentsSqlServerProviderModule>();

        builder.DependsOn<MassTransitModule>();

        builder.DependsOn<GraphQLModule, GraphQLModuleOptions>(opts =>
        {
            opts.ExposeInPublicApi<Equipment, GraphQL.Equipment>("equipments");
        });
    }
}

Info

Notice that on a previous chapter, when adding the Code property to our entity we had to also add it on a DTO and an AutoMapper Profile, that was necessary because the ExposeInPublicApi uses AutoMapper to map from the entity to the DTO configured on the generic types.

Now we can run the Application project and try executing the following query on the GraphQL Playground:

GraphQL
1
2
3
4
5
6
7
8
query {
    equipments {
        nodes {
            displayName
            code
        }
    }
}

We should get the two equipments we created when doing Data Seeding: Query Result

The exposed entity includes also by default a lot of features like paging, filtering, quick search, etc. See the following: