From 04eba68110a8eb8747f451a4f88a59144729d5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C4=99czek?= Date: Sun, 26 May 2019 22:54:34 +0200 Subject: [PATCH] Adding Azure Cosmos DB as data storage --- Demo.Azure.Functions.GraphQL.sln | 2 +- .../Demo.Azure.Functions.GraphQL.csproj | 3 +- .../Documents/Character.cs | 2 +- .../Schema/Queries/CharacterQuery.cs | 33 +++----------- Demo.Azure.Functions.GraphQL/SeedFunction.cs | 43 +++++++++++++++++++ Demo.Azure.Functions.GraphQL/Startup.cs | 22 +++++++++- 6 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 Demo.Azure.Functions.GraphQL/SeedFunction.cs diff --git a/Demo.Azure.Functions.GraphQL.sln b/Demo.Azure.Functions.GraphQL.sln index 42a6ac6..6a20f30 100644 --- a/Demo.Azure.Functions.GraphQL.sln +++ b/Demo.Azure.Functions.GraphQL.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.28803.156 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.Azure.Functions.GraphQL", "Demo.Azure.Functions.GraphQL\Demo.Azure.Functions.GraphQL.csproj", "{960BB554-2683-4773-B86A-708C6173968D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.Azure.Functions.GraphQL", "Demo.Azure.Functions.GraphQL\Demo.Azure.Functions.GraphQL.csproj", "{960BB554-2683-4773-B86A-708C6173968D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Demo.Azure.Functions.GraphQL/Demo.Azure.Functions.GraphQL.csproj b/Demo.Azure.Functions.GraphQL/Demo.Azure.Functions.GraphQL.csproj index 299d099..e5749fd 100644 --- a/Demo.Azure.Functions.GraphQL/Demo.Azure.Functions.GraphQL.csproj +++ b/Demo.Azure.Functions.GraphQL/Demo.Azure.Functions.GraphQL.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.1 v2 @@ -7,6 +7,7 @@ + diff --git a/Demo.Azure.Functions.GraphQL/Documents/Character.cs b/Demo.Azure.Functions.GraphQL/Documents/Character.cs index bdf4c79..2da3f3a 100644 --- a/Demo.Azure.Functions.GraphQL/Documents/Character.cs +++ b/Demo.Azure.Functions.GraphQL/Documents/Character.cs @@ -2,7 +2,7 @@ namespace Demo.Azure.Functions.GraphQL.Documents { - internal class Character + public class Character { public string Id { get; set; } diff --git a/Demo.Azure.Functions.GraphQL/Schema/Queries/CharacterQuery.cs b/Demo.Azure.Functions.GraphQL/Schema/Queries/CharacterQuery.cs index 9ca9065..62271e5 100644 --- a/Demo.Azure.Functions.GraphQL/Schema/Queries/CharacterQuery.cs +++ b/Demo.Azure.Functions.GraphQL/Schema/Queries/CharacterQuery.cs @@ -1,5 +1,6 @@ using System; -using System.Collections.Generic; +using Microsoft.Azure.Documents; +using Microsoft.Azure.Documents.Client; using GraphQL.Types; using Demo.Azure.Functions.GraphQL.Documents; using Demo.Azure.Functions.GraphQL.Schema.Types; @@ -8,38 +9,16 @@ namespace Demo.Azure.Functions.GraphQL.Schema.Queries { internal class CharacterQuery: ObjectGraphType { - private static readonly IList _characters = new List - { - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Luke Skywalker", BirthYear = "19BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "C-3PO", BirthYear = "112BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "R2-D2", BirthYear = "33BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Darth Vader", BirthYear = "41.9BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Leia Organa", BirthYear = "19BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Owen Lars", BirthYear = "52BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Beru Whitesun Lars", BirthYear = "47BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "R5-D4", BirthYear = "Unknown" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Biggs Darklighter", BirthYear = "24BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Obi-Wan Kenobi", BirthYear = "57BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Anakin Skywalker", BirthYear = "41.9BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Wilhuff Tarkin", BirthYear = "64BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Chewbacca", BirthYear = "200BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Han Solo", BirthYear = "29BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Greedo", BirthYear = "44BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Jabba Desilijic Tiure", BirthYear = "600BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Wedge Antilles", BirthYear = "21BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Jek Tono Porkins", BirthYear = "Unknown" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Yoda", BirthYear = "896BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Palpatine", BirthYear = "82BBY" }, - new Character { Id = Guid.NewGuid().ToString("N"), Name = "Boba Fett", BirthYear = "31.5BBY" } - }; + private static readonly Uri _charactersCollectionUri = UriFactory.CreateDocumentCollectionUri("Demo.Azure.Functions.GraphQL", "CharactersCollection"); + private static readonly FeedOptions _feedOptions = new FeedOptions { MaxItemCount = -1, }; - public CharacterQuery() + public CharacterQuery(IDocumentClient documentClient) { Field>( "characters", resolve: context => { - return _characters; + return documentClient.CreateDocumentQuery(_charactersCollectionUri, _feedOptions); }); } } diff --git a/Demo.Azure.Functions.GraphQL/SeedFunction.cs b/Demo.Azure.Functions.GraphQL/SeedFunction.cs new file mode 100644 index 0000000..4a6e967 --- /dev/null +++ b/Demo.Azure.Functions.GraphQL/SeedFunction.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.WebJobs; +using Microsoft.Azure.WebJobs.Extensions.Http; +using Demo.Azure.Functions.GraphQL.Documents; + +namespace Demo.Azure.Functions.GraphQL +{ + public static class SeedFunction + { + [FunctionName("seed")] + public static async Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest request, + [CosmosDB(databaseName: "Demo.Azure.Functions.GraphQL", collectionName: "CharactersCollection", ConnectionStringSetting = "CosmosDBConnection", CreateIfNotExists = true)] IAsyncCollector charactersCollector) + { + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Luke Skywalker", BirthYear = "19BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "C-3PO", BirthYear = "112BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "R2-D2", BirthYear = "33BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Darth Vader", BirthYear = "41.9BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Leia Organa", BirthYear = "19BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Owen Lars", BirthYear = "52BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Beru Whitesun Lars", BirthYear = "47BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "R5-D4", BirthYear = "Unknown" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Biggs Darklighter", BirthYear = "24BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Obi-Wan Kenobi", BirthYear = "57BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Anakin Skywalker", BirthYear = "41.9BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Wilhuff Tarkin", BirthYear = "64BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Chewbacca", BirthYear = "200BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Han Solo", BirthYear = "29BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Greedo", BirthYear = "44BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Jabba Desilijic Tiure", BirthYear = "600BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Wedge Antilles", BirthYear = "21BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Jek Tono Porkins", BirthYear = "Unknown" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Yoda", BirthYear = "896BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Palpatine", BirthYear = "82BBY" }); + await charactersCollector.AddAsync(new Character { Id = Guid.NewGuid().ToString("N"), Name = "Boba Fett", BirthYear = "31.5BBY" }); + + return new OkResult(); + } + } +} diff --git a/Demo.Azure.Functions.GraphQL/Startup.cs b/Demo.Azure.Functions.GraphQL/Startup.cs index 62ae0e9..790bd94 100644 --- a/Demo.Azure.Functions.GraphQL/Startup.cs +++ b/Demo.Azure.Functions.GraphQL/Startup.cs @@ -1,4 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using System.Data.Common; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Azure.Documents; +using Microsoft.Azure.Documents.Client; using Microsoft.Azure.Functions.Extensions.DependencyInjection; using GraphQL; using GraphQL.Server; @@ -12,6 +17,21 @@ public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { + builder.Services.AddSingleton((serviceProvider) => { + DbConnectionStringBuilder cosmosDBConnectionStringBuilder = new DbConnectionStringBuilder + { + ConnectionString = serviceProvider.GetRequiredService()["CosmosDBConnection"] + }; + + if (cosmosDBConnectionStringBuilder.TryGetValue("AccountKey", out object accountKey) && cosmosDBConnectionStringBuilder.TryGetValue("AccountEndpoint", out object accountEndpoint)) + { + return new DocumentClient(new Uri(accountEndpoint.ToString()), accountKey.ToString()); + + } + + return null; + }); + builder.Services.AddScoped(serviceProvider => new FuncDependencyResolver(serviceProvider.GetRequiredService)); builder.Services.AddScoped();