Skip to content

Commit

Permalink
feat: stitch all the queries and mutation available into composite qu…
Browse files Browse the repository at this point in the history
…eries and mutations
  • Loading branch information
psyphore committed Sep 23, 2019
1 parent 21254a3 commit 378e41c
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 18 deletions.
39 changes: 39 additions & 0 deletions GraphQLCore/BuildingHandlers/BuildingMutation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using BusinessServices.Building;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.DTOs;
using Models.GraphQLTypes.Building;
using Models.GraphQLTypes.Person;

namespace Models.Types
{
public class BuildingMutation : ObjectGraphType, IGraphMutator
{
public BuildingMutation(IBuildingService service)
{
Name = "BuildingMutation";
Description = "Actions to create, update and delete a Building";

Field<BuildingType>(
"CreateBuilding",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<BuildingInputType>> { Name = "building" }),
resolve: ctx => service.Add(ctx.GetArgument<BuildingModel>("building")),
description: "Create a new Building"
);

Field<BuildingType>(
"UpdateBuilding",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<BuildingInputType>> { Name = "building" }),
resolve: ctx => service.Update(ctx.GetArgument<BuildingModel>("building")),
description: "Update a Building"
);

Field<BuildingType>(
"RemoveBuilding",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" }),
resolve: ctx => service.Delete(ctx.GetArgument<string>("id")),
description: "Delete a Building"
);
}
}
}
29 changes: 29 additions & 0 deletions GraphQLCore/BuildingHandlers/BuildingQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BusinessServices.Building;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.GraphQLTypes.Building;

namespace Models.Types
{
public class BuildingQuery : ObjectGraphType, IGraphQueryMarker
{
public BuildingQuery(IBuildingService service)
{
Name = "BuildingQuery";
Description = "Actions fetch a Building";

Field<BuildingType>(
"Building",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" }),
resolve: ctx => service.Get(ctx.GetArgument<string>("id")),
description: "Fetch a Building by their Identifier"
);

Field<ListGraphType<BuildingType>>(
"Buildings",
resolve: ctx => service.GetAll(),
description: "Fetch all Buildings"
);
}
}
}
5 changes: 3 additions & 2 deletions GraphQLCore/MainSchema.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GraphQL;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.Types;

namespace GraphQLCore
Expand All @@ -8,8 +9,8 @@ public class MainSchema : Schema
{
public MainSchema(IDependencyResolver resolver) : base(resolver)
{
Query = resolver.Resolve<PersonQuery>();
Mutation = resolver.Resolve<PersonMutation>();
Query = resolver.Resolve<CompositeQueries>();
Mutation = resolver.Resolve<CompositeMutators>();
}
}
}
9 changes: 5 additions & 4 deletions GraphQLCore/PersonHandlers/PersonMutation.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using BusinessServices.Person;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.DTOs;
using Models.GraphQLTypes.Person;

