How to implement Generic Queries by combining EntityFramework Core and GraphQL.NET?

Thang Chung
HackerNoon.com
9 min readJan 8, 2018

--

Source

Currently, I have struggled with the problems such as I must implement the query many times for any entity in my data model. I thought that would be great if we can expose all entities to the APIs or maybe a way to limited some of the sensitive entities, and other sides or components subsequently simply to query it (just like a generic query to avoid boilerplate code when we implemented over and over the query function for all entities in the system).

I spend a long time look deeply in OData project (the parser component actually), but it couldn’t end up with the satisfaction. Maybe because it is too complex and a lot of schemas need to define in the up-front time. And moreover, it is only used by .NET ecosystem (e.g. Microsoft Dynamic CRM, Microsoft Sharepoint…).

I really want a solution that can work, adapt and consume well to other ecosystems or components like front-end, mobile or maybe IoT devices. That is a reason I choose GraphQL by Facebook. I think I don’t need to explain more about why it is so cool at these times. For more information about it, I highly recommend you to read this article 2017: The year in GraphQL.

In this article, I will show you about my POC project that tries to combine Entity Framework Core and GraphQL.NET, then we can use it to submit gql DSL language at the front-end and query every entity in your database via the back-end.

Thank you, Joe McBride and Stef Heyenrath for your great libraries (y)

Prerequisites

Database Schema

Graph Schema Model

We define the model for columns (metadata for a table in the database) in database as following

Then, we do the same thing for the table as below

Because we have attached the prefix for every table in the database (e.g. dbo.crm_Tasks) so we need a mapping table which helps us to resolve the friendly name for the entity (e.g. tasks when we submitted the query in UI)

Now is the time to put all the schema models into the database metadata as below

We have the schema models on the codes which will be populated all schema information of EfCore entities when the application started.

Graph Type

Let define the type of the GraphQL as below

The above codes help the application to identify the data type and some of the query arguments like pagination, projection… See the final part of this article when I showed you the result of the query.

Graph Resolver

To make the GraphQL understands the database schema which is populated, we need to create some of the revolvers as following

And moreover, we define the resolver for each field in the database schema

Noticing to the dynamic LINQ in the code above (bold text colors). Thank you for System.Linq.Dynamic.Core lib, without it we need to do a lot of works to get the same things like this. Now adding one more thing which makes us can do a query by using its assembly name as

If you want to know more about the solution, deep dive more at https://stackoverflow.com/questions/48041821/dynamically-access-table-in-ef-core-2-0.

Graph Query

Now is the time we need to define the query for our application. Two things to notice as following

  • If we want to query task entity , then we would like to define as { tasks (id: “<id here>”) {id, name} }, based on the database schema that we have defined. The result will be returned by only one record in the output
  • If we want to query the list of task entities, we need to do just like { tasks_list (offset:1, first:10) { id, name } } which has a meaning to query the list of task entities from page 1 and get 10 records firstly. The result should be a list of task records in the database via the back-end

As you see above, we define 2 fields for each entity which we have in the database. For example, if you have 10 entities in DbContext, then we have 10 x 2=20 fields in the GraphQL definition. Let me show you a code

We based on the schema that we got from previous steps to define the fields for the GraphQL query. Is it make sense to you?

Graph Controller

This final step of this article, we define the controller so that we can run the application. It is really simple as below

Don’t forget to wire up all components to the IOC container as

Put it all together

Source

After finished all the steps above, the structure of this as below

This is just a POC project which I will have to do more refactor in the future. Not sure just yet. Let run it by press F5, I will show you how it works

Let input some of GraphQL query above then click Try it out!

The database schema automatically loads as

And it builds out all the GraphQL fields as

With { tasks_list(offset:1, first:10) { id, name } } for the input, you will receive

If I change it to { tasks(id: “621CFF32-A15D-4622–9938–0028EA0C3FEE”) { name, id, taskStatus } }, it should be

That enough for today :) Let me know how do you feel.

Source code

All the source code can be found at https://github.com/vietnam-devs/crmcore. If you like it give me a clap, I will actually have more motivation to it better.

Recap

This article is all about the POC that I have done for combining between Entity Framework Core and GraphQL.NET. The result that we can dynamically use some of the libraries in .NET for making the query more dynamic and flexible in the real business cases. But there are some of the caveats as following

  • Authentication and authorization for entities didn’t mention yet.
  • Maybe we can hide and don’t expose everything out of the world like this article.
  • Mutation side and other concepts of GraphQL were not mentioned just yet this article.
  • Not integrating with the front-end (react/redux with Apollo client lib).
  • Solution for child relationships with the current entity (I haven’t had a solution yet, and welcome if anyone can help on that :p)
  • You name it and let me know in the comments then I can improve it later.

Additional Readings

Thanks for reading! If you enjoyed this article, be sure to CLICK 👏 symbol below so OTHERS will SEE it.

  • 13 Feb 2019: Correct some of meanings and typos in this article

--

--

Thang Chung
HackerNoon.com

Software developer & architect @NashTech. OSS creator & contributor @GitHub.