%3CLINGO-SUB%20id%3D%22lingo-sub-1537396%22%20slang%3D%22en-US%22%3EGraphQL%20on%20Azure%3A%20Part%202%20-%20dotnet%20and%20App%20Service%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1537396%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20my%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fapps-on-azure%2Fgraphql-on-azure-part-1-getting-started%2Fba-p%2F1537359%22%20target%3D%22_self%22%3Eintroductory%20post%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewe%20saw%20that%20there%20are%20many%20different%20ways%20in%20which%20you%20can%20host%20a%20GraphQL%20service%20on%20Azure%20and%20today%20we%E2%80%99ll%20take%20a%20deeper%20look%20at%20one%20such%20option%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapp-service%2F%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20App%20Service%3C%2FA%3E%2C%20by%20building%20a%20GraphQL%20server%20using%20dotnet.%20If%20you%E2%80%99re%20only%20interested%20in%20the%20Azure%20deployment%2C%20you%20can%20jump%20forward%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22%23deploying-to-app-service%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethat%20section%3C%2FA%3E.%20Also%2C%20you%E2%80%99ll%20find%20the%20complete%20sample%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Faaronpowell%2Fappservice-graphql-dotnet%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eon%20my%20GitHub%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1298967512%22%20id%3D%22toc-hId--1298190125%22%3E-ERR%3AREF-NOT-FOUND-Getting%20Started%3C%2FH2%3E%0A%3CP%3EFor%20our%20server%2C%20we%E2%80%99ll%20use%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fgraphql-dotnet%2Fgraphql-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Egraphql-dotnet%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproject%2C%20which%20is%20one%20of%20the%20most%20common%20GraphQL%20server%20implementations%20for%20dotnet.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20up%2C%20we%E2%80%99ll%20need%20an%20ASP.NET%20Core%20web%20application%2C%20which%20we%20can%20create%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ecli%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%20class%3D%22highlight%20shell%22%3E%3CCODE%3Edotnet%20new%20web%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3ENext%2C%20open%20the%20project%20in%20an%20editor%20and%20add%20the%20NuGet%20packages%20we%E2%80%99ll%20need%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-html%22%3E%3CCODE%3E%3CPACKAGEREFERENCE%20include%3D%22GraphQL.Server.Core%22%20version%3D%223.5.0-alpha0046%22%3E%3C%2FPACKAGEREFERENCE%3E%0A%3CPACKAGEREFERENCE%20include%3D%22GraphQL.Server.Transports.AspNetCore%22%20version%3D%223.5.0-alpha0046%22%3E%3C%2FPACKAGEREFERENCE%3E%0A%3CPACKAGEREFERENCE%20include%3D%22GraphQL.Server.Transports.AspNetCore.SystemTextJson%22%20version%3D%223.5.0-alpha0046%22%3E%3C%2FPACKAGEREFERENCE%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EAt%20the%20time%20of%20writing%20graphql-dotnet%20v3%20is%20in%20preview%2C%20we%E2%80%99re%20going%20to%20use%20that%20for%20our%20server%20but%20be%20aware%20there%20may%20be%20changes%20when%20it%20is%20released.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThese%20packages%20will%20provide%20us%20a%20GraphQL%20server%2C%20along%20with%20the%20middleware%20needed%20to%20wire%20it%20up%20with%20ASP.NET%20Core%20and%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fdotnet%2Fstandard%2Fserialization%2Fsystem-text-json-overview%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ESystem.Text.Json%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eas%20the%20JSON%20seralizer%2Fdeserializer%20(you%20can%20use%20Newtonsoft.Json%20if%20you%20prefer%20with%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.nuget.org%2Fpackages%2FGraphQL.Server.Transports.AspNetCore.NewtonsoftJson%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20package%3C%2FA%3E).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%E2%80%99ll%20also%20add%20a%20package%20for%20GraphiQL%2C%20the%20GraphQL%20UI%20playground%2C%20but%20it%E2%80%99s%20not%20needed%20or%20recommended%20when%20deploying%20into%20production.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-html%22%3E%3CCODE%3E%3CPACKAGEREFERENCE%20include%3D%22GraphQL.Server.Ui.Playground%22%20version%3D%223.5.0-alpha0046%22%3E%3C%2FPACKAGEREFERENCE%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20packages%20installed%2C%20it%E2%80%99s%20time%20to%20setup%20the%20server.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1188545321%22%20id%3D%22toc-hId-1189322708%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23implementing-a-server%22%20name%3D%22implementing-a-server%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3EImplementing%20a%20Server%3C%2FH2%3E%0A%3CP%3EThere%20are%20a%20few%20things%20that%20we%20need%20when%20it%20comes%20to%20implementing%20the%20server%2C%20we%E2%80%99re%20going%20to%20need%20a%20GraphQL%20schema%2C%20some%20types%20that%20implement%20that%20schema%20and%20to%20configure%20our%20route%20engine%20to%20support%20GraphQL%E2%80%99s%20endpoints.%20We%E2%80%99ll%20start%20by%20defining%20the%20schema%20that%E2%80%99s%20going%20to%20support%20our%20server%20and%20for%20the%20schema%20we%E2%80%99ll%20use%20a%20basic%20trivia%20app%20(which%20I%E2%80%99ve%20used%20for%20a%20number%20of%20GraphQL%20demos%20in%20the%20past).%20For%20the%20data%2C%20we%E2%80%99ll%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fopentdb.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOpen%20Trivia%20DB%3C%2FA%3E.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1879106795%22%20id%3D%22toc-hId-1879884182%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23net-types%22%20name%3D%22net-types%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3E.NET%20Types%3C%2FH3%3E%0A%3CP%3EFirst%20up%2C%20we%E2%80%99re%20going%20to%20need%20some%20generic%20.NET%20types%20that%20will%20represent%20the%20underlying%20data%20structure%20for%20our%20application.%20These%20would%20be%20the%20DTOs%20(Data%20Transfer%20Objects)%20that%20we%20might%20use%20in%20Entity%20Framework%2C%20but%20we%E2%80%99re%20just%20going%20to%20run%20in%20memory.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20Quiz%0A%7B%0A%20%20%20%20public%20string%20Id%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20get%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Question.ToLower().Replace(%22%20%22%2C%20%22-%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20public%20string%20Question%20%7B%20get%3B%20set%3B%20%7D%0A%20%20%20%20%5BJsonPropertyName(%22correct_answer%22)%5D%0A%20%20%20%20public%20string%20CorrectAnswer%20%7B%20get%3B%20set%3B%20%7D%0A%20%20%20%20%5BJsonPropertyName(%22incorrect_answers%22)%5D%0A%20%20%20%20public%20List%20IncorrectAnswers%20%7B%20get%3B%20set%3B%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20you%20can%20see%2C%20it%E2%80%99s%20a%20fairly%20generic%20C%23%20class.%20We%E2%80%99ve%20added%20a%20few%20serialization%20attributes%20to%20help%20converting%20the%20JSON%20to%20.NET%2C%20but%20otherwise%20it%E2%80%99s%20nothing%20special.%20It%E2%80%99s%20also%20not%20usable%20with%20GraphQL%20yet%20and%20for%20that%2C%20we%20need%20to%20expose%20the%20type%20to%20a%20GraphQL%20schema%2C%20and%20to%20do%20that%20we%E2%80%99ll%20create%20a%20new%20class%20that%20inherits%20from%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EObjectGraphType%3CQUIZ%3E%3C%2FQUIZ%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewhich%20comes%20from%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGraphQL.Types%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Enamespace%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20QuizType%20%3A%20ObjectGraphType%3CQUIZ%3E%0A%7B%0A%20%20%20%20public%20QuizType()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Name%20%3D%20%22Quiz%22%3B%0A%20%20%20%20%20%20%20%20Description%20%3D%20%22A%20representation%20of%20a%20single%20quiz.%22%3B%0A%20%20%20%20%20%20%20%20Field(q%20%3D%26gt%3B%20q.Id%2C%20nullable%3A%20false)%3B%0A%20%20%20%20%20%20%20%20Field(q%20%3D%26gt%3B%20q.Question%2C%20nullable%3A%20false)%3B%0A%20%20%20%20%20%20%20%20Field(q%20%3D%26gt%3B%20q.CorrectAnswer%2C%20nullable%3A%20false)%3B%0A%20%20%20%20%20%20%20%20Field%3CNONNULLGRAPHTYPE%3E%3CLISTGRAPHTYPE%3E%3CNONNULLGRAPHTYPE%3E%26gt%3B%26gt%3B(%22incorrectAnswers%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FNONNULLGRAPHTYPE%3E%3C%2FLISTGRAPHTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FQUIZ%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EName%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EDescription%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperties%20are%20used%20provide%20the%20documentation%20for%20the%20type%2C%20next%20we%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EField%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20define%20what%20we%20want%20exposed%20in%20the%20schema%20and%20how%20we%20want%20that%20marked%20up%20for%20the%20GraphQL%20type%20system.%20We%20do%20this%20for%20each%20field%20of%20the%20DTO%20that%20we%20want%20to%20expose%20using%20a%20lambda%20like%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eq%20%3D%26gt%3B%20q.Id%3C%2FCODE%3E%2C%20or%20by%20giving%20an%20explicit%20field%20name%20(%3CCODE%3EincorrectAnswers%3C%2FCODE%3E).%20Here%E2%80%99s%20also%20where%20you%20control%20the%20schema%20validation%20information%20as%20well%2C%20defining%20the%20nullability%20of%20the%20fields%20to%20match%20the%20way%20GraphQL%20expects%20it%20to%20be%20represented.%20This%20class%20would%20make%20a%20GraphQL%20type%20representation%20of%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%20class%3D%22highlight%20graphql%22%3E%3CCODE%3E%3CSPAN%20class%3D%22k%22%3Etype%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22n%22%3EQuiz%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22p%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3Eid%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nb%22%3EString%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3Equestion%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nb%22%3EString%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3EcorrectAnswer%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nb%22%3EString%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3EincorrectAnswers%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22p%22%3E%5B%3C%2FSPAN%3E%3CSPAN%20class%3D%22nb%22%3EString%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%5D!%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22p%22%3E%7D%3C%2FSPAN%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3EFinally%2C%20we%20want%20to%20expose%20a%20way%20to%20query%20our%20the%20types%20in%20our%20schema%2C%20and%20for%20that%20we'll%20need%20a%20Query%20that%20inherits%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EObjectGraphType%3C%2FCODE%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20TriviaQuery%20%3A%20ObjectGraphType%0A%7B%0A%20%20%20%20public%20TriviaQuery()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Field%3CNONNULLGRAPHTYPE%3E%3CLISTGRAPHTYPE%3E%3CNONNULLGRAPHTYPE%3E%3CQUIZTYPE%3E%26gt%3B%26gt%3B%26gt%3B(%22quizzes%22%2C%20resolve%3A%20context%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20NotImplementedException()%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20Field%3CNONNULLGRAPHTYPE%3E%3CQUIZTYPE%3E%26gt%3B(%22quiz%22%2C%20arguments%3A%20new%20QueryArguments()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20new%20QueryArgument%3CNONNULLGRAPHTYPE%3E%3CSTRINGGRAPHTYPE%3E%26gt%3B%20%7B%20Name%20%3D%20%22id%22%2C%20Description%20%3D%20%22id%20of%20the%20quiz%22%20%7D%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20resolve%3A%20(context)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20NotImplementedException()%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FSTRINGGRAPHTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FQUIZTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FQUIZTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FLISTGRAPHTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERight%20now%20there%20is%20only%20a%20single%20type%20in%20our%20schema%2C%20but%20if%20you%20had%20multiple%20then%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ETriviaQuery%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewould%20have%20more%20fields%20with%20resolvers%20to%20represent%20them.%20We've%20also%20not%20implemented%20the%20resolver%2C%20which%20is%20how%20GraphQL%20gets%20the%20data%20to%20return%2C%20we'll%20come%20back%20to%20that%20a%20bit%20later.%20This%20class%20produces%20the%20equivalent%20of%20the%20following%20GraphQL%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%20class%3D%22highlight%20graphql%22%3E%3CCODE%3E%3CSPAN%20class%3D%22k%22%3Etype%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22n%22%3ETriviaQuery%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22p%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3Equizzes%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22p%22%3E%5B%3C%2FSPAN%3E%3CSPAN%20class%3D%22n%22%3EQuiz%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%5D!%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22n%22%3Equiz%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22n%22%3Eid%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nb%22%3EString%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!)%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22n%22%3EQuiz%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E!%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22p%22%3E%7D%3C%2FSPAN%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId-71652332%22%20id%3D%22toc-hId-72429719%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23creating-a-graphql-schema%22%20name%3D%22creating-a-graphql-schema%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3ECreating%20a%20GraphQL%20Schema%3C%2FH3%3E%0A%3CP%3EWith%20the%20DTO%20type%2C%20GraphQL%20type%20and%20Query%20type%20defined%2C%20we%20can%20now%20implement%20a%20schema%20to%20be%20used%20on%20the%20server%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20TriviaSchema%20%3A%20Schema%0A%7B%0A%20%20%20%20public%20TriviaSchema(TriviaQuery%20query)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Query%20%3D%20query%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20we%20would%20also%20have%20mutations%20and%20subscriptions%2C%20but%20we're%20not%20using%20them%20for%20this%20demo.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-61149228%22%20id%3D%22toc-hId-61926615%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23wiring-up-the-server%22%20name%3D%22wiring-up-the-server%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3EWiring%20up%20the%20Server%3C%2FH2%3E%0A%3CP%3EFor%20the%20Server%20we%20integrate%20with%20the%20ASP.NET%20Core%20pipeline%2C%20meaning%20that%20we%20need%20to%20setup%20some%20services%20for%20the%20Dependency%20Injection%20framework.%20Open%20up%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EStartup.cs%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20add%20update%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EConfigureServices%3C%2FCODE%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20void%20ConfigureServices(IServiceCollection%20services)%0A%7B%0A%20%20%20%20services.AddTransient%3CHTTPCLIENT%3E()%3B%0A%20%20%20%20services.AddSingleton%3CQUIZDATA%3E()%3B%0A%20%20%20%20services.AddSingleton%3CTRIVIAQUERY%3E()%3B%0A%20%20%20%20services.AddSingleton%3CISCHEMA%3E()%3B%0A%0A%20%20%20%20services.AddGraphQL(options%20%3D%26gt%3B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20options.EnableMetrics%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20options.ExposeExceptions%20%3D%20true%3B%0A%20%20%20%20%7D)%0A%20%20%20%20.AddSystemTextJson()%3B%0A%7D%0A%3C%2FISCHEMA%3E%3C%2FTRIVIAQUERY%3E%3C%2FQUIZDATA%3E%3C%2FHTTPCLIENT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20most%20important%20part%20of%20the%20configuration%20is%20lines%208%20-%2013%2C%20where%20the%20GraphQL%20server%20is%20setup%20and%20we're%20defining%20the%20JSON%20seralizer%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ESystem.Text.Json%3C%2FCODE%3E.%20All%20the%20lines%20above%20are%20defining%20dependencies%20that%20will%20be%20injected%20to%20other%20types%2C%20but%20there's%20a%20new%20type%20we've%20not%20seen%20before%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EQuizData%3C%2FCODE%3E.%20This%20type%20is%20just%20used%20to%20provide%20access%20to%20the%20data%20store%20that%20we're%20using%20(we're%20just%20doing%20in-memory%20storage%20using%20data%20queried%20from%20Open%20Trivia%20DB)%2C%20so%20I'll%20skip%20its%20implementation%20(you%20can%20see%20it%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Faaronpowell%2Fappservice-graphql-dotnet%2Fblob%2Fmaster%2FQuizData.cs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eon%20GitHub%3C%2FA%3E).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20data%20store%20available%2C%20we%20can%20update%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ETriviaQuery%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20consume%20the%20data%20store%20and%20use%20it%20in%20the%20resolvers%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20TriviaQuery%20%3A%20ObjectGraphType%0A%7B%0A%20%20%20%20public%20TriviaQuery(QuizData%20data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Field%3CNONNULLGRAPHTYPE%3E%3CLISTGRAPHTYPE%3E%3CNONNULLGRAPHTYPE%3E%3CQUIZTYPE%3E%26gt%3B%26gt%3B%26gt%3B(%22quizzes%22%2C%20resolve%3A%20context%20%3D%26gt%3B%20data.Quizzes)%3B%0A%0A%20%20%20%20%20%20%20%20Field%3CNONNULLGRAPHTYPE%3E%3CQUIZTYPE%3E%26gt%3B(%22quiz%22%2C%20arguments%3A%20new%20QueryArguments()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20new%20QueryArgument%3CNONNULLGRAPHTYPE%3E%3CSTRINGGRAPHTYPE%3E%26gt%3B%20%7B%20Name%20%3D%20%22id%22%2C%20Description%20%3D%20%22id%20of%20the%20quiz%22%20%7D%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20resolve%3A%20(context)%20%3D%26gt%3B%20data.FindById(context.GetArgument%3CSTRING%3E(%22id%22)))%3B%0A%20%20%20%20%7D%0A%7D%3C%2FSTRING%3E%3C%2FSTRINGGRAPHTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FQUIZTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FQUIZTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FLISTGRAPHTYPE%3E%3C%2FNONNULLGRAPHTYPE%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20services%20are%20defined%20we%20can%20add%20the%20routing%20in%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20void%20Configure(IApplicationBuilder%20app%2C%20IWebHostEnvironment%20env)%0A%7B%0A%20%20%20%20if%20(env.IsDevelopment())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20app.UseDeveloperExceptionPage()%3B%0A%20%20%20%20%20%20%20%20app.UseGraphQLPlayground()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20app.UseRouting()%3B%0A%0A%20%20%20%20app.UseGraphQL%3CISCHEMA%3E()%3B%0A%7D%3C%2FISCHEMA%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EI've%20put%20the%20inclusion%20GraphiQL.%20within%20the%20development%20environment%20check%20as%20that'd%20be%20how%20you'd%20want%20to%20do%20it%20for%20a%20real%20app%2C%20but%20in%20the%20demo%20on%20GitHub%20I%20include%20it%20every%20time.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20if%20we%20can%20launch%20our%20application%2C%20navigate%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A5001%2Fui%2Fplayground%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CCODE%3Ehttps%3A%2F%2Flocalhost%3A5001%2Fui%2Fplayground%3C%2FCODE%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20run%20the%20queries%20to%20get%20some%20data%20back.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1746305235%22%20id%3D%22toc-hId--1745527848%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23deploying-to-app-service%22%20name%3D%22deploying-to-app-service%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3EDeploying%20to%20App%20Service%3C%2FH2%3E%0A%3CP%3EWith%20all%20the%20code%20complete%2C%20let%E2%80%99s%20look%20at%20deploying%20it%20to%20Azure.%20For%20this%2C%20we%E2%80%99ll%20use%20a%20standard%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapp-service%2F%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20App%20Service%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Erunning%20the%20latest%20.NET%20Core%20(3.1%20at%20time%20of%20writing)%20on%20Windows.%20We%20don%E2%80%99t%20need%20to%20do%20anything%20special%20for%20the%20App%20Service%2C%20it%E2%80%99s%20already%20optimised%20to%20run%20an%20ASP.NET%20Core%20application%2C%20which%20is%20all%20this%20really%20is.%20If%20we%20were%20using%20a%20different%20runtime%2C%20like%20Node.js%2C%20we%E2%80%99d%20follow%20the%20standard%20setup%20for%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapp-service%2Fapp-service-web-get-started-nodejs%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ENode.js%20App%20Service%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20deploy%2C%20we%E2%80%99ll%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fhelp.github.com%2Fen%2Farticles%2Fabout-github-actions%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%20Actions%3C%2FA%3E%2C%20and%20you%E2%80%99ll%20find%20docs%20on%20how%20to%20do%20that%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapp-service%2Fdeploy-github-actions%3FWT.mc_id%3Dtechcommunity-blog-aapowell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ealready%20written%3C%2FA%3E.%20You%E2%80%99ll%20find%20the%20workflow%20file%20I%E2%80%99ve%20used%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Faaronpowell%2Fappservice-graphql-dotnet%2Fblob%2Fmaster%2F.github%2Fworkflows%2Fworkflow.yml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ein%20the%20GitHub%20repo%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20a%20workflow%20committed%20and%20pushed%20to%20GitHub%20and%20our%20App%20Service%20waiting%2C%20the%20Action%20will%20run%20and%20our%20application%20will%20be%20deployed.%20The%20demo%20I%20created%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgraphql-on-azure-appservice.azurewebsites.net%2Fui%2Fplayground%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eis%20here%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-741207598%22%20id%3D%22toc-hId-741984985%22%3E%3CA%20class%3D%22anchor%22%20href%3D%22%23conclusion%22%20name%3D%22conclusion%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3C%2FA%3EConclusion%3C%2FH2%3E%0A%3CP%3EThroughout%20this%20post%20we%E2%80%99ve%20taken%20a%20look%20at%20how%20we%20can%20create%20a%20GraphQL%20server%20running%20on%20ASP.NET%20Core%20using%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Egraphql-dotnet%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20deploy%20it%20to%20an%20Azure%20App%20Service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20it%20comes%20to%20the%20Azure%20side%20of%20things%2C%20there%E2%80%99s%20nothing%20different%20we%20have%20to%20do%20to%20run%20the%20GraphQL%20server%20in%20an%20App%20Service%20than%20any%20other%20ASP.NET%20Core%20application%2C%20as%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Egraphql-dotnet%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20implemented%20to%20leverage%20all%20the%20features%20of%20ASP.NET%20Core%20seamlessly.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAgain%2C%20you%E2%80%99ll%20find%20the%20complete%20sample%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Faaronpowell%2Fappservice-graphql-dotnet%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eon%20my%20GitHub%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efor%20you%20to%20play%20around%20with%20yourself.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EThis%20post%20was%20originally%20published%20on%20%3CA%20href%3D%22https%3A%2F%2Fwww.aaron-powell.com%2Fposts%2F2020-07-21-graphql-on-azure-part-2-app-service-with-dotnet%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewww.aaron-powell.com%3C%2FA%3E%26nbsp%3B.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1537396%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3ELet's%20look%20at%20how%20we%20can%20create%20a%20dotnet%20GraphQL%20server%20and%20deploy%20it%20to%20an%20AppService.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1537396%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.NET%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20App%20Service%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWeb%20Apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

In my introductory post we saw that there are many different ways in which you can host a GraphQL service on Azure and today we’ll take a deeper look at one such option, Azure App Service, by building a GraphQL server using dotnet. If you’re only interested in the Azure deployment, you can jump forward to that section. Also, you’ll find the complete sample on my GitHub.

Getting Started

For our server, we’ll use the graphql-dotnet project, which is one of the most common GraphQL server implementations for dotnet.

 

First up, we’ll need an ASP.NET Core web application, which we can create with the dotnet cli:

dotnet new web

Next, open the project in an editor and add the NuGet packages we’ll need:

 

<PackageReference Include="GraphQL.Server.Core" Version="3.5.0-alpha0046" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0046" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="3.5.0-alpha0046" />

 

At the time of writing graphql-dotnet v3 is in preview, we’re going to use that for our server but be aware there may be changes when it is released.

 

These packages will provide us a GraphQL server, along with the middleware needed to wire it up with ASP.NET Core and use System.Text.Json as the JSON seralizer/deserializer (you can use Newtonsoft.Json if you prefer with this package).

 

We’ll also add a package for GraphiQL, the GraphQL UI playground, but it’s not needed or recommended when deploying into production.

 

<PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.5.0-alpha0046" />

 

With the packages installed, it’s time to setup the server.

Implementing a Server

There are a few things that we need when it comes to implementing the server, we’re going to need a GraphQL schema, some types that implement that schema and to configure our route engine to support GraphQL’s endpoints. We’ll start by defining the schema that’s going to support our server and for the schema we’ll use a basic trivia app (which I’ve used for a number of GraphQL demos in the past). For the data, we’ll use Open Trivia DB.

.NET Types

First up, we’re going to need some generic .NET types that will represent the underlying data structure for our application. These would be the DTOs (Data Transfer Objects) that we might use in Entity Framework, but we’re just going to run in memory.

 

public class Quiz
{
    public string Id
    {
        get
        {
            return Question.ToLower().Replace(" ", "-");
        }
    }
    public string Question { get; set; }
    [JsonPropertyName("correct_answer")]
    public string CorrectAnswer { get; set; }
    [JsonPropertyName("incorrect_answers")]
    public List IncorrectAnswers { get; set; }
}

 

As you can see, it’s a fairly generic C# class. We’ve added a few serialization attributes to help converting the JSON to .NET, but otherwise it’s nothing special. It’s also not usable with GraphQL yet and for that, we need to expose the type to a GraphQL schema, and to do that we’ll create a new class that inherits from ObjectGraphType<Quiz> which comes from the GraphQL.Types namespace:

 

public class QuizType : ObjectGraphType<Quiz>
{
    public QuizType()
    {
        Name = "Quiz";
        Description = "A representation of a single quiz.";
        Field(q => q.Id, nullable: false);
        Field(q => q.Question, nullable: false);
        Field(q => q.CorrectAnswer, nullable: false);
        Field<NonNullGraphType<ListGraphType<NonNullGraphType>>>("incorrectAnswers");
    }
}

 

The Name and Description properties are used provide the documentation for the type, next we use Field to define what we want exposed in the schema and how we want that marked up for the GraphQL type system. We do this for each field of the DTO that we want to expose using a lambda like q => q.Id, or by giving an explicit field name (incorrectAnswers). Here’s also where you control the schema validation information as well, defining the nullability of the fields to match the way GraphQL expects it to be represented. This class would make a GraphQL type representation of:

 

type Quiz {
    id: String!
    question: String!
    correctAnswer: String!
    incorrectAnswers: [String!]!
}

Finally, we want to expose a way to query our the types in our schema, and for that we'll need a Query that inherits ObjectGraphType:

 

public class TriviaQuery : ObjectGraphType
{
    public TriviaQuery()
    {
        Field<NonNullGraphType<ListGraphType<NonNullGraphType<QuizType>>>>("quizzes", resolve: context =>
        {
            throw new NotImplementedException();
        });

        Field<NonNullGraphType<QuizType>>("quiz", arguments: new QueryArguments() {
            new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the quiz" }
        },
        resolve: (context) => {
            throw new NotImplementedException();
        });
    }
}

 

Right now there is only a single type in our schema, but if you had multiple then the TriviaQuery would have more fields with resolvers to represent them. We've also not implemented the resolver, which is how GraphQL gets the data to return, we'll come back to that a bit later. This class produces the equivalent of the following GraphQL:

type TriviaQuery {
    quizzes: [Quiz!]!
    quiz(id: String!): Quiz!
}

Creating a GraphQL Schema

With the DTO type, GraphQL type and Query type defined, we can now implement a schema to be used on the server:

 

public class TriviaSchema : Schema
{
    public TriviaSchema(TriviaQuery query)
    {
        Query = query;
    }
}

 

Here we would also have mutations and subscriptions, but we're not using them for this demo.

Wiring up the Server

For the Server we integrate with the ASP.NET Core pipeline, meaning that we need to setup some services for the Dependency Injection framework. Open up Startup.cs and add update the ConfigureServices:

 

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<HttpClient>();
    services.AddSingleton<QuizData>();
    services.AddSingleton<TriviaQuery>();
    services.AddSingleton<ISchema, TriviaSchema>();

    services.AddGraphQL(options =>
    {
        options.EnableMetrics = true;
        options.ExposeExceptions = true;
    })
    .AddSystemTextJson();
}

 

The most important part of the configuration is lines 8 - 13, where the GraphQL server is setup and we're defining the JSON seralizer, System.Text.Json. All the lines above are defining dependencies that will be injected to other types, but there's a new type we've not seen before, QuizData. This type is just used to provide access to the data store that we're using (we're just doing in-memory storage using data queried from Open Trivia DB), so I'll skip its implementation (you can see it on GitHub).

 

With the data store available, we can update TriviaQuery to consume the data store and use it in the resolvers:

 

public class TriviaQuery : ObjectGraphType
{
    public TriviaQuery(QuizData data)
    {
        Field<NonNullGraphType<ListGraphType<NonNullGraphType<QuizType>>>>("quizzes", resolve: context => data.Quizzes);

        Field<NonNullGraphType<QuizType>>("quiz", arguments: new QueryArguments() {
            new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the quiz" }
        },
        resolve: (context) => data.FindById(context.GetArgument<string>("id")));
    }
}

 

Once the services are defined we can add the routing in:

 

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseGraphQLPlayground();
    }

    app.UseRouting();

    app.UseGraphQL<ISchema>();
}

 

I've put the inclusion GraphiQL. within the development environment check as that'd be how you'd want to do it for a real app, but in the demo on GitHub I include it every time.

 

Now, if we can launch our application, navigate to https://localhost:5001/ui/playground and run the queries to get some data back.

Deploying to App Service

With all the code complete, let’s look at deploying it to Azure. For this, we’ll use a standard Azure App Service running the latest .NET Core (3.1 at time of writing) on Windows. We don’t need to do anything special for the App Service, it’s already optimised to run an ASP.NET Core application, which is all this really is. If we were using a different runtime, like Node.js, we’d follow the standard setup for a Node.js App Service.

 

To deploy, we’ll use GitHub Actions, and you’ll find docs on how to do that already written. You’ll find the workflow file I’ve used in the GitHub repo.

 

With a workflow committed and pushed to GitHub and our App Service waiting, the Action will run and our application will be deployed. The demo I created is here.

Conclusion

Throughout this post we’ve taken a look at how we can create a GraphQL server running on ASP.NET Core using graphql-dotnet and deploy it to an Azure App Service.

 

When it comes to the Azure side of things, there’s nothing different we have to do to run the GraphQL server in an App Service than any other ASP.NET Core application, as graphql-dotnet is implemented to leverage all the features of ASP.NET Core seamlessly.

 

Again, you’ll find the complete sample on my GitHub for you to play around with yourself.

 

This post was originally published on www.aaron-powell.com .