Skip to content

Commit

Permalink
Use file-scoped namespaces for StarWars project (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Feb 3, 2023
1 parent 1a06944 commit 48f7a1f
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 212 deletions.
15 changes: 7 additions & 8 deletions src/StarWars/HumanInputType.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using GraphQL.Types;
using StarWars.Types;

namespace StarWars
namespace StarWars;

public class HumanInputType : InputObjectGraphType<Human>
{
public class HumanInputType : InputObjectGraphType<Human>
public HumanInputType()
{
public HumanInputType()
{
Name = "HumanInput";
Field(x => x.Name);
Field(x => x.HomePlanet, nullable: true);
}
Name = "HumanInput";
Field(x => x.Name);
Field(x => x.HomePlanet, nullable: true);
}
}
15 changes: 7 additions & 8 deletions src/StarWars/InputValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using GraphQL.Validation;
using GraphQLParser.AST;

namespace StarWars
namespace StarWars;

public class InputValidationRule : IValidationRule
{
public class InputValidationRule : IValidationRule
public ValueTask<INodeVisitor> ValidateAsync(ValidationContext context)
{
public ValueTask<INodeVisitor> ValidateAsync(ValidationContext context)
{
return new ValueTask<INodeVisitor>(new MatchingNodeVisitor<GraphQLField>(
(field, context2) => { },
(field, context2) => { }));
}
return new ValueTask<INodeVisitor>(new MatchingNodeVisitor<GraphQLField>(
(field, context2) => { },
(field, context2) => { }));
}
}
1 change: 1 addition & 0 deletions src/StarWars/StarWars.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<Version>1.0.0</Version>
<LangVersion>latest</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Description>Taken from https://github.com/graphql-dotnet/graphql-dotnet/tree/master/src/GraphQL.StarWars</Description>
Expand Down
129 changes: 64 additions & 65 deletions src/StarWars/StarWarsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,81 @@
using System.Threading.Tasks;
using StarWars.Types;

namespace StarWars
namespace StarWars;

public class StarWarsData
{
public class StarWarsData
{
private readonly List<Human> _humans = new List<Human>();
private readonly List<Droid> _droids = new List<Droid>();
private readonly List<Human> _humans = new List<Human>();
private readonly List<Droid> _droids = new List<Droid>();

public StarWarsData()
public StarWarsData()
{
_humans.Add(new Human
{
_humans.Add(new Human
{
Id = "1",
Name = "Luke",
Friends = new[] { "3", "4" },
AppearsIn = new[] { 4, 5, 6 },
HomePlanet = "Tatooine"
});
_humans.Add(new Human
{
Id = "2",
Name = "Vader",
AppearsIn = new[] { 4, 5, 6 },
HomePlanet = "Tatooine"
});

_droids.Add(new Droid
{
Id = "3",
Name = "R2-D2",
Friends = new[] { "1", "4" },
AppearsIn = new[] { 4, 5, 6 },
PrimaryFunction = "Astromech"
});
_droids.Add(new Droid
{
Id = "4",
Name = "C-3PO",
AppearsIn = new[] { 4, 5, 6 },
PrimaryFunction = "Protocol"
});
}

public IEnumerable<StarWarsCharacter> GetFriends(StarWarsCharacter character)
Id = "1",
Name = "Luke",
Friends = new[] { "3", "4" },
AppearsIn = new[] { 4, 5, 6 },
HomePlanet = "Tatooine"
});
_humans.Add(new Human
{
if (character == null)
{
return null;
}
Id = "2",
Name = "Vader",
AppearsIn = new[] { 4, 5, 6 },
HomePlanet = "Tatooine"
});

var friends = new List<StarWarsCharacter>();
var lookup = character.Friends;
if (lookup != null)
{
foreach (var h in _humans.Where(h => lookup.Contains(h.Id)))
friends.Add(h);
foreach (var d in _droids.Where(d => lookup.Contains(d.Id)))
friends.Add(d);
}
return friends;
}

public Task<Human> GetHumanByIdAsync(string id)
_droids.Add(new Droid
{
return Task.FromResult(_humans.FirstOrDefault(h => h.Id == id));
}
Id = "3",
Name = "R2-D2",
Friends = new[] { "1", "4" },
AppearsIn = new[] { 4, 5, 6 },
PrimaryFunction = "Astromech"
});
_droids.Add(new Droid
{
Id = "4",
Name = "C-3PO",
AppearsIn = new[] { 4, 5, 6 },
PrimaryFunction = "Protocol"
});
}

public Task<Droid> GetDroidByIdAsync(string id)
public IEnumerable<StarWarsCharacter> GetFriends(StarWarsCharacter character)
{
if (character == null)
{
return Task.FromResult(_droids.FirstOrDefault(h => h.Id == id));
return null;
}

public Human AddHuman(Human human)
var friends = new List<StarWarsCharacter>();
var lookup = character.Friends;
if (lookup != null)
{
human.Id = Guid.NewGuid().ToString();
_humans.Add(human);
return human;
foreach (var h in _humans.Where(h => lookup.Contains(h.Id)))
friends.Add(h);
foreach (var d in _droids.Where(d => lookup.Contains(d.Id)))
friends.Add(d);
}
return friends;
}

