DEV Community

Cover image for C# | Building GraphQL APIs in C#
Hassan BOLAJRAF
Hassan BOLAJRAF

Posted on

C# | Building GraphQL APIs in C#

Note
You can check other posts on my personal website: https://hbolajraf.net

What is GraphQL?

GraphQL is a query language for APIs that was developed by Facebook. It allows clients to request only the data they need and nothing more. Unlike traditional REST APIs, where the server determines the structure and format of the response, GraphQL puts the power in the hands of the client to specify the shape and depth of the data.

Key Concepts

1. Schema

A GraphQL schema defines the types of data that can be queried and the relationships between them. It serves as a contract between the client and the server.

2. Types

Types represent the structure of the data in GraphQL. There are two main types:

  • Scalar Types: These are atomic types like Int, Float, String, Boolean, and ID.

  • Object Types: These are user-defined types that represent complex objects with fields.

3. Query

A GraphQL query is a request for specific data from the server. It resembles the shape of the data it expects to receive. Queries are hierarchical and match the structure of the GraphQL schema.

4. Mutation

While queries are used to read data, mutations are used to modify or create data on the server. They are similar to queries but are used for write operations.

GraphQL in C# with HotChocolate

HotChocolate is a popular GraphQL server implementation for .NET. It allows you to easily integrate GraphQL into your C# projects.

Installation

To use HotChocolate in your C# project, you can install the necessary NuGet packages:

dotnet add package HotChocolate.AspNetCore
dotnet add package HotChocolate.AspNetCore.Interceptors
Enter fullscreen mode Exit fullscreen mode

Usage

Here is a basic example of setting up a GraphQL server using HotChocolate in C#:

  1. Create a new ASP.NET Core Web API project:

    dotnet new webapi -n GraphQLExample
    
  2. Modify Startup.cs to configure GraphQL:

    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.DependencyInjection;
    using HotChocolate.AspNetCore;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGraphQLServer()
                    .AddQueryType<Query>();
        }
    
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGraphQL("/graphql");
            });
        }
    }
    
  3. Create a Query.cs file for GraphQL queries:

    using HotChocolate;
    
    public class Query
    {
        [GraphQLName("hello")]
        public string GetHello() => "Hello, GraphQL!";
    }
    

Running the Application

  1. Run your GraphQL API:

    cd GraphQLExample
    dotnet run
    

Image description

  1. Access your GraphQL API:

    Open your browser and navigate to http://localhost:5283/graphql. You can now execute GraphQL queries(Check the correct port number within launchSettings.json applicationUrl property).

Image description

This simple setup defines a GraphQL server with a single query (hello) that returns a string.

What next?

GraphQL provides a flexible and efficient way to query and manipulate data. With the HotChocolate library, integrating GraphQL into your C# projects becomes straightforward.

For more advanced features and customization options, refer to the HotChocolate Documentation.

Source Code

Top comments (0)