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

Implement LessonSave mutation #31

Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions TeacherWorkout.Api/GraphQL/Inputs/LessonSaveInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GraphQL.Types;

namespace TeacherWorkout.Api.GraphQL.Inputs
{
public class LessonSaveInput : InputObjectGraphType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposal:

Rename this to a LessonSaveInputType
Define a LessonSaveInput
Inherit LessonSaveInputType from InputObjectGraphType
Define the fields using the strongly typed methods to avoid typos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this correctly. Which strongly typed methods are you referring to?

Also, wouldn't it be confusing if a file was named ..InputType? Would it be both an Input & a Type? 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made alex-damian-negru#1 to show what i was talknig about.

{
public LessonSaveInput()
{
Name = "LessonSaveInput";
Field<IdGraphType>("lessonId");
}
}
}
42 changes: 42 additions & 0 deletions TeacherWorkout.Api/GraphQL/Resolvers/LessonSaveResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using TeacherWorkout.Api.Models;

namespace TeacherWorkout.Api.GraphQL.Resolvers

{
public static class LessonSaveResolver
{
public static LessonSavePayload Resolve(LessonSave lessonSave)
{
return new() {Lesson = MockData(lessonSave)};
}

private static Lesson MockData(LessonSave lessonSave)
{
return new()
{
Id = lessonSave.LessonId,
Title = "Lorem Ipsum",
Thumbnail = new Image
{
Description = "For Challenged People",
Url = "https://example.com"
},
Theme = new Theme
{
Id = "1",
Title = "Lorem Ipsum",
Thumbnail = new Image
{
Description = "For Challenged People",
Url = "https://example.com"
}
},
Duration = new Duration
{
Value = 45,
Unit = DurationUnit.Minutes
}
};
}
}
}
28 changes: 28 additions & 0 deletions TeacherWorkout.Api/GraphQL/TeacherWorkoutMutation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using GraphQL;
using GraphQL.Types;
using TeacherWorkout.Api.GraphQL.Resolvers;
using TeacherWorkout.Api.GraphQL.Types;
using TeacherWorkout.Api.Models;
using LessonSaveInput = TeacherWorkout.Api.GraphQL.Inputs.LessonSaveInput;

namespace TeacherWorkout.Api.GraphQL
{
public class TeacherWorkoutMutation : ObjectGraphType<object>
{
public TeacherWorkoutMutation(LessonSaveInput input)
{
Name = "Mutation";

Field<LessonSavePayloadType>(
"lessonSave",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<LessonSaveInput>> {Name = "input"}
),
resolve: context =>
{
var lessonSave = context.GetArgument<LessonSave>("input");
return LessonSaveResolver.Resolve(lessonSave);
});
}
}
}
1 change: 1 addition & 0 deletions TeacherWorkout.Api/GraphQL/TeacherWorkoutSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public TeacherWorkoutSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.GetRequiredService<TeacherWorkoutQuery>();
Mutation = provider.GetRequiredService<TeacherWorkoutMutation>();
AddTypeMappings();

RegisterTypeMapping(typeof(ILessonStep), typeof(LessonStepInterface));
Expand Down
15 changes: 15 additions & 0 deletions TeacherWorkout.Api/GraphQL/Types/LessonSavePayloadType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GraphQL.Types;
using TeacherWorkout.Api.Models;

namespace TeacherWorkout.Api.GraphQL.Types
{
public class LessonSavePayloadType : ObjectGraphType<LessonSavePayload>
{
public LessonSavePayloadType()
{
Name = "LessonSavePayload";

Field(x => x.Lesson).Description("The newly created lesson.");
}
}
}
14 changes: 14 additions & 0 deletions TeacherWorkout.Api/Models/LessonSave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using TeacherWorkout.Api.GraphQL.Types;

namespace TeacherWorkout.Api.Models
{
public class LessonSave
{
private string _lessonId;
public dynamic LessonId
{
get => _lessonId;
set => _lessonId = value.ToString();
}
}
}
9 changes: 9 additions & 0 deletions TeacherWorkout.Api/Models/LessonSavePayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TeacherWorkout.Api.GraphQL.Types;

namespace TeacherWorkout.Api.Models
{
public class LessonSavePayload
{
public Lesson Lesson { get; set; }
}
}
3 changes: 3 additions & 0 deletions TeacherWorkout.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TeacherWorkout.Api.GraphQL;
using TeacherWorkout.Api.GraphQL.Inputs;

namespace TeacherWorkout.Api
{
Expand Down Expand Up @@ -36,6 +37,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();

services.AddSingleton<TeacherWorkoutQuery>();
services.AddSingleton<TeacherWorkoutMutation>();
services.AddSingleton<LessonSaveInput>();
alex-damian-negru marked this conversation as resolved.
Show resolved Hide resolved
services.AddSingleton<ISchema, TeacherWorkoutSchema>();
AddGraphTypes(services);

Expand Down
1 change: 1 addition & 0 deletions TeacherWorkout.Api/TeacherWorkout.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<Folder Include="Controllers" />
<Folder Include="GraphQL\Mutations" />
</ItemGroup>

</Project>