Hello GraphQL.NET using GraphType First Approach
Problem
Solution
The GraphType first approach requires us to use inheritance mechanism to integrate GraphQL in your application. In this section, we will look at how to use GraphType “ObjectGraphType” to integrate GraphQL in your .NET application at a very high level.
1. Create a new .NET Core 3.1 Console Application in Visual Studio 2019.
2. Open Package Console Manager and install the GrapghQL.NET packages by running the below commands.
Install-Package GraphQL -Version 3.0.0-preview-1490 Install-Package GraphQL.Server.Transports.AspNetCore.NewtonsoftJson -Version 3.5.0-alpha0046
3. Open the Program.cs file and then add the following namespaces.
using GraphQL; using GraphQL.Types; using GraphQL.NewtonsoftJson; using System.Threading.Tasks;
4. Change the return type of Main to Task instead of void and make it a async function as we should be making async calls inside it.
5. We will be using the Blog as an example to model our GraphQL throughout this book, so we start with the BlogPost in thie example. Create a new class called BlogPost and define two properties as shown below.
public class BlogPost { public int Id { get; set; } public string Title { get; set; } }
6. Create a BlogType which would be used by your schema and that lets the user to query with the defined fields in the type. In the constructor use the Field method to define the properties/fields for this type.The description is needed so that it is shown in the schema definition in the GraphQL playground which we will look in to it in the future sections.
public class BlogPostType : ObjectGraphType<BlogPost> { public BlogPostType() { Field(x => x.Id).Description("The Id of the Droid."); Field(x => x.Title).Description("The name of the Droid."); } }
7. Create a class BlogPostsQuery inherited by ObjectGraphType and start defining the query name and the resolve function in the constructor. The parameter “posts” defines what the client will be using and the resolve: context defines the function that returns the results.
public class BlogPostsQuery : ObjectGraphType { public BlogPostsQuery() { Field<BlogPostType>( "posts", resolve: context => new BlogPost { Id = 1, Title = "First GraphQL.NET Blog post" } ); } }
8. The Final step in defining the Schema is creating the instance of schema and assigning the previously created schema to it.
var schema = new Schema { Query = new BlogPostsQuery() };
9. Now, you could run the query by passing the query name and the fields that you need in the results.
var json = await schema.ExecuteAsync(_ => { _.Query = "{ posts { title } }"; });
In the above code snippet , we are only retrieving the field title. When this query is run from the client, you should see the following result.
using GraphQL; using GraphQL.NewtonsoftJson; using GraphQL.Types; using System; using System.Threading.Tasks; namespace _1._5.HelloWorldGraphQL { class Program { static async Task Main(string[] args) { var schema = new Schema { Query = new BlogPostsQuery() }; var json = await schema.ExecuteAsync(_ => { _.Query = "{ posts { title } }"; }); Console.WriteLine(json); } } public class BlogPost { public int Id { get; set; } public string Title { get; set; } } public class BlogPostType : ObjectGraphType<BlogPost> { public BlogPostType() { Field(x => x.Id).Description("The Id of the Droid."); Field(x => x.Title).Description("The name of the Droid."); } } public class BlogPostsQuery : ObjectGraphType { public BlogPostsQuery() { Field<BlogPostType>( "posts", resolve: context => new BlogPost { Id = 1, Title = "First GraphQL.NET Blog post" } ); } } }