public Task<Human> GetHumanByIdAsync(string id)
{
return Task.FromResult(_humans.FirstOrDefault(h => h.Id == id));
}

public Task<Droid> GetDroidByIdAsync(string id)
{
return Task.FromResult(_droids.FirstOrDefault(h => h.Id == id));
}

public Human AddHuman(Human human)
{
human.Id = Guid.NewGuid().ToString();
_humans.Add(human);
return human;
}
}
53 changes: 26 additions & 27 deletions src/StarWars/StarWarsMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@
using GraphQL.Types;
using StarWars.Types;

namespace StarWars
namespace StarWars;

/// <example>
/// This is an example JSON request for a mutation
/// {
/// "query": "mutation ($human:HumanInput!){ createHuman(human: $human) { id name } }",
/// "variables": {
/// "human": {
/// "name": "Boba Fett"
/// }
/// }
/// }
/// </example>
public class StarWarsMutation : ObjectGraphType
{
/// <example>
/// This is an example JSON request for a mutation
/// {
/// "query": "mutation ($human:HumanInput!){ createHuman(human: $human) { id name } }",
/// "variables": {
/// "human": {
/// "name": "Boba Fett"
/// }
/// }
/// }
/// </example>
public class StarWarsMutation : ObjectGraphType
public StarWarsMutation(StarWarsData data)
{
public StarWarsMutation(StarWarsData data)
{
Name = "Mutation";
Name = "Mutation";

Field<HumanType>(
"createHuman",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<HumanInputType>> { Name = "human" }
),
resolve: context =>
{
var human = context.GetArgument<Human>("human");
return data.AddHuman(human);
});
}
Field<HumanType>(
"createHuman",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<HumanInputType>> { Name = "human" }
),
resolve: context =>
{
var human = context.GetArgument<Human>("human");
return data.AddHuman(human);
});
}
}
43 changes: 21 additions & 22 deletions src/StarWars/StarWarsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
using GraphQL.Types;
using StarWars.Types;

namespace StarWars
namespace StarWars;

public class StarWarsQuery : ObjectGraphType<object>
{
public class StarWarsQuery : ObjectGraphType<object>
public StarWarsQuery(StarWarsData data)
{
public StarWarsQuery(StarWarsData data)
{
Name = "Query";
Name = "Query";

FieldAsync<CharacterInterface>("hero", resolve: async context => await data.GetDroidByIdAsync("3"));
FieldAsync<HumanType>(
"human",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the human" }
),
resolve: async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id"))
);
FieldAsync<CharacterInterface>("hero", resolve: async context => await data.GetDroidByIdAsync("3"));
FieldAsync<HumanType>(
"human",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the human" }
),
resolve: async context => await data.GetHumanByIdAsync(context.GetArgument<string>("id"))
);

Func<IResolveFieldContext, string, Task<Droid>> func = (context, id) => data.GetDroidByIdAsync(id);
Func<IResolveFieldContext, string, Task<Droid>> func = (context, id) => data.GetDroidByIdAsync(id);

FieldDelegate<DroidType>(
"droid",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the droid" }
),
resolve: func
);
}
FieldDelegate<DroidType>(
"droid",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the droid" }
),
resolve: func
);
}
}
17 changes: 8 additions & 9 deletions src/StarWars/StarWarsSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
using GraphQL.Instrumentation;
using GraphQL.Types;

namespace StarWars
namespace StarWars;

public class StarWarsSchema : Schema
{
public class StarWarsSchema : Schema
public StarWarsSchema(IServiceProvider provider)
: base(provider)
{
public StarWarsSchema(IServiceProvider provider)
: base(provider)
{
Query = (StarWarsQuery)provider.GetService(typeof(StarWarsQuery)) ?? throw new InvalidOperationException();
Mutation = (StarWarsMutation)provider.GetService(typeof(StarWarsMutation)) ?? throw new InvalidOperationException();
Query = (StarWarsQuery)provider.GetService(typeof(StarWarsQuery)) ?? throw new InvalidOperationException();
Mutation = (StarWarsMutation)provider.GetService(typeof(StarWarsMutation)) ?? throw new InvalidOperationException();

FieldMiddleware.Use(new InstrumentFieldsMiddleware());
}
FieldMiddleware.Use(new InstrumentFieldsMiddleware());
}
}
19 changes: 9 additions & 10 deletions src/StarWars/Types/CharacterInterface.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using GraphQL.Types;

namespace StarWars.Types
namespace StarWars.Types;

public class CharacterInterface : InterfaceGraphType<StarWarsCharacter>
{
public class CharacterInterface : InterfaceGraphType<StarWarsCharacter>
public CharacterInterface()
{
public CharacterInterface()
{
Name = "Character";
Name = "Character";

Field(d => d.Id).Description("The id of the character.");
Field(d => d.Name, nullable: true).Description("The name of the character.");
Field(d => d.Id).Description("The id of the character.");
Field(d => d.Name, nullable: true).Description("The name of the character.");

Field<ListGraphType<CharacterInterface>>("friends");
Field<ListGraphType<EpisodeEnum>>("appearsIn", "Which movie they appear in.");
}
Field<ListGraphType<CharacterInterface>>("friends");
Field<ListGraphType<EpisodeEnum>>("appearsIn", "Which movie they appear in.");
}
}
Loading

0 comments on commit 48f7a1f

Please sign in to comment.