I divide the MarketApp article into two parts. In this article, we will develop a Web API with Asp.Net Core and respond to all requests with a single endpoint using GraphQL. In the next article we will develop a mobile application with Xamarin Forms also this application will get the data from the service will be written in this article.

By the way I used GraphQL for the first time. If there was a mistake I made, you can give me a feedback. At the end of this example, I would like to get an answer to the question whether we can use GraphQL in a future project.

Achievements

-Creating tables in the PostgreSQL database with Model Classes

-Initialize the database

-Basic CRUD operations

-Using Asp.Net Core Web API with GraphQL

1)Creating Asp.Net Core Web API

The project structure will be as follows.Our service will respond to requests for a simple shopping application.

Here are the plugins used in the project.

I will push the codes of the project to Github. You can look up  here . so I will not write some basic codes here. You can find the codes in this section in Github Repository. Firstly we create Models. Category, Product, User, Review and Order models will be enough for our application. Then write the DbContext to set the tables. Later write DbInitializer class. Lastly;

-In appSettings.Development add connection string

-ConfigureServices method in Startup.cs

Define the necessary settings for the PostgreSQL entity framework.

-Configure method in Statup.cs DbInitializer.Initialize(app);  dont forget to call DbInitializer. When you run the project If there is no missing, tables will be created in database and records should be added.

Now let’s write repositories. If you have a question that why we did this part, before you can research the DI (Dependency Injection) topic. Write the interface as follows.

I made CRUD transactions simple, but it could be more detail. It could work asynchronously, But it’s enough for this project, Now create repository classes and inherit from interface and implement the related methods. Then injection in Startup.cs will be as follows.

at this stage you can include the GraphQL package in the project

There are two basic root types in the schema of GraphQL(query and mutation). We will define fields for reading data. In mutation, we will define fields for writing data.

You can see the query fields in the second picture and the mutation fields in the third picture

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I will not write all the object types and fields here. I will write only for Product, but you can find the rest in Github.

Define object types for Product Class.

Inherited from ObjectGrapType and we defined fields corresponding to Prodcut Model fields. Then wrote the resolve function for relation with Category, Review and Order.

Query fields required for Product

We wrote two fields and specified where to get the data with the resolve functions.

Define mutation class and write the fields.

 

Define the root types in MarketAppScheme

Repeat the same process for other Models, after doing these operations, we will finally write a single endpoint.

We have defined the GraphQLQuery Class to pass the parameters in the Post method.

Startup.cs is as follows.

We completed our project. I also tested EndPoint with GraphiQL.

– http://graphql.org/learn/

https://github.com/graphql-dotnet/graphql-dotnet plugin is used in the project

https://github.com/spetz/graphql-sample

http://asp.net-hacker.rocks/2017/05/29/graphql-and-aspnetcore.html

 

Source Code