{"id":27170,"date":"2020-03-25T00:00:00","date_gmt":"2020-03-24T23:00:00","guid":{"rendered":"https:\/\/blexin.com\/creiamo-la-nostra-api-con-graphql-e-hot-chocolate\/"},"modified":"2021-05-20T18:34:39","modified_gmt":"2021-05-20T16:34:39","slug":"creating-our-api-with-graphql-and-hot-chocolate","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/","title":{"rendered":"Creating our API with GraphQL and Hot Chocolate"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-attachment-id=\"27129\" data-permalink=\"https:\/\/blexin.com\/en\/top00-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&amp;ssl=1\" data-orig-size=\"1024,608\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"top00-3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?resize=1024%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-27129\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">How often have you called an API and received more data than needed? Or, on the contrary, you obtained less than necessary and therefore have to make more calls to different endpoints to have all the information required?<br>Those two events are called&nbsp;<strong>overfetching<\/strong>&nbsp;and&nbsp;<strong>underfetching,&nbsp;<\/strong>and they probably represent the major problem in an API REST.<br>We need something with greater flexibility, allowing the client to be able to retrieve only the required information with a specific query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With GraphQL, we can do that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GraphQL is like a middle layer between our data and our clients, and it can be considered as an alternative to REST API or maybe even an evolution.<br>It is a query language: it means that the GraphQL queries are more flexible than the other interface architectures, which accept only very strict queries, often addressed to a single resource.<br>The Client requires exactly the fields it needs and nothing more.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can find this flexibility also in the improvement phase of the API: in fact, the addition of fields returned to our structure will not impact the existing clients.<br>GraphQL is the strongly typed: each query level match a given type, and every type describes a set of available fields. Thanks to this feature, GraphQL can validate a query before running it, and in case of error, GraphQL can also return descriptive error messages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are four key concepts in GraphQL:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Schema<\/li><li>Resolver<\/li><li>Query<\/li><li>Mutation<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A GraphQL&nbsp;<strong>schema<\/strong>&nbsp;consists of object types that define the type of objects that are possible to receive and the type of fields available.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>resolvers<\/strong>&nbsp;are the collaborators that we can associate with the fields of our scheme and which will take care of recovering data from these fields.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Considering a typical CRUD, we can define the concept of&nbsp;<strong>query<\/strong>&nbsp;and&nbsp;<strong>mutation<\/strong>.<br>The first one will deal with the information reading, while the creation, modification, and deletion are tasks managed by mutation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s try to create an application that is able to execute a CRUD with GraphQL.<br>We will do it in ASP.NET Core with Hot Chocolate, a library that allows you to create a GraphQL Server implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s create an ASP.NET Core Web application, and add the libraries HotChocolate e HotChocolate.AspNetCore with Nuget package manager. The HotChocolate.AspNetCore.Playground library has also proved very useful since it allows us to test in the browser the API we are developing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We add the necessary instructions to configure GraphQL to the Startup.cs file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace DemoGraphQL\n{\n    public class Startup\n    {\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddGraphQL(s =&gt; SchemaBuilder.New()\n                .AddServices(s)\n                .AddType&lt;AuthorType&gt;()\n                .AddType&lt;BookType&gt;()\n                .AddQueryType&lt;Query&gt;()\n                .Create());\n        }\n \n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\n        {\n            if (env.IsDevelopment())\n            {\n                app.UsePlayground();\n            }\n \n            app.UseGraphQL(&quot;\/api&quot;);\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We define&nbsp;<strong>AuthorType<\/strong>&nbsp;and&nbsp;<strong>BookType<\/strong>, which will be the types exposed by our API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In those classes, we define which fields of the domain classes&nbsp;<strong>Author<\/strong>&nbsp;and&nbsp;<strong>Book&nbsp;<\/strong>will be exposed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class AuthorType : ObjectType&lt;Author&gt;\n{\n\u00a0\u00a0\u00a0\u00a0protected override void Configure(IObjectTypeDescriptor&lt;Author&gt; descriptor)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Id).Type&lt;IdType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Name).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Surname).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0}\n}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\npublic class Author\n{\n\u00a0\u00a0\u00a0\u00a0public int Id { get; set; }\n\u00a0\u00a0\u00a0\u00a0public string Name { get; set; }\n\u00a0\u00a0\u00a0\u00a0public string Surname { get; set; }\n}\n\u00a0\npublic class BookType : ObjectType&lt;Book&gt;\n{\n\u00a0\u00a0\u00a0\u00a0protected override void Configure(IObjectTypeDescriptor&lt;Book&gt; descriptor)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Id).Type&lt;IdType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Title).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Price).Type&lt;DecimalType&gt;();\n\u00a0\u00a0\u00a0\u00a0}\n}\n\u00a0\u00a0\n\u00a0\npublic class Book\n{\n\u00a0\u00a0\u00a0\u00a0public int Id { get; set; }\n\u00a0\u00a0\u00a0\u00a0public string Title { get; set; }\n\u00a0\u00a0\u00a0\u00a0public decimal Price { get; set; }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">At this point, we can create the class Query, starting with defining the authors.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Query\n{\n\u00a0\u00a0\u00a0\u00a0private readonly IAuthorService _authorService;\n\u00a0\u00a0\u00a0\u00a0public Query(IAuthorService authorService)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_authorService = authorService;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0&#x5B;UsePaging(SchemaType = typeof(AuthorType))]\n\u00a0\u00a0\u00a0\u00a0public IQueryable&lt;Author&gt; Authors =&gt; _authorService.GetAll();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">With the annotation UsePaging, we are instructing GraphQL so that the authors returned by the service will have to be made available with pagination and in the previously defined&nbsp;<strong>AuthorType<\/strong>.<br>This way, by starting the application and going to the playground, we can make the following query and see the result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27133\" data-permalink=\"https:\/\/blexin.com\/en\/image01-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-5.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image01-5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-5.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-5.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27133\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-5.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-5-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">By adding the HotChocolate.Types and HotChocolate.Types.Filters library you can add a new annotation to enable filters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;UsePaging(SchemaType = typeof(AuthorType))]\n&#x5B;UseFiltering]\npublic IQueryable&lt;Author&gt; Authors =&gt; _authorService.GetAll();\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27139\" data-permalink=\"https:\/\/blexin.com\/en\/image02-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02-4.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image02-4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02-4.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02-4.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27139\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image02-4.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image02-4-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We get the same result with the query on books<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;UsePaging(SchemaType = typeof(BookType))]\n&#x5B;UseFiltering]\npublic IQueryable&lt;Book&gt; Books =&gt; _bookService.GetAll();\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27142\" data-permalink=\"https:\/\/blexin.com\/en\/image03-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-3.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image03-3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-3.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-3.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27142\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-3.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-3-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At the moment, books and authors are not related to each other. Still, in a real application, we want to be able to get the information of authors when we make a query about books, and, in the same way, we want to get the list of books written when we make a query about authors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s modify the Book class to add the author id:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Book\n{\n\u00a0\u00a0\u00a0\u00a0public int Id { get; set; }\n\u00a0\u00a0\u00a0\u00a0public int AuthorId { get; set; }\n\u00a0\u00a0\u00a0\u00a0public string Title { get; set; }\n\u00a0\u00a0\u00a0\u00a0public decimal Price { get; set; }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now, we can use one of the items listed before: the resolvers.&nbsp;We implement the one that allows us to get the author\u2019s information on the book query first.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class BookType : ObjectType&lt;Book&gt;\n{\n\u00a0\u00a0\u00a0\u00a0protected override void Configure(IObjectTypeDescriptor&lt;Book&gt; descriptor)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Id).Type&lt;IdType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Title).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(b =&gt; b.Price).Type&lt;DecimalType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field&lt;AuthorResolver&gt;(t =&gt; t.GetAuthor(default, default));\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We added a new field to the&nbsp;<strong>BookType<\/strong>&nbsp;schema with&nbsp;<em>descriptor.Field<\/em>&nbsp;and told him to solve it by the&nbsp;<strong>GetAuthor<\/strong>&nbsp;method of&nbsp;<strong>AuthorResolver.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class AuthorResolver\n{\n\u00a0\u00a0\u00a0\u00a0private readonly IAuthorService _authorService;\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public AuthorResolver(&#x5B;Service]IAuthorService authorService)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_authorService = authorService;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public Author GetAuthor(Book book, IResolverContext ctx)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return _authorService.GetAll().Where(a =&gt; a.Id == book.AuthorId).FirstOrDefault();\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27145\" data-permalink=\"https:\/\/blexin.com\/en\/image04-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-4.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image04-4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-4.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-4.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27145\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-4.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-4-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the same way, we can define a new field with relative resolver in order to add to the authors also the books they wrote.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class AuthorType : ObjectType&lt;Author&gt;\n{\n\u00a0\u00a0\u00a0\u00a0protected override void Configure(IObjectTypeDescriptor&lt;Author&gt; descriptor)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Id).Type&lt;IdType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Name).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field(a =&gt; a.Surname).Type&lt;StringType&gt;();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0descriptor.Field&lt;BookResolver&gt;(t =&gt; t.GetBooks(default, default));\n\u00a0\u00a0\u00a0\u00a0}\n}\n\u00a0\n\u00a0\npublic class BookResolver\n{\n\u00a0\u00a0\u00a0\u00a0private readonly IBookService _bookService;\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public BookResolver(&#x5B;Service]IBookService bookService)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_bookService = bookService;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0public IEnumerable&lt;Book&gt; GetBooks(Author author, IResolverContext ctx)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return _bookService.GetAll().Where(b =&gt; b.AuthorId == author.Id);\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27148\" data-permalink=\"https:\/\/blexin.com\/en\/image05-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-5.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image05-5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-5.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-5.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27148\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-5.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-5-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To complete the CRUD with creation and deletion operations, we need to implement what is called&nbsp;<strong>mutation<\/strong>. It is a class with methods that indicate the possible operations.<br>It must be recorded using the following statement&nbsp;<code>AddMutationTypez&lt;Mutation&gt;()<\/code>&nbsp;that must be added to the configuration block of GraphQL in Startup.cs<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nservices.AddGraphQL(s =&gt; SchemaBuilder.New()\n\u00a0\u00a0\u00a0\u00a0.AddServices(s)\n\u00a0\u00a0\u00a0\u00a0.AddType&lt;AuthorType&gt;()\n\u00a0\u00a0\u00a0\u00a0.AddType&lt;BookType&gt;()\n\u00a0\u00a0\u00a0\u00a0.AddQueryType&lt;Query&gt;()\n\u00a0\u00a0\u00a0\u00a0.AddMutationType&lt;Mutation&gt;()\n\u00a0\u00a0\u00a0\u00a0.Create());\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start by adding a method that allow us to insert a new book:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Mutation\n{\n\u00a0\u00a0\u00a0\u00a0private readonly IBookService _bookService;\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public Mutation(IBookService bookService)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_bookService = bookService;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public Book CreateBook(CreateBookInput inputBook)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return _bookService.Create(inputBook);\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">we create a class of input that we will insert in the query<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class CreateBookInput\n{\n\u00a0\u00a0\u00a0\u00a0public string Title { get; set; }\n\u00a0\u00a0\u00a0\u00a0public decimal Price { get; set; }\n\u00a0\u00a0\u00a0\u00a0public int AuthorId { get; set; }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The service will only have to create a new instance of Book, add it to its list, and return the created book.<br>From playground, we can test the new feature by launching the query you can see in the shot<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27151\" data-permalink=\"https:\/\/blexin.com\/en\/image06-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-4.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image06-4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-4.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-4.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27151\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-4.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-4-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s add now the feature of book removing.<br>As for creation, we must add a method to mutation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic Book DeleteBook(DeleteBookInput inputBook)\n{\n\u00a0\u00a0\u00a0\u00a0return _bookService.Delete(inputBook);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The class&nbsp;<strong>DeleteBookInput<\/strong>&nbsp;has only an int-type property that represents the book\u2019s Id you want to delete.<br>A possible implementation of service&nbsp;<strong>Delete<\/strong>&nbsp;method is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic Book Delete(DeleteBookInput inputBook)\n{\n\u00a0\u00a0\u00a0\u00a0var bookToDelete = _books.Single(b =&gt; b.Id == inputBook.Id);\n\u00a0\u00a0\u00a0\u00a0_books.Remove(bookToDelete);\n\u00a0\u00a0\u00a0\u00a0return bookToDelete;\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27157\" data-permalink=\"https:\/\/blexin.com\/en\/image07-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-4.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image07-4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-4.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-4.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27157\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-4.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-4-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Trying to run the query again, we will get an error message about the exception thrown by the Single method. We can make the error message more informative, so that who calls our API will not have doubts about the error.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27159\" data-permalink=\"https:\/\/blexin.com\/en\/image08-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08-3.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image08-3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08-3.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08-3.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27159\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image08-3.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image08-3-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s create a new Exception<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class BookNotFoundException : Exception\n{\n\u00a0\u00a0\u00a0\u00a0public int BookId { get; internal set; }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And let\u2019s make sure that this will be launched, if the book that we want to delete was not found.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic Book Delete(DeleteBookInput inputBook)\n{\n\u00a0\u00a0\u00a0\u00a0var bookToDelete = _books.FirstOrDefault(b =&gt; b.Id == inputBook.Id);\n\u00a0\u00a0\u00a0\u00a0if (bookToDelete == null)\n\u00a0\u00a0\u00a0\u00a0throw new BookNotFoundException() { BookId = inputBook.Id };\n\u00a0\u00a0\u00a0\u00a0_books.Remove(bookToDelete);\n\u00a0\u00a0\u00a0\u00a0return bookToDelete;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now we create a class that implements&nbsp;<strong>IErrorFilter<\/strong>&nbsp;made available by HotChocolate that intercepts&nbsp;<strong>BookNotFoundException&nbsp;<\/strong>and adds to the error the information we want return to the user.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class BookNotFoundExceptionFilter : IErrorFilter\n{\n\u00a0\u00a0\u00a0\u00a0public IError OnError(IError error)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (error.Exception is BookNotFoundException ex)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return error.WithMessage($&quot;Book with id {ex.BookId} not found&quot;);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return error;\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Finally, let\u2019s record this class in the Startup<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nservices.AddErrorFilter&lt;BookNotFoundExceptionFilter&gt;();\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"657\" data-attachment-id=\"27163\" data-permalink=\"https:\/\/blexin.com\/en\/image09-2-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image09-2.png?fit=981%2C657&amp;ssl=1\" data-orig-size=\"981,657\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image09-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image09-2.png?fit=981%2C657&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image09-2.png?resize=981%2C657&#038;ssl=1\" alt=\"\" class=\"wp-image-27163\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image09-2.png 981w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image09-2-480x321.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 981px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The API is finally completed, the source code is on my github<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<a href=\"https:\/\/github.com\/AARNOLD87\/GraphQLWithHotChocolate\" rel=\"nofollow\">https:\/\/github.com\/AARNOLD87\/GraphQLWithHotChocolate<\/a>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the project, I thought: it all looks great, why didn\u2019t I use GraphQL for all the APIs? Is there some disadvantage I haven\u2019t considered?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this demo, we have implemented the error management. In API REST, when something goes wrong, we expect a Status Code other than 200. In GraphQL, instead, the Status Code is always 200. That means that the client will have to rely exclusively on the content of the reply without being able to make preventive decisions based on the status received from the request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another aspect that in a demo obviously does not come out is the&nbsp;<strong>caching<\/strong>.<br>There\u2019s no built-in cache support, and implementing it is not as simple as the REST API.&nbsp;This aspect should not be underestimated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope I intrigued you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See you at the next article!<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>GraphQL: let\u2019s see what it is, what allows us to do and how we can create an API with ASP.NET Core and Hot Chocolate<\/p>\n","protected":false},"author":196716250,"featured_media":27129,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","inline_featured_image":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[688637524],"tags":[688637416,688637384,688637417],"class_list":["post-27170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-asp-net-core-en","tag-c-en","tag-graphql-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating our API with GraphQL and Hot Chocolate - Blexin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating our API with GraphQL and Hot Chocolate - Blexin\" \/>\n<meta property=\"og:description\" content=\"GraphQL: let\u2019s see what it is, what allows us to do and how we can create an API with ASP.NET Core and Hot Chocolate\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-24T23:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-20T16:34:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Adolfo Arnold\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Adolfo Arnold\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/\"},\"author\":{\"name\":\"Adolfo Arnold\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/0de430b61c8a48b0e9d81308817c1517\"},\"headline\":\"Creating our API with GraphQL and Hot Chocolate\",\"datePublished\":\"2020-03-24T23:00:00+00:00\",\"dateModified\":\"2021-05-20T16:34:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/\"},\"wordCount\":1164,\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/top00-3.png?fit=1024%2C608&ssl=1\",\"keywords\":[\"Asp.net core\",\"C#\",\"GraphQL\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/\",\"name\":\"Creating our API with GraphQL and Hot Chocolate - Blexin\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/top00-3.png?fit=1024%2C608&ssl=1\",\"datePublished\":\"2020-03-24T23:00:00+00:00\",\"dateModified\":\"2021-05-20T16:34:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/0de430b61c8a48b0e9d81308817c1517\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/top00-3.png?fit=1024%2C608&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/top00-3.png?fit=1024%2C608&ssl=1\",\"width\":1024,\"height\":608},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/creating-our-api-with-graphql-and-hot-chocolate\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blexin.com\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating our API with GraphQL and Hot Chocolate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blexin.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/0de430b61c8a48b0e9d81308817c1517\",\"name\":\"Adolfo Arnold\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g\",\"caption\":\"Adolfo Arnold\"},\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/author\\\/adolfo-arnoldblexin-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating our API with GraphQL and Hot Chocolate - Blexin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/","og_locale":"en_US","og_type":"article","og_title":"Creating our API with GraphQL and Hot Chocolate - Blexin","og_description":"GraphQL: let\u2019s see what it is, what allows us to do and how we can create an API with ASP.NET Core and Hot Chocolate","og_url":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/","og_site_name":"Blexin","article_published_time":"2020-03-24T23:00:00+00:00","article_modified_time":"2021-05-20T16:34:39+00:00","og_image":[{"width":1024,"height":608,"url":"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","type":"image\/png"}],"author":"Adolfo Arnold","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Adolfo Arnold","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/"},"author":{"name":"Adolfo Arnold","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/0de430b61c8a48b0e9d81308817c1517"},"headline":"Creating our API with GraphQL and Hot Chocolate","datePublished":"2020-03-24T23:00:00+00:00","dateModified":"2021-05-20T16:34:39+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/"},"wordCount":1164,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","keywords":["Asp.net core","C#","GraphQL"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/","url":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/","name":"Creating our API with GraphQL and Hot Chocolate - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","datePublished":"2020-03-24T23:00:00+00:00","dateModified":"2021-05-20T16:34:39+00:00","author":{"@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/0de430b61c8a48b0e9d81308817c1517"},"breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","width":1024,"height":608},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/creating-our-api-with-graphql-and-hot-chocolate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"Creating our API with GraphQL and Hot Chocolate"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/0de430b61c8a48b0e9d81308817c1517","name":"Adolfo Arnold","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ff2a87b54d0f130d7452164533199af05ef16dbd08b9241729946cea0eec7cca?s=96&d=identicon&r=g","caption":"Adolfo Arnold"},"url":"https:\/\/blexin.com\/en\/author\/adolfo-arnoldblexin-com\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/top00-3.png?fit=1024%2C608&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-74e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/27170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/users\/196716250"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=27170"}],"version-history":[{"count":9,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/27170\/revisions"}],"predecessor-version":[{"id":32905,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/27170\/revisions\/32905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/27129"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=27170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=27170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=27170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}