namespace Models.Types
{
public class PersonMutation : ObjectGraphType
public class PersonMutation : ObjectGraphType, IGraphMutator
{
public PersonMutation(IPersonService service)
{
Name = "PersonMutation";
Description = "Actions to create, update and delete a person";

Field<PersonType>(
"Create",
"CreatePerson",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<PersonInputType>> { Name = "person" }),
resolve: ctx => service.Add(ctx.GetArgument<PersonModel>("person")),
description: "Create a new person"
);

Field<PersonType>(
"Update",
"UpdatePerson",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<PersonInputType>> { Name = "person" }),
resolve: ctx => service.Update(ctx.GetArgument<PersonModel>("person")),
description: "Update a person"
);

Field<PersonType>(
"Remove",
"RemovePerson",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" }),
resolve: ctx => service.Delete(ctx.GetArgument<string>("id")),
description: "Delete a person"
Expand Down
3 changes: 2 additions & 1 deletion GraphQLCore/PersonHandlers/PersonQuery.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using BusinessServices.Person;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.GraphQLTypes.Person;

namespace Models.Types
{
public class PersonQuery : ObjectGraphType
public class PersonQuery : ObjectGraphType, IGraphQueryMarker
{
public PersonQuery(IPersonService service)
{
Expand Down
46 changes: 46 additions & 0 deletions GraphQLCore/ProductHandlers/ProductMutation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using BusinessServices.Product;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.GraphQLTypes.Product;

namespace Models.Types
{
public class ProductMutation : ObjectGraphType, IGraphMutator
{
public ProductMutation(IProductService service)
{
Name = "ProductMutation";
Description = "Actions to create, update and delete a Product";

Field<ProductType>(
"CreateProduct",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<ProductInputType>> { Name = "Product" }),
resolve: ctx => {
// service.Add(ctx.GetArgument<ProductModel>("Product"))
return null;
},
description: "Create a new Product"
);

Field<ProductType>(
"UpdateProduct",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<ProductInputType>> { Name = "Product" }),
resolve: ctx => {
// service.Update(ctx.GetArgument<ProductModel>("Product"));
return null;
},
description: "Update a Product"
);

Field<ProductType>(
"RemoveProduct",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" }),
resolve: ctx => {
// service.Delete(ctx.GetArgument<string>("id"));
return null;
},
description: "Delete a Product"
);
}
}
}
29 changes: 29 additions & 0 deletions GraphQLCore/ProductHandlers/ProductQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BusinessServices.Product;
using GraphQL.Types;
using GraphQLCore.Unions;
using Models.GraphQLTypes.Product;

namespace Models.Types
{
public class ProductQuery : ObjectGraphType, IGraphQueryMarker
{
public ProductQuery(IProductService service)
{
Name = "ProductnQuery";
Description = "Actions fetch a Product";

Field<ProductType>(
"Product",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" }),
resolve: ctx => service.Get(ctx.GetArgument<string>("id")),
description: "Fetch a Product by their Identifier"
);

Field<ListGraphType<ProductType>>(
"Products",
resolve: ctx => service.GetAll(),
description: "Fetch all Products"
);
}
}
}
21 changes: 21 additions & 0 deletions GraphQLCore/Unions/CompositeMutators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GraphQL.Types;
using System.Collections.Generic;

namespace GraphQLCore.Unions
{
public class CompositeMutators : ObjectGraphType
{
public CompositeMutators(IEnumerable<IGraphMutator> mutators)
{
Name = "Mutators";
Description = "Actions with side-effects";

foreach (var query in mutators)
{
var q = query as ObjectGraphType;
foreach (var field in q.Fields)
AddField(field);
}
}
}
}
21 changes: 21 additions & 0 deletions GraphQLCore/Unions/CompositeQueries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GraphQL.Types;
using System.Collections.Generic;

namespace GraphQLCore.Unions
{
public class CompositeQueries: ObjectGraphType
{
public CompositeQueries(IEnumerable<IGraphQueryMarker> queries)
{
Name = "Queries";
Description = "Read only fetch actions";

foreach (var query in queries)
{
var q = query as ObjectGraphType;
foreach(var field in q.Fields)
AddField(field);
}
}
}
}
4 changes: 4 additions & 0 deletions GraphQLCore/Unions/IGraphMutator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace GraphQLCore.Unions
{
public interface IGraphMutator { }
}
4 changes: 4 additions & 0 deletions GraphQLCore/Unions/IGraphQueryMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace GraphQLCore.Unions
{
public interface IGraphQueryMarker { }
}
15 changes: 15 additions & 0 deletions GraphQLCore/Unions/Mutations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using Models.Types;

namespace GraphQLCore.Unions
{
public class Mutations: UnionGraphType
{
public Mutations()
{
Type<PersonMutation>();
Type<ProductMutation>();
Type<BuildingMutation>();
}
}
}
15 changes: 15 additions & 0 deletions GraphQLCore/Unions/Queries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using Models.Types;

namespace GraphQLCore.Unions
{
public class Queries : UnionGraphType
{
public Queries()
{
Type<PersonQuery>();
Type<ProductQuery>();
Type<BuildingQuery>();
}
}
}
27 changes: 25 additions & 2 deletions IoC/RegisterGraphQLHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using GraphQL.Types;
using GraphQL.Validation;
using GraphQLCore;
using GraphQLCore.Unions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Models.GraphQLTypes.Building;
using Models.GraphQLTypes.Person;
using Models.GraphQLTypes.Product;
using Models.Types;
using System.Threading.Tasks;

Expand All @@ -20,10 +23,30 @@ public static void ConfigureGraphQLServices(this IServiceCollection services)
services.AddSingleton<IDocumentWriter, DocumentWriter>();

// Person
services.AddSingleton<PersonQuery>();
services.AddSingleton<PersonMutation>();
services.AddSingleton<PersonType>();
services.AddSingleton<PersonInputType>();
services.AddSingleton<IGraphQueryMarker, PersonQuery>();
services.AddSingleton<IGraphMutator, PersonMutation>();

// Product
services.AddSingleton<ProductInputType>();
services.AddSingleton<ProductType>();
services.AddSingleton<IGraphMutator, ProductMutation>();
services.AddSingleton<IGraphQueryMarker, ProductQuery>();

// Building
services.AddSingleton<BuildingInputType>();
services.AddSingleton<BuildingType>();
services.AddSingleton<IGraphMutator, BuildingMutation>();
services.AddSingleton<IGraphQueryMarker, BuildingQuery>();

// Unions
services.AddSingleton<Mutations>();
services.AddSingleton<Queries>();

// Composites
services.AddSingleton<CompositeQueries>();
services.AddSingleton<CompositeMutators>();

// GraphQL Schema
services.AddSingleton<ISchema, MainSchema>();
Expand Down
14 changes: 14 additions & 0 deletions Models/GraphQLTypes/Building/BuildingInputType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GraphQL.Types;

namespace Models.GraphQLTypes.Person
{
public class BuildingInputType : InputObjectGraphType
{
public BuildingInputType()
{
Name = "BuildingInput";
Field<NonNullGraphType<StringGraphType>>("name");
Field<NonNullGraphType<StringGraphType>>("address");
}
}
}
Loading

0 comments on commit 378e41c

Please sign in to comment.