GraphQL Server With .NET Framework (Not .NET Core)
Wondering whether you can set up a GraphQL server implementation using the regular ole .NET Framework? Then read on.
Join the DZone community and get the full member experience.Join For Free
in a recent post, we had listed out analyses done and conclusions made when asked by a customer to convert their existing api to graphql. while exploring graphql with .net framework, we realized there is a large gap in examples and demos on using graphql with full scale .net framework and entityframework. we took some time out to create a sample repo and put it out there so that other programmers don’t have to wonder whether its even possible.
the repo is available at https://github.com/anshulee/graphqlhobbyapi
why another graphql repo?
we used various samples and demos to build out a simple demo app showcasing the use of graphql and graphiql with .net framework. in some senses this is the simplest and most vanilla graphql server demo you will see which is what we were looking for.
- graphql server with full .net 4.5 framework
- code first entity framework 6.0
- windows machine
- visual studio
- visual studio code
this example showcases
- parameterized queries
- nested queries
- parameterized mutations
- does not use dependency injection
- https://fullstackmark.com/post/17/building-a-graphql-api-with-aspnet-core-2-and-entity-framework-core ( this is for .net core but good to get started)
- https://github.com/jacekkosciesza/starwars ( again this is for .net core)
- https://graphql-dotnet.github.io/ (graphql for .net repo)
- https://github.com/graphql-dotnet/examples/tree/master/src/aspnetwebapi (pretty much the only .net framework example we got and most useful!)
the api exposes a simple person-hobby system with the following relationship
the schema allows for the following operations:
- query person by id (allows nested queries to get person’s hobbies)
- query hobby by id (including get count of people interested in that hobby)
- create new hobby (mutation)
the nuget at https://www.nuget.org/packages/graphiql/ is made for .net core and does not work with the standard .net framework. we tried using the graphiql repo at https://github.com/graphql/graphiql directly but it had some issues building on a windows machine.( https://github.com/graphql/graphiql/issues/318 ) . we finally used the fork at https://github.com/mattferrin/graphiql to get this working on a windows machine.
steps were as under
- clone repo from https://github.com/mattferrin/graphiql
- install yarn from https://yarnpkg.com/en/docs/install#windows-stable
- open command prompt and navigate to main project
- use “yarn install” to install all dependencies
- “npm run build” on the parent directory
- navigate to example directory
- “ npm install”
- update function graphqlfetcher in example/index.html to hit the hobbyapiserver
9. “ npm start”
10. navigate to localhost:8080
what is shown here is simply how to connect the various pieces. however if someone needs specific steps, please ping us at firstname.lastname@example.org and we will write up a tutorial
if you enjoyed this article and want to learn more about graphql, check out this collection of tutorials and articles on all things graphql.
Published at DZone with permission of Anshulee Asthana, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.