Ehsan Ghanbari

Experience, DotNet, Solutions

GraphQL as an alternative for Rest

GraphQL is an open-source runtime for filling data query language introduced by Facebook. It could be an alternative to REST as it allows the client to define the structure of the data query based on the need and retrieve the same structure of the data from the server! Wow.. That's not all the story, in fact, the APIs of GraphQL get all the needed data in a single request while, traditional REST APIs sometimes require multiple URLs to fetch data. So it would be more efficient. Many programming languages support GraphQL, you can see the list of languages and frameworks here. I'm starting with C# and Dot Net. To keep it simple, create a console application and reference the GraphQL:

 

 

After installing, add a Class named Book in the project:

 

   class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

 

Now, in order to use the above class as GraphQL metadata, there is an attribute with the same name in GraphQL namespace:

 

 class Query
    {
        [GraphQLMetadata("Book")]
        public Book Retrieve()
        {
            var book = new Book { Id = 1, Name = "Asp.net Core" };
            return book;
        }
    }

 

A simple method with a custom return type(Book) which has been assigned by GraphQLMetadata attribute. Now in order to retrieve the sample data via GraphQL as JSON, create a class and use the Schema class of QraphQL which lives in GraphQL.Types namespace:

 

 static class GraphQLRequest
    {
        public static string Request()
        {
            var schema = Schema.For(@"
                  type Book {
                    id: ID
                    name: String
                  }

                  type Query {
                    book: Book
                  }
                  ", q =>
                     {
                         q.Types.Include<Query>();
                     });

            var json = schema.Execute(q =>
            {
                q.Query = "{ book { id name } }";
            });

            return json;
        }
    }

 

Now if you call the above Request method in your program class:

 

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0}", GraphQLRequest.Request());
        }
    }

 

You will see the output of QraphQL as JSON:

 



The combination of Stackify and Netling are doing a great job!

There are lots of tools and libraries out there for monitoring the Dot Net web applications. I personally use several tools based on my need. One of the tools I've always liked is Netling. It's great for fetching some overall results about the load time and resource usages of the application on heavy requests.  Rather than that, I usually use another code monitoring tool to see what's going exactly in my code. Get the latest version of Stackify and Netling. Firstly, run the Netling client and test your application load via different Threads, I'm testing the app on my website source code:

 

 

You can see the result in the picture above for 16 threads. Note that I'm running the app on my local computer machine and running on the web server would appear different result:

 

 

Anyway, you can see about the details of each parameter of Netling over here. Netling gives us overall result about the load and performance but it doesn't give us which section or which method causes the load issue. As I told, Stackify is a great tool for code monitoring; After installing and enabling the application via the mentioned link at the beginning of the post, run your application and see the result in the local address that Stackify gives you:

 

 

It's obviously shown that which action takes more time than others. And by selecting the action on the left side you can see the details on the right side. You can detect the heavy queries and modify them. needless to say that, Stackify code monitoring and you can only use it in your local machine on your source code.



Enabling the CORS in Asp.net Core

As I'm talking with Asp.net core these days, so I'm supposed to be eager about that! In asp.net core, in order to enable the CORS, you should refer to application startup and ConfigureServices and add the following configuration:

 

 public class Startup

    {

        public Startup(IConfiguration configuration)

        {

            Configuration = configuration;

        }


        public IConfiguration Configuration { get; }

 

        // This method gets called by the runtime. Use this method to add services to the container.

        public void ConfigureServices(IServiceCollection services)

        {

            services.Configure<CookiePolicyOptions>(options =>

            {

                // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                options.CheckConsentNeeded = context => true;

                options.MinimumSameSitePolicy = SameSiteMode.None;

            });


            services.AddCors(o => o.AddPolicy("MyPolicyName", builder =>

            {

                builder.AllowAnyOrigin()

                       .AllowAnyMethod()

                       .AllowAnyHeader();

            }));

 
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        }

 

 Now for applying the above policy in your controller or action, you just need to use it via attribute:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Cors;

using Microsoft.AspNetCore.Mvc;

 

namespace WebApplication3.Controllers

{

    [EnableCors(policyName: "MyPolicyName")]

    public class DefaultController : Controller

    {

        [EnableCors(policyName: "MyPolicyName")]

        public IActionResult Index()

        {

            return View();

        }

    }

}

 

And, in order to apply for every request, you can config it like below in startup class: 

 

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)

        {

            app.UseCors("MyPolicyName");

 

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            else

            {

                app.UseExceptionHandler("/Error");

                app.UseHsts();

            }

 

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseCookiePolicy();

 

            app.UseMvc();

        }

 



HttpContext in asp.net core

Accessing to HttpContext in asp.net core application is just like before, for example:

 

 public class MyController : Controller

    {

        [HttpGet]

        public ActionResult Show()

        {

            var user = HttpContext.User;

            return View();

        }

    }

 

 But HttpContext is not available everywhere in your solution! For IHttpContextAccessor. If you are using the default dependency injection of Ap.net Core then you should example to access to HttpContext in layers rather than web, you should inject a new interface of asp.net core named firstly resolve the mentioned interface in it:

 

public void ConfigureServices(IServiceCollection services)

        {

            services.Configure<CookiePolicyOptions>(options =>

            {

                // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                options.CheckConsentNeeded = context => true;

                options.MinimumSameSitePolicy = SameSiteMode.None;

            });

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        }

 

Now you can inject the interface wherever you are going to use HttpContext:

 

   public interface ISampleService

    {

    }

 

    public class SampleService : ISampleService

    {

        private readonly IHttpContextAccessor _httpContextAccessor;

 

        public SampleService(IHttpContextAccessor httpContextAccessor)

        {

            _httpContextAccessor = httpContextAccessor;

        }

    }

 

To tell the truth, I don't like this kind of using HttpContext in the outside scope of the controller. It's an antipattern in my point of view because HttpContext and every related thing to the web should live in the web project. By the way! Using HttpContext in Razor view engine is just like before!



Disable directory browsing in asp.net core

In asp.net applications, directory browsing has enabled some versions and you let the users see all of the content and structure of your application like the picture below:

To the best of my knowledge, in order to disable the directory browsing, there are two ways: by web.config and IIS. In web.config file you just need to add the following piece of code:

 

    <system.webServer>
      <directoryBrowse enabled="false" />
    </system.webServer>

 

and In IIS, you have to disable the configuration manually:

 

 

Actually, both of them are the same. But in asp.net core, as the structure has been changed, you can handle the mentioned feature by code. In web asp.net core, static files are located in a folder named wwwroot. In the configure method of Startup class you can call the UseFileServer() and set the enableDirectoryBrowsing as false:

 

      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseMvc();
            app.UseFileServer(enableDirectoryBrowsing: false);
        }



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC