Simple GraphQL Tutorial In .NET Core Web API To Hit Ground Running

Run these commands in the command prompt after you get to your desired directory. This will create a solution:
 
dotnet new sln -n MOTUGraphQL
 
this will create a .NET core web project ,
 
dotnet new web -n MOTUGraphQL
 
this will add the project to the solution,
 
dotnet sln add MOTUGraphQL
 
Add these packages
 
Simple GraphQL tutorial In .NET Core Web API to hit ground running 
 
Now open the solution you just created in Visual Studio (you can get a community edition for free)
 
Check if launchSettings.json file looks like this, if it is not then it won't start up in IIS server,
  1. {  
  2.     "iisSettings": {  
  3.         "windowsAuthentication"false,  
  4.         "anonymousAuthentication"true,  
  5.         "iisExpress": {  
  6.             "applicationUrl""http://localhost:51876",  
  7.             "sslPort": 44396  
  8.         }  
  9.     },  
  10.     "profiles": {  
  11.         "IIS Express": {  
  12.             "commandName""IISExpress",  
  13.             "launchBrowser"true,  
  14.             "environmentVariables": {  
  15.                 "ASPNETCORE_ENVIRONMENT""Development"  
  16.             }  
  17.         },  
  18.         "MOTUGraphQL": {  
  19.             "commandName""Project",  
  20.             "dotnetRunMessages""true",  
  21.             "launchBrowser"true,  
  22.             "applicationUrl""https://localhost:5001;http://localhost:5000",  
  23.             "environmentVariables": {  
  24.                 "ASPNETCORE_ENVIRONMENT""Development"  
  25.             }  
  26.         }  
  27.     }  
  28. }  
Contents of appsettings.js , this contains SQL server connection string and context name (developer edition is free to use)
  1. {  
  2.     "Logging": {  
  3.         "LogLevel": {  
  4.             "Default""Information",  
  5.             "Microsoft""Warning",  
  6.             "Microsoft.Hosting.Lifetime""Information"  
  7.         }  
  8.     },  
  9.     "AllowedHosts""*",  
  10.     "ConnectionStrings": {  
  11.         "MotuContext""Data Source=(local);Database=motu_characters;User ID=sa;Password=;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"  
  12.     }  
  13. }  
Create a Faction.cs class which is the reflection of the table we are going to use.
  1. [Table("factionMain")]  
  2. public class Faction {  
  3.     [Key]  
  4.     [Column("factionId")]  
  5.     public int FactionID {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [Column("baseFaction")]  
  10.     public int BaseFaction {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     [Column("factionName")]  
  15.     public string FactionName {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     [Column("updated")]  
  20.     public DateTime Updated {  
  21.         get;  
  22.         set;  
  23.     }  
  24.     [Column("inserted")]  
  25.     public DateTime Inserted {  
  26.         get;  
  27.         set;  
  28.     }  
  29. }  
Create a MotuContext.cs inherited from DbContext to create our context
  1. public class MotuContext: DbContext {  
  2.     public MotuContext(DbContextOptions < MotuContext > options): base(options) {}  
  3.     public DbSet < Faction > Factions {  
  4.         get;  
  5.         set;  
  6.     }  
  7. }  
Create a Query.cs class, which serves as our simple query resolver
  1. public class Query {  
  2.     //public IQueryable<Faction> GetFactions([Service] MotuContext context) =>  
  3.     // context.Factions;  
  4.     public List < Faction > GetFactions(string factionName, int ? baseFaction, int ? factionID,  
  5.         [Service] MotuContext context) {  
  6.         if (factionName == null && baseFaction == null && factionID == null) {  
  7.             return context.Factions.ToList();  
  8.         }  
  9.         List < Faction > entries = new List < Faction > ();  
  10.         if (factionID != null) {  
  11.             entries.AddRange(context.Factions.Where(_ => _.FactionID == factionID).ToList());  
  12.         }  
  13.         if (baseFaction != null) {  
  14.             entries.AddRange(context.Factions.Where(_ => _.BaseFaction == baseFaction).ToList());  
  15.         }  
  16.         if (factionName != null) {  
  17.             entries.AddRange(context.Factions.Where(_ => _.FactionName.ToLower().Contains(factionName.ToLower())).ToList());  
  18.         }  
  19.         return entries;  
  20.     }  
  21. }  
Now we will go to Startup.cs where we will wire up these things
  1. public class Startup {  
  2.     public Startup(IConfiguration configuration) {  
  3.         Configuration = configuration;  
  4.     }  
  5.     public IConfiguration Configuration {  
  6.         get;  
  7.     }  
  8.     // This method gets called by the runtime. Use this method to add services to the container.  
  9.     public void ConfigureServices(IServiceCollection services) {  
  10.         services.AddControllers();  
  11.         services.AddDbContext < MotuContext > (options => options.UseSqlServer(Configuration.GetConnectionString("MotuContext")));  
  12.         services.AddRouting().AddGraphQLServer().AddQueryType < Query > ();  
  13.     }  
  14.     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  15.     public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {  
  16.         if (env.IsDevelopment()) {  
  17.             app.UseDeveloperExceptionPage();  
  18.         }  
  19.         app.UseRouting();  
  20.         app.UseEndpoints(endpoints => {  
  21.             endpoints.MapGraphQL();  
  22.             endpoints.MapControllers();  
  23.         });  
  24.     }  
  25. }  
  26. //if downloading source code does not work then run the commands in command line again and copy paste the  //code again