diff --git a/Demo.Azure.Functions.GraphQL/Schema/Mutations/StarWarsMutation.cs b/Demo.Azure.Functions.GraphQL/Schema/Mutations/StarWarsMutation.cs new file mode 100644 index 0000000..cbebe69 --- /dev/null +++ b/Demo.Azure.Functions.GraphQL/Schema/Mutations/StarWarsMutation.cs @@ -0,0 +1,39 @@ +using System; +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; +using Demo.Azure.Functions.GraphQL.Infrastructure; + +namespace Demo.Azure.Functions.GraphQL.Schema.Mutations +{ + internal class StarWarsMutation : ObjectGraphType + { + private const string HOMEWORLD_MUTATION_ARGUMENT = "homeworld"; + private const string CHARACTER_MUTATION_ARGUMENT = "character"; + + private static readonly Uri _charactersCollectionUri = UriFactory.CreateDocumentCollectionUri(Constants.DATABASE_NAME, Constants.CHARACTERS_COLLECTION_NAME); + + public StarWarsMutation(IDocumentClient documentClient) + { + FieldAsync( + "createCharacter", + arguments: new QueryArguments( + new QueryArgument> { Name = HOMEWORLD_MUTATION_ARGUMENT }, + new QueryArgument> { Name = CHARACTER_MUTATION_ARGUMENT } + ), + resolve: async context => + { + Character character = context.GetArgument(CHARACTER_MUTATION_ARGUMENT); + character.HomeworldId = context.GetArgument(HOMEWORLD_MUTATION_ARGUMENT); + character.CharacterId = Guid.NewGuid().ToString("N"); + + await documentClient.CreateDocumentAsync(_charactersCollectionUri, character); + + return character; + } + ); + } + } +} diff --git a/Demo.Azure.Functions.GraphQL/Schema/StarWarsSchema.cs b/Demo.Azure.Functions.GraphQL/Schema/StarWarsSchema.cs index e33e69d..b24acdb 100644 --- a/Demo.Azure.Functions.GraphQL/Schema/StarWarsSchema.cs +++ b/Demo.Azure.Functions.GraphQL/Schema/StarWarsSchema.cs @@ -1,6 +1,7 @@ using GraphQL; using GraphQLSchema = global::GraphQL.Types.Schema; using Demo.Azure.Functions.GraphQL.Schema.Queries; +using Demo.Azure.Functions.GraphQL.Schema.Mutations; namespace Demo.Azure.Functions.GraphQL.Schema { @@ -10,6 +11,7 @@ public StarWarsSchema(IDependencyResolver dependencyResolver) : base(dependencyResolver) { Query = dependencyResolver.Resolve(); + Mutation = dependencyResolver.Resolve(); } } } diff --git a/Demo.Azure.Functions.GraphQL/Schema/Types/CharacterCreationType.cs b/Demo.Azure.Functions.GraphQL/Schema/Types/CharacterCreationType.cs new file mode 100644 index 0000000..3d0c288 --- /dev/null +++ b/Demo.Azure.Functions.GraphQL/Schema/Types/CharacterCreationType.cs @@ -0,0 +1,14 @@ +using GraphQL.Types; +using Demo.Azure.Functions.GraphQL.Documents; + +namespace Demo.Azure.Functions.GraphQL.Schema.Types +{ + internal class CharacterCreationType: InputObjectGraphType + { + public CharacterCreationType() + { + Field>(nameof(Character.Name)); + Field(nameof(Character.BirthYear)); + } + } +} diff --git a/README.md b/README.md index df192f8..d7e7442 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Sample project for demonstrating GraphQL in Azure Functions. You can read more here: - [Serverless GraphQL with Azure Functions, GraphQL for .NET, and Cosmos DB](https://www.tpeczek.com/2019/05/serverless-graphql-with-azure-functions.html) +- [More Performant Serverless GraphQL with Azure Functions, GraphQL for .NET, and Cosmos DB](https://www.tpeczek.com/2020/05/more-performant-serverless-graphql-with.html) ## Donating