forked from ChilliCream/hotchocolate-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pure code-first example (ChilliCream#3)
- Loading branch information
1 parent
d861936
commit 09217a0
Showing
27 changed files
with
866 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,34 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": ".NET Core Launch (web)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/StarWars.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}", | ||
"stopAtEntry": false, | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach", | ||
"processId": "${command:pickProcess}" | ||
} | ||
] | ||
} |
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,24 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "shell", | ||
"args": [ | ||
"build", | ||
// Ask dotnet build to generate full paths for file names. | ||
"/property:GenerateFullPaths=true", | ||
// Do not generate summary otherwise it leads to duplicate errors in Problems panel | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"group": "build", | ||
"presentation": { | ||
"reveal": "silent" | ||
}, | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
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,51 @@ | ||
using System.Collections.Generic; | ||
using HotChocolate; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Relay; | ||
using StarWars.Repositories; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
[ExtendObjectType(Name = "Query")] | ||
public class CharacterQueries | ||
{ | ||
/// <summary> | ||
/// Retrieve a hero by a particular Star Wars episode. | ||
/// </summary> | ||
/// <param name="episode">The episode to look up by.</param> | ||
/// <param name="repository"></param> | ||
/// <returns>The character.</returns> | ||
public ICharacter GetHero( | ||
Episode episode, | ||
[Service]ICharacterRepository repository) => | ||
repository.GetHero(episode); | ||
|
||
/// <summary> | ||
/// Gets all character. | ||
/// </summary> | ||
/// <param name="repository"></param> | ||
/// <returns>The character.</returns> | ||
[UsePaging] | ||
[UseFiltering] | ||
[UseSorting] | ||
public IEnumerable<ICharacter> GetCharacters( | ||
[Service]ICharacterRepository repository) => | ||
repository.GetCharacters(); | ||
|
||
/// <summary> | ||
/// Gets a character by it`s id. | ||
/// </summary> | ||
/// <param name="ids">The ids of the human to retrieve.</param> | ||
/// <param name="repository"></param> | ||
/// <returns>The character.</returns> | ||
public IEnumerable<ICharacter> GetCharacter( | ||
int[] ids, | ||
[Service]ICharacterRepository repository) => | ||
repository.GetCharacters(ids); | ||
|
||
public IEnumerable<ISearchResult> Search( | ||
string text, | ||
[Service]ICharacterRepository repository) => | ||
repository.Search(text); | ||
} | ||
} |
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,51 @@ | ||
using System.Collections.Generic; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Relay; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// A droid in the Star Wars universe. | ||
/// </summary> | ||
public class Droid : ICharacter | ||
{ | ||
public Droid( | ||
int id, | ||
string name, | ||
IReadOnlyList<int> friends, | ||
IReadOnlyList<Episode> appearsIn, | ||
string primaryFunction, | ||
double height = 1.72d) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Friends = friends; | ||
AppearsIn = appearsIn; | ||
PrimaryFunction = primaryFunction; | ||
Height = height; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int Id { get; } | ||
|
||
/// <inheritdoc /> | ||
public string Name { get; } | ||
|
||
/// <inheritdoc /> | ||
[UsePaging(SchemaType = typeof(InterfaceType<ICharacter>))] | ||
[GetFriendsResolver] | ||
public IReadOnlyList<int> Friends { get; } | ||
|
||
/// <inheritdoc /> | ||
public IReadOnlyList<Episode> AppearsIn { get; } | ||
|
||
/// <summary> | ||
/// The droid's primary function. | ||
/// </summary> | ||
public string PrimaryFunction { get; } | ||
|
||
/// <inheritdoc /> | ||
[UseConvertUnit] | ||
public double Height { get; } | ||
} | ||
} |
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,23 @@ | ||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// The Star Wars episodes. | ||
/// </summary> | ||
public enum Episode | ||
{ | ||
/// <summary> | ||
/// Star Wars Episode IV: A New Hope | ||
/// </summary> | ||
NewHope, | ||
|
||
/// <summary> | ||
/// Star Wars Episode V: Empire Strikes Back | ||
/// </summary> | ||
Empire, | ||
|
||
/// <summary> | ||
/// Star Wars Episode VI: Return of the Jedi | ||
/// </summary> | ||
Jedi | ||
} | ||
} |
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,25 @@ | ||
using System.Linq; | ||
using System.Reflection; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
using StarWars.Repositories; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
public sealed class GetFriendsResolverAttribute | ||
: ObjectFieldDescriptorAttribute | ||
{ | ||
public override void OnConfigure( | ||
IDescriptorContext context, | ||
IObjectFieldDescriptor descriptor, | ||
MemberInfo member) | ||
{ | ||
descriptor.Resolver(ctx => | ||
{ | ||
ICharacter character = ctx.Parent<ICharacter>(); | ||
ICharacterRepository repository = ctx.Service<ICharacterRepository>(); | ||
return repository.GetCharacters(character.Friends.ToArray()); | ||
}); | ||
} | ||
} | ||
} |
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,51 @@ | ||
using System.Collections.Generic; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Relay; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// A human character in the Star Wars universe. | ||
/// </summary> | ||
public class Human : ICharacter | ||
{ | ||
public Human( | ||
int id, | ||
string name, | ||
IReadOnlyList<int> friends, | ||
IReadOnlyList<Episode> appearsIn, | ||
string? homePlanet = null, | ||
double height = 1.72d) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Friends = friends; | ||
AppearsIn = appearsIn; | ||
HomePlanet = homePlanet; | ||
Height = height; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int Id { get; } | ||
|
||
/// <inheritdoc /> | ||
public string Name { get; } | ||
|
||
/// <inheritdoc /> | ||
[UsePaging(SchemaType = typeof(InterfaceType<ICharacter>))] | ||
[GetFriendsResolver] | ||
public IReadOnlyList<int> Friends { get; } | ||
|
||
/// <inheritdoc /> | ||
public IReadOnlyList<Episode> AppearsIn { get; } | ||
|
||
/// <summary> | ||
/// The planet the character is originally from. | ||
/// </summary> | ||
public string? HomePlanet { get; } | ||
|
||
/// <inheritdoc /> | ||
[UseConvertUnit] | ||
public double Height { get; } | ||
} | ||
} |
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,40 @@ | ||
using System.Collections.Generic; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Relay; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// A character in the Star Wars universe. | ||
/// </summary> | ||
[InterfaceType(Name = "Character")] | ||
public interface ICharacter : ISearchResult | ||
{ | ||
/// <summary> | ||
/// The unique identifier for the character. | ||
/// </summary> | ||
int Id { get; } | ||
|
||
/// <summary> | ||
/// The name of the character. | ||
/// </summary> | ||
string Name { get; } | ||
|
||
/// <summary> | ||
/// The ids of the character's friends. | ||
/// </summary> | ||
[UsePaging(SchemaType = typeof(InterfaceType<ICharacter>))] | ||
IReadOnlyList<int> Friends { get; } | ||
|
||
/// <summary> | ||
/// The episodes the character appears in. | ||
/// </summary> | ||
IReadOnlyList<Episode> AppearsIn { get; } | ||
|
||
/// <summary> | ||
/// The height of the character. | ||
/// </summary> | ||
[UseConvertUnit] | ||
double Height { get; } | ||
} | ||
} |
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,9 @@ | ||
using HotChocolate.Types; | ||
|
||
namespace StarWars.Characters | ||
{ | ||
[UnionType(Name = "SearchResult")] | ||
public interface ISearchResult | ||
{ | ||
} | ||
} |
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,31 @@ | ||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// A starship in the Star Wars universe. | ||
/// </summary> | ||
public class Starship : ISearchResult | ||
{ | ||
public Starship(int id, string name, double length) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Length = length; | ||
} | ||
|
||
/// <summary> | ||
/// The Id of the starship. | ||
/// </summary> | ||
public int Id { get; } | ||
|
||
/// <summary> | ||
/// The name of the starship. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// The length of the starship. | ||
/// </summary> | ||
[UseConvertUnit] | ||
public double Length { get; } | ||
} | ||
} |
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,11 @@ | ||
namespace StarWars.Characters | ||
{ | ||
/// <summary> | ||
/// Different units of measurement. | ||
/// </summary> | ||
public enum Unit | ||
{ | ||
Foot, | ||
Meters | ||
} | ||
} |
Oops, something went wrong.