├── .gitattributes ├── .gitignore ├── GraphQLSampleApp.sln └── GraphQLSampleApp ├── Controllers └── WeatherForecastController.cs ├── DataAccess ├── DAO │ ├── DepartmentRepository.cs │ └── EmployeeRepository.cs ├── Entity │ ├── Department.cs │ ├── Employee.cs │ └── SampleAppDbContext.cs ├── Mutation.cs ├── Query.cs └── Subscription.cs ├── GraphQLSampleApp.csproj ├── Migrations ├── 20201211200237_InitialNigration.Designer.cs ├── 20201211200237_InitialNigration.cs ├── 20201211215343_SecondMigration.Designer.cs ├── 20201211215343_SecondMigration.cs └── SampleAppDbContextModelSnapshot.cs ├── Program.cs ├── Properties └── launchSettings.json ├── Script └── DatabaseSchemeAndData.sql ├── Startup.cs ├── WeatherForecast.cs ├── appsettings.Development.json └── appsettings.json /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /GraphQLSampleApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30717.126 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphQLSampleApp", "GraphQLSampleApp\GraphQLSampleApp.csproj", "{35011719-C5DB-4AA8-A5C5-ED92DDBFF425}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {35011719-C5DB-4AA8-A5C5-ED92DDBFF425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {35011719-C5DB-4AA8-A5C5-ED92DDBFF425}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {35011719-C5DB-4AA8-A5C5-ED92DDBFF425}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {35011719-C5DB-4AA8-A5C5-ED92DDBFF425}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {663EA2EC-9423-40C2-81BE-5295B9945163} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Controllers/WeatherForecastController.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.DAO; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.Extensions.Logging; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Threading.Tasks; 8 | 9 | namespace GraphQLSampleApp.Controllers 10 | { 11 | [ApiController] 12 | [Route("[controller]")] 13 | public class WeatherForecastController : ControllerBase 14 | { 15 | private static readonly string[] Summaries = new[] 16 | { 17 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" 18 | }; 19 | 20 | private readonly ILogger _logger; 21 | private readonly EmployeeRepository employeeRepository; 22 | 23 | public WeatherForecastController(ILogger logger, EmployeeRepository employeeRepository) 24 | { 25 | _logger = logger; 26 | this.employeeRepository = employeeRepository; 27 | } 28 | 29 | [HttpGet] 30 | public IActionResult Get() 31 | { 32 | return Ok(employeeRepository.GetEmployees()); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/DAO/DepartmentRepository.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.Entity; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace GraphQLSampleApp.DataAccess.DAO 9 | { 10 | public class DepartmentRepository 11 | { 12 | private readonly SampleAppDbContext _sampleAppDbContext; 13 | 14 | public DepartmentRepository(SampleAppDbContext sampleAppDbContext) 15 | { 16 | _sampleAppDbContext = sampleAppDbContext; 17 | } 18 | 19 | public List GetAllDepartmentOnly() 20 | { 21 | return _sampleAppDbContext.Department.ToList(); 22 | } 23 | 24 | public List GetAllDepartmentsWithEmployee() 25 | { 26 | return _sampleAppDbContext.Department 27 | .Include(d => d.Employees) 28 | .ToList(); 29 | } 30 | 31 | public async Task CreateDepartment(Department department) 32 | { 33 | await _sampleAppDbContext.Department.AddAsync(department); 34 | await _sampleAppDbContext.SaveChangesAsync(); 35 | return department; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/DAO/EmployeeRepository.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.Entity; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace GraphQLSampleApp.DataAccess.DAO 9 | { 10 | public class EmployeeRepository 11 | { 12 | private readonly SampleAppDbContext _sampleAppDbContext; 13 | 14 | public EmployeeRepository(SampleAppDbContext sampleAppDbContext) 15 | { 16 | _sampleAppDbContext = sampleAppDbContext; 17 | } 18 | 19 | public List GetEmployees() 20 | { 21 | return _sampleAppDbContext.Employee.ToList(); 22 | } 23 | 24 | public Employee GetEmployeeById(int id) 25 | { 26 | var employee = _sampleAppDbContext.Employee 27 | .Include(e => e.Department) 28 | .Where(e => e.EmployeeId == id) 29 | .FirstOrDefault(); 30 | 31 | if (employee != null) 32 | return employee; 33 | 34 | return null; 35 | } 36 | 37 | public List GetEmployeesWithDepartment() 38 | { 39 | return _sampleAppDbContext.Employee 40 | .Include(e => e.Department) 41 | .ToList(); 42 | } 43 | 44 | public async Task CreateEmployee(Employee employee) 45 | { 46 | await _sampleAppDbContext.Employee.AddAsync(employee); 47 | await _sampleAppDbContext.SaveChangesAsync(); 48 | return employee; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Entity/Department.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace GraphQLSampleApp.DataAccess.Entity 6 | { 7 | public class Department 8 | { 9 | [Key] 10 | [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int DepartmentId { get; set; } 12 | 13 | public string Name { get; set; } 14 | 15 | public ICollection Employees { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Entity/Employee.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace GraphQLSampleApp.DataAccess.Entity 6 | { 7 | public class Employee 8 | { 9 | [Key] 10 | [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int EmployeeId { get; set; } 12 | 13 | [Required] 14 | public string Name { get; set; } 15 | 16 | [Required] 17 | [EmailAddress] 18 | public string Email { get; set; } 19 | 20 | [Required] 21 | [Range(minimum: 20, maximum: 50)] 22 | public int Age { get; set; } 23 | 24 | public int DepartmentId { get; set; } 25 | public Department Department { get; set; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Entity/SampleAppDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace GraphQLSampleApp.DataAccess.Entity 8 | { 9 | public class SampleAppDbContext : DbContext 10 | { 11 | public SampleAppDbContext(DbContextOptions options) : base(options) 12 | { 13 | } 14 | 15 | public DbSet Employee { get; set; } 16 | 17 | public DbSet Department { get; set; } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Mutation.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.DAO; 2 | using GraphQLSampleApp.DataAccess.Entity; 3 | using HotChocolate; 4 | using HotChocolate.Subscriptions; 5 | using Microsoft.AspNetCore.Components; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Threading.Tasks; 10 | 11 | namespace GraphQLSampleApp.DataAccess 12 | { 13 | public class Mutation 14 | { 15 | public async Task CreateDepartment([Service]DepartmentRepository departmentRepository, 16 | [Service]ITopicEventSender eventSender, string departmentName) 17 | { 18 | var newDepartment = new Department 19 | { 20 | Name = departmentName 21 | }; 22 | var createdDepartment = await departmentRepository.CreateDepartment(newDepartment); 23 | 24 | await eventSender.SendAsync("DepartmentCreated", createdDepartment); 25 | 26 | return createdDepartment; 27 | } 28 | 29 | public async Task CreateEmployeeWithDepartmentId([Service]EmployeeRepository employeeRepository, 30 | string name, int age, string email, int departmentId) 31 | { 32 | Employee newEmployee = new Employee 33 | { 34 | Name = name, 35 | Age = age, 36 | Email = email, 37 | DepartmentId = departmentId 38 | }; 39 | 40 | var createdEmployee = await employeeRepository.CreateEmployee(newEmployee); 41 | return createdEmployee; 42 | } 43 | 44 | public async Task CreateEmployeeWithDepartment([Service] EmployeeRepository employeeRepository, 45 | string name, int age, string email, string departmentName) 46 | { 47 | Employee newEmployee = new Employee 48 | { 49 | Name = name, 50 | Age = age, 51 | Email = email, 52 | Department = new Department { Name = departmentName} 53 | }; 54 | 55 | var createdEmployee = await employeeRepository.CreateEmployee(newEmployee); 56 | return createdEmployee; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Query.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.DAO; 2 | using GraphQLSampleApp.DataAccess.Entity; 3 | using HotChocolate; 4 | using HotChocolate.Subscriptions; 5 | using System.Collections.Generic; 6 | using System.Threading.Tasks; 7 | 8 | namespace GraphQLSampleApp.DataAccess 9 | { 10 | public class Query 11 | { 12 | public List AllEmployeeOnly([Service] EmployeeRepository employeeRepository) => 13 | employeeRepository.GetEmployees(); 14 | 15 | public List AllEmployeeWithDepartment([Service] EmployeeRepository employeeRepository) => 16 | employeeRepository.GetEmployeesWithDepartment(); 17 | 18 | public async Task GetEmployeeById([Service] EmployeeRepository employeeRepository, 19 | [Service]ITopicEventSender eventSender, int id) 20 | { 21 | Employee gottenEmployee = employeeRepository.GetEmployeeById(id); 22 | await eventSender.SendAsync("ReturnedEmployee", gottenEmployee); 23 | return gottenEmployee; 24 | } 25 | 26 | public List AllDepartmentsOnly([Service] DepartmentRepository departmentRepository) => 27 | departmentRepository.GetAllDepartmentOnly(); 28 | 29 | public List AllDepartmentsWithEmployee([Service] DepartmentRepository departmentRepository) => 30 | departmentRepository.GetAllDepartmentsWithEmployee(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /GraphQLSampleApp/DataAccess/Subscription.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess.Entity; 2 | using HotChocolate; 3 | using HotChocolate.Execution; 4 | using HotChocolate.Subscriptions; 5 | using HotChocolate.Types; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | namespace GraphQLSampleApp.DataAccess 13 | { 14 | public class Subscription 15 | { 16 | [SubscribeAndResolve] 17 | public async ValueTask> OnDepartmentCreate([Service] ITopicEventReceiver eventReceiver, 18 | CancellationToken cancellationToken) 19 | { 20 | return await eventReceiver.SubscribeAsync("DepartmentCreated", cancellationToken); 21 | } 22 | 23 | 24 | [SubscribeAndResolve] 25 | public async ValueTask> OnEmployeeGet([Service] ITopicEventReceiver eventReceiver, 26 | CancellationToken cancellationToken) 27 | { 28 | return await eventReceiver.SubscribeAsync("ReturnedEmployee", cancellationToken); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GraphQLSampleApp/GraphQLSampleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | all 13 | runtime; build; native; contentfiles; analyzers; buildtransitive 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Migrations/20201211200237_InitialNigration.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using GraphQLSampleApp.DataAccess.Entity; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace GraphQLSampleApp.Migrations 10 | { 11 | [DbContext(typeof(SampleAppDbContext))] 12 | [Migration("20201211200237_InitialNigration")] 13 | partial class InitialNigration 14 | { 15 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 16 | { 17 | #pragma warning disable 612, 618 18 | modelBuilder 19 | .UseIdentityColumns() 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.1"); 22 | 23 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Employee", b => 24 | { 25 | b.Property("EmployeeId") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("int") 28 | .UseIdentityColumn(); 29 | 30 | b.Property("Age") 31 | .HasColumnType("int"); 32 | 33 | b.Property("Email") 34 | .IsRequired() 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Name") 38 | .IsRequired() 39 | .HasColumnType("nvarchar(max)"); 40 | 41 | b.HasKey("EmployeeId"); 42 | 43 | b.ToTable("Employee"); 44 | }); 45 | #pragma warning restore 612, 618 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Migrations/20201211200237_InitialNigration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace GraphQLSampleApp.Migrations 4 | { 5 | public partial class InitialNigration : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.CreateTable( 10 | name: "Employee", 11 | columns: table => new 12 | { 13 | EmployeeId = table.Column(type: "int", nullable: false) 14 | .Annotation("SqlServer:Identity", "1, 1"), 15 | Name = table.Column(type: "nvarchar(max)", nullable: false), 16 | Email = table.Column(type: "nvarchar(max)", nullable: false), 17 | Age = table.Column(type: "int", nullable: false) 18 | }, 19 | constraints: table => 20 | { 21 | table.PrimaryKey("PK_Employee", x => x.EmployeeId); 22 | }); 23 | } 24 | 25 | protected override void Down(MigrationBuilder migrationBuilder) 26 | { 27 | migrationBuilder.DropTable( 28 | name: "Employee"); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Migrations/20201211215343_SecondMigration.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using GraphQLSampleApp.DataAccess.Entity; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace GraphQLSampleApp.Migrations 10 | { 11 | [DbContext(typeof(SampleAppDbContext))] 12 | [Migration("20201211215343_SecondMigration")] 13 | partial class SecondMigration 14 | { 15 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 16 | { 17 | #pragma warning disable 612, 618 18 | modelBuilder 19 | .UseIdentityColumns() 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.1"); 22 | 23 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Department", b => 24 | { 25 | b.Property("DepartmentId") 26 | .ValueGeneratedOnAdd() 27 | .HasColumnType("int") 28 | .UseIdentityColumn(); 29 | 30 | b.Property("Name") 31 | .HasColumnType("nvarchar(max)"); 32 | 33 | b.HasKey("DepartmentId"); 34 | 35 | b.ToTable("Department"); 36 | }); 37 | 38 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Employee", b => 39 | { 40 | b.Property("EmployeeId") 41 | .ValueGeneratedOnAdd() 42 | .HasColumnType("int") 43 | .UseIdentityColumn(); 44 | 45 | b.Property("Age") 46 | .HasColumnType("int"); 47 | 48 | b.Property("DepartmentId") 49 | .HasColumnType("int"); 50 | 51 | b.Property("Email") 52 | .IsRequired() 53 | .HasColumnType("nvarchar(max)"); 54 | 55 | b.Property("Name") 56 | .IsRequired() 57 | .HasColumnType("nvarchar(max)"); 58 | 59 | b.HasKey("EmployeeId"); 60 | 61 | b.HasIndex("DepartmentId"); 62 | 63 | b.ToTable("Employee"); 64 | }); 65 | 66 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Employee", b => 67 | { 68 | b.HasOne("GraphQLSampleApp.DataAccess.Entity.Department", "Department") 69 | .WithMany("Employees") 70 | .HasForeignKey("DepartmentId") 71 | .OnDelete(DeleteBehavior.Cascade) 72 | .IsRequired(); 73 | 74 | b.Navigation("Department"); 75 | }); 76 | 77 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Department", b => 78 | { 79 | b.Navigation("Employees"); 80 | }); 81 | #pragma warning restore 612, 618 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Migrations/20201211215343_SecondMigration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace GraphQLSampleApp.Migrations 4 | { 5 | public partial class SecondMigration : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.AddColumn( 10 | name: "DepartmentId", 11 | table: "Employee", 12 | type: "int", 13 | nullable: false, 14 | defaultValue: 0); 15 | 16 | migrationBuilder.CreateTable( 17 | name: "Department", 18 | columns: table => new 19 | { 20 | DepartmentId = table.Column(type: "int", nullable: false) 21 | .Annotation("SqlServer:Identity", "1, 1"), 22 | Name = table.Column(type: "nvarchar(max)", nullable: true) 23 | }, 24 | constraints: table => 25 | { 26 | table.PrimaryKey("PK_Department", x => x.DepartmentId); 27 | }); 28 | 29 | migrationBuilder.CreateIndex( 30 | name: "IX_Employee_DepartmentId", 31 | table: "Employee", 32 | column: "DepartmentId"); 33 | 34 | migrationBuilder.AddForeignKey( 35 | name: "FK_Employee_Department_DepartmentId", 36 | table: "Employee", 37 | column: "DepartmentId", 38 | principalTable: "Department", 39 | principalColumn: "DepartmentId", 40 | onDelete: ReferentialAction.Cascade); 41 | } 42 | 43 | protected override void Down(MigrationBuilder migrationBuilder) 44 | { 45 | migrationBuilder.DropForeignKey( 46 | name: "FK_Employee_Department_DepartmentId", 47 | table: "Employee"); 48 | 49 | migrationBuilder.DropTable( 50 | name: "Department"); 51 | 52 | migrationBuilder.DropIndex( 53 | name: "IX_Employee_DepartmentId", 54 | table: "Employee"); 55 | 56 | migrationBuilder.DropColumn( 57 | name: "DepartmentId", 58 | table: "Employee"); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Migrations/SampleAppDbContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using GraphQLSampleApp.DataAccess.Entity; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | 8 | namespace GraphQLSampleApp.Migrations 9 | { 10 | [DbContext(typeof(SampleAppDbContext))] 11 | partial class SampleAppDbContextModelSnapshot : ModelSnapshot 12 | { 13 | protected override void BuildModel(ModelBuilder modelBuilder) 14 | { 15 | #pragma warning disable 612, 618 16 | modelBuilder 17 | .UseIdentityColumns() 18 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 19 | .HasAnnotation("ProductVersion", "5.0.1"); 20 | 21 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Department", b => 22 | { 23 | b.Property("DepartmentId") 24 | .ValueGeneratedOnAdd() 25 | .HasColumnType("int") 26 | .UseIdentityColumn(); 27 | 28 | b.Property("Name") 29 | .HasColumnType("nvarchar(max)"); 30 | 31 | b.HasKey("DepartmentId"); 32 | 33 | b.ToTable("Department"); 34 | }); 35 | 36 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Employee", b => 37 | { 38 | b.Property("EmployeeId") 39 | .ValueGeneratedOnAdd() 40 | .HasColumnType("int") 41 | .UseIdentityColumn(); 42 | 43 | b.Property("Age") 44 | .HasColumnType("int"); 45 | 46 | b.Property("DepartmentId") 47 | .HasColumnType("int"); 48 | 49 | b.Property("Email") 50 | .IsRequired() 51 | .HasColumnType("nvarchar(max)"); 52 | 53 | b.Property("Name") 54 | .IsRequired() 55 | .HasColumnType("nvarchar(max)"); 56 | 57 | b.HasKey("EmployeeId"); 58 | 59 | b.HasIndex("DepartmentId"); 60 | 61 | b.ToTable("Employee"); 62 | }); 63 | 64 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Employee", b => 65 | { 66 | b.HasOne("GraphQLSampleApp.DataAccess.Entity.Department", "Department") 67 | .WithMany("Employees") 68 | .HasForeignKey("DepartmentId") 69 | .OnDelete(DeleteBehavior.Cascade) 70 | .IsRequired(); 71 | 72 | b.Navigation("Department"); 73 | }); 74 | 75 | modelBuilder.Entity("GraphQLSampleApp.DataAccess.Entity.Department", b => 76 | { 77 | b.Navigation("Employees"); 78 | }); 79 | #pragma warning restore 612, 618 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.Extensions.Configuration; 3 | using Microsoft.Extensions.Hosting; 4 | using Microsoft.Extensions.Logging; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace GraphQLSampleApp 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:55106", 8 | "sslPort": 44351 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "weatherforecast", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "GraphQLSampleApp": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "launchUrl": "weatherforecast", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GraphQLSampleApp/Script/DatabaseSchemeAndData.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CloudBloq/GraphQLSampleApp/5d70387a80d95f64c7429673e9db743c986db736/GraphQLSampleApp/Script/DatabaseSchemeAndData.sql -------------------------------------------------------------------------------- /GraphQLSampleApp/Startup.cs: -------------------------------------------------------------------------------- 1 | using GraphQLSampleApp.DataAccess; 2 | using GraphQLSampleApp.DataAccess.DAO; 3 | using GraphQLSampleApp.DataAccess.Entity; 4 | using HotChocolate; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.HttpsPolicy; 8 | using Microsoft.AspNetCore.Mvc; 9 | using Microsoft.EntityFrameworkCore; 10 | using Microsoft.Extensions.Configuration; 11 | using Microsoft.Extensions.DependencyInjection; 12 | using Microsoft.Extensions.Hosting; 13 | using Microsoft.Extensions.Logging; 14 | using System; 15 | using System.Collections.Generic; 16 | using System.Linq; 17 | using System.Threading.Tasks; 18 | 19 | namespace GraphQLSampleApp 20 | { 21 | public class Startup 22 | { 23 | private readonly string AllowedOrigin = "allowedOrigin"; 24 | 25 | public Startup(IConfiguration configuration) 26 | { 27 | Configuration = configuration; 28 | } 29 | 30 | public IConfiguration Configuration { get; } 31 | 32 | // This method gets called by the runtime. Use this method to add services to the container. 33 | public void ConfigureServices(IServiceCollection services) 34 | { 35 | services.AddDbContext(options => 36 | options.UseSqlServer(Configuration.GetConnectionString("SampleAppDbContext"))); 37 | 38 | services.AddInMemorySubscriptions(); 39 | 40 | services 41 | .AddGraphQLServer() 42 | .AddQueryType() 43 | .AddMutationType() 44 | .AddSubscriptionType(); 45 | 46 | services.AddScoped(); 47 | services.AddScoped(); 48 | 49 | services.AddCors(option => { 50 | option.AddPolicy("allowedOrigin", 51 | builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader() 52 | ); 53 | }); 54 | } 55 | 56 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 57 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 58 | { 59 | if (env.IsDevelopment()) 60 | { 61 | app.UseDeveloperExceptionPage(); 62 | } 63 | app.UseCors(AllowedOrigin); 64 | app.UseWebSockets(); 65 | app 66 | .UseRouting() 67 | .UseEndpoints(endpoints => 68 | { 69 | endpoints.MapGraphQL(); 70 | }); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /GraphQLSampleApp/WeatherForecast.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace GraphQLSampleApp 4 | { 5 | public class WeatherForecast 6 | { 7 | public DateTime Date { get; set; } 8 | 9 | public int TemperatureC { get; set; } 10 | 11 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 12 | 13 | public string Summary { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /GraphQLSampleApp/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /GraphQLSampleApp/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings": { 3 | "SampleAppDbContext": "Data Source=DESKTOP-HC36QP0;Initial Catalog=GraphQLSampleApp;Integrated Security=True;" 4 | }, 5 | 6 | "Logging": { 7 | "LogLevel": { 8 | "Default": "Information", 9 | "Microsoft": "Warning", 10 | "Microsoft.Hosting.Lifetime": "Information" 11 | } 12 | }, 13 | "AllowedHosts": "*" 14 | } 15 | --------------------------------------------------------------------------------