6

a little bit naive question but right now I want to create a web api to be more flexible I just read about gqphql , is it a good practice to accept a query string and return a string containing the result

any example using web api and graphql , I know I can secure queries with something like jws but am speaking about the idea and best practice

1

6 Answers 6

5

I am a contributor of Hot Chocolate.

In order to get GraphQL running side by side with Web API just add the following package to your project:

HotChocolate.AspNetCore

Then in the ConfigureService method of your startup add the following:

public void ConfigureServices(IServiceCollection services) { services.AddGraphQL(Schema.Create(c => { c.RegisterQueryType<Query>(); })); } 

In the configure section just add UseGraphQL

public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseGraphQL(); } 

GraphQL is then just another middleware that is side by side to your web api middleware.

If you are using .Net Framework instead of .Net Core then you have to use

HotChocolate.AspNetClassic

We have a StarWars example for .Net Core and .Net Framework here: https://github.com/ChilliCream/hotchocolate/tree/master/examples

And you can find the documentation here: https://hotchocolate.io

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

I have not tried this myself but from what I have read a lot of users have used graphql-dotnet. It also has a good getting started guide.

http://graphql-dotnet.github.io/graphql-dotnet/getting-started

https://github.com/graphql-dotnet/graphql-dotnet

It is based on Facebooks graphql

https://github.com/facebook/graphql

To learn more about graphql and what it is:

http://graphql.org/learn/

1 Comment

Here's a recent guide on using graphql-dotnet with ASP.NET Core Web API: fullstackmark.com/post/17/…
1

I think the best solution is hotchocolate, to create a functional sample use these commands:

  • dotnet new -i HotChocolate.Templates.StarWars
  • mkdir starwars
  • cd starwars
  • dotnet new starwars
  • dotnet run --project StarWars/StarWars.csproj -c release

use this address: http://127.0.0.1:5000/playground

Try a query like the following to get started:

{ human(id:1000) { name, appearsIn, friends { name, appearsIn } } }

Comments

0

I can highlight https://github.com/graphql-dotnet/graphql-dotnet as Ogglas said previously. btw, many of contributors are making efforts to develop that lib and me personally used it in real project that has been released recently. There is no problem with performance or memory licks, so, definitely, recommend.

Moreover, just finished component based on GraphQL.Net lib. It allows to configure GraphQL schemas without developing stuff in code (you can define it in json-file) and very simple and fast db-adapter from the box (can be connected to any popular DBs such as - MSSQL, MySQL, PostgreSQL or even ElasticSearch).

Comments

0

I hope this helps: Take a look at my small sample of .Net Core 2.1 GraphQL Web API using graphql-dotnet Nuget Package. That is a small example but it uses EF Core, and a node Parent-Child relations to accomplish Queries and Mutations.

Take a look: SampleGraphQLWebApi

Thanks

Comments

0

I wrote a "Database to GraphQL generator" that like Michael Ingmar Staib suggested uses HotChocolate and has all the best practices in-built! IT saves so much time for large databases and you can make mock tables that represent service calls.

Check it out here: https://github.com/MeaningOfLights/dB2GraphQL.Net

See the projects it generates and the demo databases it comes with, this is what it does:

db2GraphQL.Net enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.