Skip to content

Commit

Permalink
Adding mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeczek committed May 26, 2020
1 parent 0f8cec1 commit 21c6ce4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Demo.Azure.Functions.GraphQL/Schema/Mutations/StarWarsMutation.cs
Original file line number Diff line number Diff line change
@@ -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<CharacterType>(
"createCharacter",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<IntGraphType>> { Name = HOMEWORLD_MUTATION_ARGUMENT },
new QueryArgument<NonNullGraphType<CharacterCreationType>> { Name = CHARACTER_MUTATION_ARGUMENT }
),
resolve: async context =>
{
Character character = context.GetArgument<Character>(CHARACTER_MUTATION_ARGUMENT);
character.HomeworldId = context.GetArgument<int>(HOMEWORLD_MUTATION_ARGUMENT);
character.CharacterId = Guid.NewGuid().ToString("N");

await documentClient.CreateDocumentAsync(_charactersCollectionUri, character);

return character;
}
);
}
}
}
2 changes: 2 additions & 0 deletions Demo.Azure.Functions.GraphQL/Schema/StarWarsSchema.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -10,6 +11,7 @@ public StarWarsSchema(IDependencyResolver dependencyResolver)
: base(dependencyResolver)
{
Query = dependencyResolver.Resolve<StarWarsQuery>();
Mutation = dependencyResolver.Resolve<StarWarsMutation>();
}
}
}
14 changes: 14 additions & 0 deletions Demo.Azure.Functions.GraphQL/Schema/Types/CharacterCreationType.cs
Original file line number Diff line number Diff line change
@@ -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<NonNullGraphType<StringGraphType>>(nameof(Character.Name));
Field<StringGraphType>(nameof(Character.BirthYear));
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 21c6ce4

Please sign in to comment.