Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice using web api with graphql

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

like image 862
AMH Avatar asked Jan 15 '17 12:01

AMH


People also ask

How do you integrate API with GraphQL?

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.

Is GraphQL good for public API?

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.

What should you not use GraphQL for?

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.

Can GraphQL TALK TO REST API?

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.


2 Answers

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.

like image 176
Michael Ingmar Staib Avatar answered Sep 28 '22 02:09

Michael Ingmar Staib


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/

like image 24
Ogglas Avatar answered Sep 28 '22 04:09

Ogglas