Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GraphQL endpoint to interact with the application layer #52

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/Types/MovementType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL.Types
{
public class MovementType : ObjectGraphType<Movement>
{
public MovementType()
{
Field(x => x.Id).Description("The ID of the Movement.");
Field(x => x.Name).Description("The name of the Movement.");
Field(x => x.Description, nullable: true).Description("The description of the Movement.");
}
}
}
17 changes: 17 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/Types/WorkoutMovementType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL.Types
{
public class WorkoutMovementType : ObjectGraphType<WorkoutMovement>
{
public WorkoutMovementType()
{
Field(x => x.Id).Description("The ID of the Workout Movement.");
Field(x => x.Movement).Description("The movement of the Workout Movement.");
Field(x => x.Reps, nullable: true).Description("The reps of the Workout Movement.");
Field(x => x.Weight, nullable: true).Description("The weight of the Workout Movement.");
Field(x => x.Distance, nullable: true).Description("The distance of the Workout Movement.");
}
}
}
16 changes: 16 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/Types/WorkoutRecordType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL.Types
{
public class WorkoutRecordType : ObjectGraphType<WorkoutRecord>
{
public WorkoutRecordType()
{
Field(x => x.Id).Description("The ID of the WorkoutRecord.");
Field(x => x.Workout).Description("The workout associated with the record.");
Field(x => x.Time).Description("The time taken to complete the workout.");
Field(x => x.RepsCount).Description("The number of reps completed in the workout.");
}
}
}
21 changes: 21 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/Types/WorkoutType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL.Types
{
public class WorkoutType : ObjectGraphType<Workout>
{
public WorkoutType()
{
Field(x => x.Id).Description("The ID of the Workout.");
Field(x => x.Name).Description("The name of the Workout.");
Field(x => x.Description, nullable: true).Description("The description of the Workout.");
Field<ListGraphType<WorkoutMovementType>>(
"movements",
resolve: context => context.Source.Movements
);
Field(x => x.TimeCap).Description("The time cap of the Workout.");
Field(x => x.RoundsCount).Description("The number of rounds in the Workout.");
}
}
}
30 changes: 30 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/WorkoutRecordsMutation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using GraphQL.Types;
using WorkoutRecords.Api.GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL
{
public class WorkoutRecordsMutation : ObjectGraphType
{
public WorkoutRecordsMutation()
{
Field<WorkoutRecordType>(
"createWorkoutRecord",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<WorkoutRecordInputType>> { Name = "workoutRecord" }
),
resolve: context =>
{
var workoutRecord = context.GetArgument<WorkoutRecord>("workoutRecord");
return CreateWorkoutRecord(workoutRecord);
}
);
}

private WorkoutRecord CreateWorkoutRecord(WorkoutRecord workoutRecord)
{
// Placeholder for creating a new workout record
return workoutRecord;
}
}
}
23 changes: 23 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/WorkoutRecordsQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using GraphQL.Types;
using WorkoutRecords.Api.GraphQL.Types;
using WorkoutRecords.Domain.DDD;

namespace WorkoutRecords.Api.GraphQL
{
public class WorkoutRecordsQuery : ObjectGraphType
{
public WorkoutRecordsQuery()
{
Field<ListGraphType<WorkoutRecordType>>(
"workoutRecords",
resolve: context => GetWorkoutRecords()
);
}

private List<WorkoutRecord> GetWorkoutRecords()
{
// Placeholder for fetching workout records data
return new List<WorkoutRecord>();
}
}
}
15 changes: 15 additions & 0 deletions src/WorkoutRecords.Api/GraphQL/WorkoutRecordsSchema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using WorkoutRecords.Api.GraphQL.Types;

namespace WorkoutRecords.Api.GraphQL
{
public class WorkoutRecordsSchema : Schema
{
public WorkoutRecordsSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.GetRequiredService<WorkoutRecordsQuery>();
Mutation = provider.GetRequiredService<WorkoutRecordsMutation>();
}
}
}
50 changes: 50 additions & 0 deletions src/WorkoutRecords.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using GraphQL;
using GraphQL.Server;
using GraphQL.Types;
using WorkoutRecords.Api.GraphQL;

namespace WorkoutRecords.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddGraphQL(options =>
{
options.EnableMetrics = false;
})
.AddSystemTextJson()
.AddGraphTypes(ServiceLifetime.Scoped);

services.AddScoped<ISchema, WorkoutRecordsSchema>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

app.UseGraphQL<ISchema>();
app.UseGraphQLPlayground();
}
}
}
Loading