DEV Community

Cover image for Get started with GraphQL and Entity Framework

Get started with GraphQL and Entity Framework

Michael Staib on March 18, 2020

In this post I will walk you through how to build a GraphQL Server using Hot Chocolate and Entity Framework. Entity Framework is an OR-mapper from...
Collapse
 
garmendash profile image
GARMENDASH

Awesome post!, solve many of my questions when looking in HotChocolate documentation, as a newbie in the web development i get confused when i studied REST architecture, but when i see graphql, as a .NET enthusiast, i just know that this would be ideal for my database project, thanks a lot!!!

Collapse
 
garmendash profile image
GARMENDASH

I would like to contribute to HotChocolate project, not like programmer but as a translator in the documentation, i'm a spanish native speaker.

Collapse
 
michaelstaib profile image
Michael Staib

Hi,

the best way to get in touch with us is by heading to our slack channel. You can find the slack invite link on our github repo.

Collapse
 
maldworth profile image
maldworth • Edited

Great tutorial @michaelstaib . The only concern I have is your DB Entities (Domain Models) are being exposed through the graphql schema. Is there any pattern you use to hide them? Maybe you have a GraphQL Gateway? Mobile -> BFF (GraphQL with stitching) -> contoso microservice (graphQL + EFCore as shown above)?

This would mean that in addition to stitching, you would have your "view models" which map from the domain models at this BFF graphql, to not expose any database fields you don't want to?

Collapse
 
michaelstaib profile image
Michael Staib

There are actually easier ways to do that :) This example was only to show how you can couple filtering in hc with your ef context. When planning for a proper project I would not couple my schema to the DB and also think about various other approaches. We have a great slack channel ... so I encourage you to join we have lots of schema design discussions there.

Collapse
 
chandsalam1108 profile image
chandsalam1108

HI Michael, Can we use following attributes in Azure Functions?

[UsePaging]
[UseSelection]
[UseFiltering]
[UseSorting]

Please provide if you have any github sample for that.

Thank you!

Collapse
 
michaelstaib profile image
Michael Staib

There is no technical difference in the GraphQL core between Azure Functions and ASP.NET Core.

Collapse
 
michaelstaib profile image
Michael Staib

So yes :)

Collapse
 
rahmanm profile image
rahman mahmoodi

Hi Michael,
Just to let you know that selection on the model property does't work anymore. E.g. I followed this exact tutorial and this bit doesn't work anymore: enrollments(where: { courseId: 1 })

query {
students(where: { lastName: "Bar" }) {
firstMidName
lastName
enrollments(where: { courseId: 1 }) {
courseId
course {
title
}
}
}
}

I am using the latest of asp.net core and the latest of Hot Chocolate as of today:

<PackageReference Include="HotChocolate.AspNetCore" Version="10.4.3" />
<PackageReference Include="HotChocolate.AspNetCore.Playground" Version="10.4.3" />
<PackageReference Include="HotChocolate.Types.Filters" Version="10.4.3" />
<PackageReference Include="HotChocolate.Types.Selections" Version="10.4.3" />
<PackageReference Include="HotChocolate.Types.Sorting" Version="10.4.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.4" />

The playground shows error and doesn't allow to execute the enrollment where clause:

"unknown arguments on field enrollments"

TA

Collapse
 
allieberezhnaya profile image
allieberezhnaya

That could be because of the paiging. The request which is needed in case of paging enabled (nodes added) -

query {
students(where: { OR: [{ lastName: "Bar" }, { lastName: "Baz" }] }) {
nodes {
firstMidName
lastName
enrollmentDate
enrollments (where: { courseId: 1 }) {
grade
course {
title
}
}
}
}
}

Collapse
 
turbolion profile image
TurboLion

Hi Michael, thank you for the article and especially for the middleware order diagram, that helps a lot with understanding how things work.

Collapse
 
menorless profile image
Francisco Mendez

Hi Michael @michaelstaib

Look, I don't like this:

[UseFiltering]
public virtual ICollection Enrollments { get; set; }

Because I don't like to apply attributes to my models.
My question is:

There's a way that I can get this behavior but by applying it into ObjectType without losing the IQueriable feature?

Collapse
 
michaelstaib profile image
Michael Staib

You can also use our fluent API.

Collapse
 
vempatiforcode profile image
vempatiforcode • Edited

Hello Michael -
Unable to find the entities Student, Enrollment and Course in Sql server but appication working fine. Please help me to find the tables in sql server.

Collapse
 
jorgesanabria profile image
jorgesanabria

How could I implement a mutation with a call to context.saveCahngesAsync? 🤔