GraphQL Hello World in .NET

GraphQL is the query language for the API. With an API endpoint, any data can be retrieved from the server. You can tell the API what data you want, the API will return those values. So it helps to avoid multiple requests and instead of getting all the data and selecting the one you want, you can tell the API what you want and it will send that data.

This article explains how to create the first basic GraphQL in .NET. Let’s look at it with some sample code.

First, you have to install the GraphQL package. You can install the latest version of GraphQL from NuGet. The following is a command to install the GraphQL package

PM> Install-Package GraphQL

Next, you need to install the JSON.NET serializer for GraphQL.NET. The below command installs NewtonsoftJson for the application.

PM> Install-Package GraphQL.NewtonsoftJson

The following is the .NET C # console application. First I added the necessary packages. Then the schema was initialized. The schema states what the required fields are. In this code, there is only one field, hello, and the data type string

using GraphQL;
using GraphQL.NewtonsoftJson;
using GraphQL.Types;
using System;

namespace SampleConsoleApp
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var schema = Schema.For(@" 
                                    type Query {
                                                message: String
                                               }
                                    ");

            var root = new { message = "Hello World..." };
            var json = await schema.ExecuteAsync(_ =>
            {
                _.Query = "{ message }";
                _.Root = root;
            });

             Console.WriteLine(json);
        }
    }
}

The following is an output of the above code. It is a JSON result

{
  "data": {
    "message": "Hello World..."
  }
}

Let’s look at another example with different data types. In the code below I have added some more fields with different data types. GraphQL has the default scalar that is Int, Float, String, Boolean, and ID.

using GraphQL;
using GraphQL.NewtonsoftJson;
using GraphQL.Types;
using System;

namespace SampleConsoleApp
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var schema = Schema.For(@" 
                                    type Query {
                                                movieName: String,
                                                runningTime: Int
                                                BoxOffice: Float
                                               }
                                    ");

            var root = new { MovieName = "The Lion King",
                RunningTime = 118, // in minutes
                BoxOffice = 1.657  // in billion $
            };

            var json = await schema.ExecuteAsync(_ =>
            {
                _.Query = "{ movieName, runningTime, boxOffice }";
                _.Root = root;
            });

             Console.WriteLine(json);
        }
    }
}

The following is an output of the above code

{
  "data": {
    "movieName": "The Lion King",
    "runningTime": 118,
    "boxOffice": 1.657
  }
}

I hope this helps you. Keep coding.

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Creating a C# Azure Function with Visual Studio: Step-by-Step Guide

Exploring EventCallback in Blazor: Building Interactive Components