-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,657 additions
and
1,794 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphQL.DataLoader; | ||
using Microsoft.EntityFrameworkCore; | ||
using Sample.DbModels; | ||
|
||
namespace Sample.DataLoaders | ||
namespace Sample.DataLoaders; | ||
|
||
public class PersonDataLoader : DataLoaderBase<int, Person?> | ||
{ | ||
public class PersonDataLoader : DataLoaderBase<int, Person> | ||
private readonly TodoDbContext _db; | ||
|
||
public PersonDataLoader(TodoDbContext db) | ||
{ | ||
private TodoDbContext _db; | ||
public PersonDataLoader(TodoDbContext db) => _db = db; | ||
_db = db; | ||
} | ||
|
||
protected override async Task FetchAsync(IEnumerable<DataLoaderPair<int, Person>> list, CancellationToken cancellationToken) | ||
{ | ||
var ids = list.Select(x => x.Key); | ||
var people = await _db.Set<Person>().Where(x => ids.Contains(x.Id)).ToDictionaryAsync(x => x.Id, cancellationToken); | ||
foreach (var value in list) { | ||
people.TryGetValue(value.Key, out var person); | ||
value.SetResult(person); | ||
} | ||
protected override async Task FetchAsync(IEnumerable<DataLoaderPair<int, Person?>> list, CancellationToken cancellationToken) | ||
{ | ||
var ids = list.Select(x => x.Key); | ||
var people = await _db.Set<Person>().Where(x => ids.Contains(x.Id)).ToDictionaryAsync(x => x.Id, cancellationToken); | ||
foreach (var value in list) { | ||
people.TryGetValue(value.Key, out var person); | ||
value.SetResult(person); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
namespace Sample.DbModels; | ||
|
||
namespace Sample.DbModels | ||
public class Person | ||
{ | ||
public class Person | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
public int Id { get; set; } | ||
public string Name { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample.DbModels | ||
namespace Sample.DbModels; | ||
|
||
public class Todo | ||
{ | ||
public class Todo | ||
{ | ||
public int Id { get; set; } | ||
public string Title { get; set; } | ||
public string Notes { get; set; } | ||
public bool Completed { get; set; } | ||
public DateTime? CompletionDate { get; set; } | ||
public int? CompletedByPersonId { get; set; } | ||
[ForeignKey("CompletedByPersonId")] | ||
public Person CompletedBy { get; set; } | ||
} | ||
public int Id { get; set; } | ||
public string Title { get; set; } = null!; | ||
public string Notes { get; set; } = null!; | ||
public bool Completed { get; set; } | ||
public DateTime? CompletionDate { get; set; } | ||
public int? CompletedByPersonId { get; set; } | ||
[ForeignKey("CompletedByPersonId")] | ||
public Person CompletedBy { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
namespace Sample; | ||
|
||
namespace Sample | ||
/// <summary> | ||
/// Indicates that GetClrTypeMappings should | ||
/// skip this class when scanning an assembly for CLR type mappings. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
public class DoNotMapClrTypeAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Indicates that GetClrTypeMappings should | ||
/// skip this class when scanning an assembly for CLR type mappings. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
public class DoNotMapClrTypeAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using GraphQL.Types; | ||
using GraphQL; | ||
using GraphQL.DI; | ||
using Sample.DbModels; | ||
using Microsoft.EntityFrameworkCore; | ||
using GraphQL; | ||
using System.Threading; | ||
using Sample.DbModels; | ||
|
||
namespace Sample.GraphTypes; | ||
|
||
namespace Sample.GraphTypes | ||
public class MutationType : DIObjectGraphType<Mutation> { } | ||
public class Mutation : DIObjectGraphBase<object> | ||
{ | ||
public class MutationType : DIObjectGraphType<Mutation> { } | ||
public class Mutation : DIObjectGraphBase<object> | ||
private readonly TodoDbContext _db; | ||
|
||
public Mutation(TodoDbContext db) | ||
{ | ||
public readonly TodoDbContext _db; | ||
public Mutation(TodoDbContext db) => _db = db; | ||
_db = db; | ||
} | ||
|
||
public async Task<Todo> AddTodoAsync([Required] string title, string notes, CancellationToken cancellationToken) | ||
{ | ||
var todo = new Todo { | ||
Title = title, | ||
Notes = notes, | ||
}; | ||
_db.Add(todo); | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return todo; | ||
} | ||
public async Task<Todo> AddTodoAsync([Required] string title, string notes, CancellationToken cancellationToken) | ||
{ | ||
var todo = new Todo { | ||
Title = title, | ||
Notes = notes, | ||
}; | ||
_db.Add(todo); | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return todo; | ||
} | ||
|
||
public async Task<bool> DeleteTodoAsync(int id, CancellationToken cancellationToken) | ||
{ | ||
var todo = await _db.Set<Todo>().Where(x => x.Id == id).SingleOrDefaultAsync(); | ||
if (todo == null) | ||
return false; | ||
_db.Set<Todo>().Remove(todo); | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return true; | ||
} | ||
public async Task<bool> DeleteTodoAsync(int id, CancellationToken cancellationToken) | ||
{ | ||
var todo = await _db.Set<Todo>().Where(x => x.Id == id).SingleOrDefaultAsync(cancellationToken); | ||
if (todo == null) | ||
return false; | ||
_db.Set<Todo>().Remove(todo); | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return true; | ||
} | ||
|
||
public async Task<Todo> SetCompleteAsync(int id, int completedByPersonId, CancellationToken cancellationToken) | ||
{ | ||
var todo = await _db.Set<Todo>().Where(x => x.Id == id).SingleOrDefaultAsync(cancellationToken); | ||
if (todo == null) | ||
return null; | ||
if (todo.Completed) | ||
throw new ExecutionError($"Task id {id} has already been completed"); | ||
todo.Completed = true; | ||
todo.CompletedByPersonId = completedByPersonId; | ||
todo.CompletionDate = DateTime.Now; | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return todo; | ||
} | ||
public async Task<Todo?> SetCompleteAsync(int id, int completedByPersonId, CancellationToken cancellationToken) | ||
{ | ||
var todo = await _db.Set<Todo>().Where(x => x.Id == id).SingleOrDefaultAsync(cancellationToken); | ||
if (todo == null) | ||
return null; | ||
if (todo.Completed) | ||
throw new ExecutionError($"Task id {id} has already been completed"); | ||
todo.Completed = true; | ||
todo.CompletedByPersonId = completedByPersonId; | ||
todo.CompletionDate = DateTime.Now; | ||
await _db.SaveChangesAsync(cancellationToken); | ||
return todo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using GraphQL.Types; | ||
using Sample.DbModels; | ||
|
||
namespace Sample.GraphTypes | ||
namespace Sample.GraphTypes; | ||
|
||
public class PersonType : ObjectGraphType<Person> | ||
{ | ||
public class PersonType : ObjectGraphType<Person> | ||
public PersonType() | ||
{ | ||
public PersonType() | ||
{ | ||
Field(x => x.Id); | ||
Field(x => x.Name); | ||
} | ||
Field(x => x.Id); | ||
Field(x => x.Name); | ||
} | ||
} |
Oops, something went wrong.