Skip to content

Commit

Permalink
Added pure code-first example (ChilliCream#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 25, 2019
1 parent d861936 commit 09217a0
Show file tree
Hide file tree
Showing 27 changed files with 866 additions and 0 deletions.
Binary file added PureCodeFirst/.DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions PureCodeFirst/.vscode/launch.json
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}"
}
]
}
24 changes: 24 additions & 0 deletions PureCodeFirst/.vscode/tasks.json
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"
}
]
}
51 changes: 51 additions & 0 deletions PureCodeFirst/Charachters/CharacterQueries.cs
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);
}
}
51 changes: 51 additions & 0 deletions PureCodeFirst/Charachters/Droid.cs
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; }
}
}
23 changes: 23 additions & 0 deletions PureCodeFirst/Charachters/Episode.cs
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
}
}
25 changes: 25 additions & 0 deletions PureCodeFirst/Charachters/GetFriendsResolverAttribute.cs
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());
});
}
}
}
51 changes: 51 additions & 0 deletions PureCodeFirst/Charachters/Human.cs
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; }
}
}
40 changes: 40 additions & 0 deletions PureCodeFirst/Charachters/ICharacter.cs
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; }
}
}
9 changes: 9 additions & 0 deletions PureCodeFirst/Charachters/ISearchResult.cs
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
{
}
}
31 changes: 31 additions & 0 deletions PureCodeFirst/Charachters/Starship.cs
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; }
}
}
11 changes: 11 additions & 0 deletions PureCodeFirst/Charachters/Unit.cs
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
}
}
Loading

0 comments on commit 09217a0

Please sign in to comment.