Giter Club home page Giter Club logo

graphqldoornet's Introduction

GraphQLDoorNet

When you build an api service, do you feel boring when repeatedly creating the same simple actions: get all, insert, update, delete, and each query type, you have to create a specific action? I'm really tired of that. That's why I rely on GraphQL, I try to build a library that helps to speed up that works. With this library, you can freely query from client with Linq and built-in support GetAll, paging, GetById, insert, update, delete. Of course, you can override the default action for the complex case. I don't explain detail about GraphQL, you can easy find the clearly documentation at link https://graphql.org/, https://graphql-dotnet.github.io/

Installation

This library requires .NET Core 3.x, Entity Framework Core that can integrated with any SQL database you want.

Via Package manager

Install-Package GraphQLDoorNet

Via dotnet CLI

dotnet add package GraphQLDoorNet

Initialization

  • Implement for IRepository and IUnitOfWork (If you prefer using PostgreSQL, i suggest library EFPostgresEngagement, it helps integration Entity Framework Core and PostgreSQL in some line of code)
  • Make sure you already registered IUnitOfWork in container, i recommend you should use transient, it will helpful for data consistency in asynchronous
services.AddTransient<GraphQLDoorNet.Abstracts.IUnitOfWork, GraphQLUnitOfWork>();
  • Create the Query class and let's it inherits IQuery
internal sealed class Query: IQuery
{
}
  • Create the Mutation class and let's it inherits IMutation
internal sealed class Mutation : IMutation
{
}
  • Create a controller as a door between GraphQL and database. This is only an example, you can modify anything as you want
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using GraphQLDoorNet.Abstracts;

    [Route("api/graphql")]
    [ApiController]
    public class GraphQLController : ControllerBase
    {
        private readonly IResolver graphqlResolver;

        public GraphQLController(IResolver resolver)
        {
            this.graphqlResolver = resolver;
        }

        [HttpPost]
        public async Task<IActionResult> Post()
        {
            var response = await this.graphqlResolver.Resolve(this.Request.Body);
            return new ContentResult
            {
                Content = response.Content,
                ContentType = "application/json; charset=utf-8",
                StatusCode = response.StatusCode
            };
        }
    }
  • In Startup.cs, add the following code to ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddGraphQLServices<Query, Mutation>();
  ...
}

Finally, you completed the initialization for this library. Next step, let's explore how to use it.

How to use?

Let's example, we develop a service to manage project. We have Project table and client need to CRUD on this table. To support that actions with this library, you follows the step:

  • Open the Query class, add a Query object for Project table.
    internal sealed class Query: IQuery
    {
        public EntityQueryBase<Project> Project([Inject] EntityQueryBase<Project> entityQuery) => entityQuery;
    }

The EntityQueryBase supports actions: QueryMany, GetById, QueryOne, Count. The details will be explained below. This class is freely overrided.

  • For the CUD, it is mutation behaviors, these actions need the separate models rather than the root entities. So, let's create an input model for Project entity
[InputType(EntityType = typeof(Project))]
public class ProjectInput: OptionalWrapper
{
  public string Name { get; set; }
  public string Code { get; set; }
}
  • Open the Mutation class, add an Mutation object for Project table
    internal sealed class Mutation : IMutation
    {
        public EntityMutationBase<Project, ProjectInput> Project(
            [Inject] EntityMutationBase<Project, ProjectInput> entityMutation) =>
            entityMutation;
    }
  • DONE!

Let's try to query by Postman. The endpoint should be: https://your-domain/api/graphql

Query all:

alt text

Query all with page 1 and page size = 2:

alt text

Query with condition:

alt text

You can easily replace Project with other entity, queryMany with other action - please explore more action in EntityQueryBase

Let's try CUD by Postman

Add a new Project

alt text

You can easily replace Project with other entity, add with other action - please explore more action in EntityMutationBase.

In the real world, you may need to override the default CUD or add more mutation. Just create a new mutation and inherit EntityMutationBase, then add it to the Mutation

    internal sealed class Mutation : IMutation
    {
        public ProjectMutation Project([Inject] ProjectMutation entityMutation) => entityMutation;
    }

Example, ProjectMutation has a custom action that EstablishProduct. Please don't forget to mark the Mutation attribute for the custom mutation.

[Mutation]
public class ProjectMutation : EntityMutationBase<Project, ProjectInput>
{
  public ProjectMutation(IUnitOfWork unitOfWork) : base(unitOfWork)
  {
  }

  public async Task<MutationResult> EstablishDatabase(ProjectDatabaseEstablishInput input)
  {
   ...
  }
}

alt text

You develops a front end application by javascript/typescript, you need a client library to comunicate with this library. Please refer to graphql-door-client. It supports all things to communication with this library.

Usage Libraries

graphqldoornet's People

Contributors

hieutran3010 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.