DEV Community

How YOU can build a Web API using GraphQL .Net Core and Entity Framework

Chris Noring on August 29, 2019

 Building a Web Api using GraphQL .Net Core and Entity Framework In this article we will learn: Build a Web Api, we will learn how to...
Collapse
 
jpeg729 profile image
jpeg729

When querying list of Books or Authors, the database access is inefficient in that it will load all properties of the books/authors, and the linked entities, even if they aren't needed.

I know Entityframework can do magic with Select on queryables, and I realise that exposing the structure of the data storage to external queries is potentially problematic, so there isn't an obvious optimisation strategy.

What are your current thoughts on this problem?

Collapse
 
softchris profile image
Chris Noring

hi. There are many approaches to optimizing GraphQL. Adding pagination and limiting the depth at which you can query is a good start. Also adding a layer between the resolvers and the database is preferred. Some data almost never change and can be put in a cache and some other needs to be reread each time. As for loading linked entities, EF will only load what you ask for with include operator, so you need to be very specific what you actually want. Unless you have "lazy loading" enabled, which I don't recommend. I mean a lot of these things depends on how many records will be in each table, how will it be used etc. I wouldn't use this article's code in production unless at minimum paging is in place. I would also be very conservative how many levels I would allow a client to query for. This needs to be a living conversation with backend team and any consumers of this API.

Collapse
 
jondesam profile image
Jonghyun

Hi Chris, Thank you for great article:) I have followed it and just found your comment "I wouldn't use this article's code in production unless at minimum paging is in place."

Can you please let me know optimized structures for in production and guide me with source code examples such as Github repositories?

Thanks! :)

Collapse
 
nk54 profile image
NK54

Hello, nice article / tutorial ! I've discovered few things here.
I still face a problem : I don't have any documentation : it says "no schema available" any idea ? Maybe you can give a link to a repo where I can try your code to see if I made a typo somewhere.
You wrote "public string Id { get; set; }" in the Book class. But inserting new item throw an error because EntityFrameworkCore doesn't seem to handle string Id. I fixed that by switching to int. You may also want to add a note to seed the database in the main method before the call to CreateHostBuilder(args).Build().Run(); otherwise the in memory db will be empty.

It feels like GraphQL is awesome to build a product until all UI are done and at some point, maybe switching to pure EF queries (to avoid N+1) might be a better option if performance matters.

Have a good day and thanks again !

Collapse
 
softchris profile image
Chris Noring

hi there. Here's a repo github.com/softchris/graphql-ef-demo Let me know if that solves things for you /Chris

Collapse
 
nk54 profile image
NK54

Hey ! Thanks :)
It doesn't compile with .NET Core 3 / 3.1
Book id of type string throw an error.
The repo doesn't have right dependencies set : entityframeworkcore and entityframeworkcore.inmemory.

I've fixed those issues and I still don't see any documentation : no schema available. At least, I didn't make a typo and I can move on without getting a headache trying to find what's wrong :D

Have a good day !

Thread Thread
 
softchris profile image
Chris Noring

let me look at that and get back to you. Thanks for pointing out the above :)

Collapse
 
dolkensp profile image
Peter Dolkens

Hey Chris,

Is this the response to my comments on your previous GraphQL article (dev.to/dolkensp/comment/e380)?

Haven't had a chance to go over it properly yet, but am keen to see what you came up with if it is!

Collapse
 
softchris profile image
Chris Noring

I'm merely showing how to use EF in this article. It needs a separate article to talk about optimization

Collapse
 
hjrb profile image
hrj

Great article. I'll give a try. I don't like code first... It is always: schema first. But my personal taste.
In a not so far future we will rediscover the power of SQL :-) On a more serious note: there are actually .net / Jscript libraries available that allow a user to create and expression tree and send it.

Collapse
 
tdesaules profile image
Thibault DESAULES

Hi !

Just a question... I'm stuck on a stupid query :
I want to add something like that :

type Book {
id: ID
name: String,
genre: String,
published: Date,
Author: Author,
ref: Book
}

adding data like :
new Book
{
ID = 2,
Name = "IT",
Published = true,
AuthorId = authorDbEntry.Entity.Id,
Genre = "Mystery",
RefID = 1
},

that I should query like:
{
books {
name,
author {
name
},
ref {
name
}
}
}

Collapse
 
softchris profile image
Chris Noring

yea you would need an entry in the Query part supporting books, You would also need a specific resolver class for author and ref as they are complex objects that you mean to resolve from numbers into pure objects. Have a look here aka.ms/graphql-workshop-dotnet. Imagine books is reviews from my example

Collapse
 
tdesaules profile image
Thibault DESAULES

Thank you look exactly what I need with the "PersonResolver" i still have an id error when it try to resolved :/ need to figured it out

Collapse
 
apung profile image
Abdul Gaffur A Dama

Bookmarked, thanks anyway 🙏

Collapse
 
tsenseval profile image
tsenseval

Tx Chris, could we do subscription in dotnetcore ?

Collapse
 
softchris profile image
Chris Noring

hi yes have a look here, graphql-dotnet.github.io/docs/gett...

Collapse
 
alan5142 profile image
Alan Ramírez Herrera • Edited

Did you know hot chocolate?, in my opinion it has better support and feels simpler

Collapse
 
softchris profile image
Chris Noring

that looks very interesting. Thanks for the link Alan :)

Collapse
 
dvanherten profile image
Dave van Herten

Didn't even realize you could use the .net graphql library in this way! Thanks for the share.

Collapse
 
kamalashrafgill profile image
Kamal Ashraf Gill

Great article. Can we add some authentication to this like JWT?

Collapse
 
softchris profile image
Chris Noring

hi Kamal.. GRaphQL doesn't have a strong opinion of how to handle authorization. You can definitely add it as part of your Web Api though. Or where you looking how to do JWT generally in .Net Core? Happy to write such an article if that's the question :)

Collapse
 
kamalashrafgill profile image
Kamal Ashraf Gill

Hi Chris, thanks for the reply. Please write an article about JWT in .Net Core. It would be beneficial.

Thread Thread
 
softchris profile image
Chris Noring

will do :)

Collapse
 
aaiing profile image
AAI INGENIERIA

thanks for article, do you have a code example for login user and return only token and user data without password?

Collapse
 
bolapatil profile image
patil

Why we need graphql in dot net if we have OData???

Collapse
 
softchris profile image
Chris Noring