Skip to content

Commit

Permalink
Adding Azure Cosmos DB as data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeczek committed May 26, 2019
1 parent 88e3e53 commit 04eba68
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Demo.Azure.Functions.GraphQL.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
Expand All @@ -7,6 +7,7 @@
<PackageReference Include="GraphQL" Version="2.4.0" />
<PackageReference Include="GraphQL.Server.Core" Version="3.4.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Demo.Azure.Functions.GraphQL/Documents/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Demo.Azure.Functions.GraphQL.Documents
{
internal class Character
public class Character
{
public string Id { get; set; }

Expand Down
33 changes: 6 additions & 27 deletions Demo.Azure.Functions.GraphQL/Schema/Queries/CharacterQuery.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,38 +9,16 @@ namespace Demo.Azure.Functions.GraphQL.Schema.Queries
{
internal class CharacterQuery: ObjectGraphType
{
private static readonly IList<Character> _characters = new List<Character>
{
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<ListGraphType<CharacterType>>(
"characters",
resolve: context =>
{
return _characters;
return documentClient.CreateDocumentQuery<Character>(_charactersCollectionUri, _feedOptions);
});
}
}
Expand Down
43 changes: 43 additions & 0 deletions Demo.Azure.Functions.GraphQL/SeedFunction.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest request,
[CosmosDB(databaseName: "Demo.Azure.Functions.GraphQL", collectionName: "CharactersCollection", ConnectionStringSetting = "CosmosDBConnection", CreateIfNotExists = true)] IAsyncCollector<Character> 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();
}
}
}
22 changes: 21 additions & 1 deletion Demo.Azure.Functions.GraphQL/Startup.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +17,21 @@ public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IDocumentClient>((serviceProvider) => {
DbConnectionStringBuilder cosmosDBConnectionStringBuilder = new DbConnectionStringBuilder
{
ConnectionString = serviceProvider.GetRequiredService<IConfiguration>()["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<IDependencyResolver>(serviceProvider => new FuncDependencyResolver(serviceProvider.GetRequiredService));
builder.Services.AddScoped<StarWarsSchema>();

Expand Down

0 comments on commit 04eba68

Please sign in to comment.