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
In essence, there are three steps you need to perform when wrapping a REST API with GraphQL: Analyze the data model of the REST API. Derive the GraphQL schema for the API based on the data model. Implement resolver functions for the schema.
GraphQL is both a great and not-so-great choice for a public API. Check it out if enjoy this kind of content! from Google. Tim talked about powering GraphQL and REST APIs with gRPC.
GraphQL makes some tasks more complex For example, in an application that uses a few fields the same way each time, using GraphQL adds more complexity because of things like types, queries, mutators, resolvers, and higher-order components, From a maintenance perspective, this is especially detrimental.
GraphQL can retrieve data on top of or instead of the API management layer, but data can still be submitted through the existing REST APIs.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With