DEV Community

Discussion on: Entity Framework DotNet Core with GraphQL and SQL Server (using HotChocolate)

Collapse
 
toddfulton profile image
Todd-Fulton

Thanks, been trying to figure out how to hook up a database to hotchocolate for a couple days. Looks like instead of hc, it's handled by asp.net and ef core, which I know next to nothing about. Lol, was reading Hotchocolate's - Tutorial Mongo where they say:

"You are all fired up and want to get started with a little tutorial walking you through an end-to-end example with MongoDB as your database?"

...but nothing about actually hooking up to mongodb, as of yet (facepalm). From reading this my next guess would be to combine info from that tut with Create a web API with ASP.NET Core and MongoDB somehow, and then implementing the schema and resolvers and such instead of a rest api. Lots to learn.

Thanks again.

Collapse
 
michaelstaib profile image
Michael Staib

That tutorial for mongo was not supposed to be published on the documentation. It kind of is not finished and stops mid-sentence.

Here is a good example of how you can do mongo with hc:
github.com/ChilliCream/graphql-wor...

Also we have an open slack channel where we help people along:
join.slack.com/t/hotchocolategraph...

However, version 10.4 is soon out and will bring support for projections and with that makes it even easier to bind your database to hc.

Collapse
 
pascalsenn profile image
PascalSenn

Regarding projections:

Given this Query type we generate all the nested types you need. By annotating with attributes you can add additional behavior on top of it.

public class Query {
     [UsePaging]
     [UseSelection] // <--- creates the projections on the database
     [UseSorting]
     [UseFiltering]
     public IQueryable<Jedi> GetJedis([Service]DBContext ctx) 
       => ctx.Jedis.AsQueryable()

}

When you now execute this query

{
  jedis(where: {name: "Han"}, order_by:{side:DESC}) { name side }
}

This SQL statement is executed:

SELECT [p].[Name],[p].[Side]
FROM [Jedis] AS [p]
WHERE [p].[Name] ="Han"
ORDER BY [p].[Side] DESC
Thread Thread
 
toddfulton profile image
Todd-Fulton

Thank you for info and redirect to the repo.

I actually got it working with the BookAPI example on the ms site, and the startwars example repo. I basically just followed the BookAPI tutorial until they started talking about controllers, then looked at the starwars repo and implemented a resolver that used the BookService that was connected to mongo, instead of the in-memory repository, and then implemented the types for the book and query and hooked it all up in startup.

I'll go through the workshop repo and see how it compares to what I did to see if I was on the right track, and start learning about all the other stuff.

Thank you kindly!

Thread Thread
 
pascalsenn profile image
PascalSenn

Join the slack channel if you have any questions, you get usually an answer real quickly :)