-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Demo.Azure.Functions.GraphQL/Schema/Mutations/StarWarsMutation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Demo.Azure.Functions.GraphQL/Schema/Types/CharacterCreationType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters