-
-
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
Samuel Abraham
committed
Jul 30, 2023
1 parent
1dbcc2a
commit 8efaecc
Showing
45 changed files
with
703 additions
and
696 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2021 Samuel Abraham | ||
|
||
using System.Linq; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using TypeCache.GraphQL.Attributes; | ||
using TypeCache.GraphQL.Extensions; | ||
using TypeCache.GraphQL.Types; | ||
using static System.FormattableString; | ||
|
||
namespace TypeCache.GraphQL.Data; | ||
|
||
public class Connection<T> | ||
where T : notnull | ||
{ | ||
public Connection() | ||
{ | ||
} | ||
|
||
public Connection(uint offset, T[] items) | ||
{ | ||
offset += 1; | ||
this.Items = items; | ||
this.Edges = items.Select((row, i) => new Edge<T>((int)offset + i, row)).ToArray(); | ||
} | ||
|
||
[GraphQLDescription("The total number of records available. Returns `null` if the total number is unknown.")] | ||
public int? TotalCount { get; init; } | ||
|
||
[GraphQLDescription("Pagination information for this result data set.")] | ||
public PageInfo? PageInfo { get; init; } | ||
|
||
[GraphQLDescription("The result data set, stored as a list of edges containing a node (the data) and a cursor (a unique identifier for the data).")] | ||
public Edge<T>[]? Edges { get; init; } | ||
|
||
[GraphQLDescription("The result data set.")] | ||
public T[]? Items { get; init; } | ||
|
||
public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphType) | ||
{ | ||
var graphType = new ObjectGraphType | ||
{ | ||
Name = Invariant($"{name}{nameof(Connection<T>)}"), | ||
Description = typeof(Connection<T>).GraphQLDescription() | ||
}; | ||
var edgeGraphType = Edge<T>.CreateGraphType(name, dataGraphType); | ||
|
||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Connection<T>.Edges), | ||
ResolvedType = new ListGraphType(new NonNullGraphType(edgeGraphType)), | ||
Resolver = new FuncFieldResolver<Connection<T>, Edge<T>[]?>(context => context.Source.Edges) | ||
}); | ||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Connection<T>.Items), | ||
ResolvedType = new ListGraphType(new NonNullGraphType(dataGraphType)), | ||
Resolver = new FuncFieldResolver<Connection<T>, T[]?>(context => context.Source.Items) | ||
}); | ||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Connection<T>.PageInfo), | ||
ResolvedType = new GraphQLObjectType<PageInfo>(Invariant($"{name}{nameof(PageInfo)}")), | ||
//Type = typeof(GraphQLObjectType<PageInfo>), | ||
Resolver = new FuncFieldResolver<Connection<T>, PageInfo?>(context => context.Source.PageInfo) | ||
}); | ||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Connection<T>.TotalCount), | ||
Type = typeof(IntGraphType), | ||
Resolver = new FuncFieldResolver<Connection<T>, int?>(context => context.Source.TotalCount) | ||
}); | ||
|
||
return graphType; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2021 Samuel Abraham | ||
|
||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using TypeCache.GraphQL.Attributes; | ||
using TypeCache.GraphQL.Extensions; | ||
using static System.FormattableString; | ||
|
||
namespace TypeCache.GraphQL.Data; | ||
|
||
[GraphQLDescription("An edge in a connection that associates a cursor ID with data.")] | ||
public readonly record struct Edge<T>( | ||
[GraphQLDescription("An identification number for use in pagination.")] int Cursor | ||
, [GraphQLDescription("The data item associated with the cursor value.")] T Node) | ||
where T : notnull | ||
{ | ||
public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphType) | ||
{ | ||
var graphType = new ObjectGraphType | ||
{ | ||
Name = Invariant($"{name}{nameof(Edge<T>)}"), | ||
Description = typeof(Edge<T>).GraphQLDescription() | ||
}; | ||
|
||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Edge<T>.Cursor), | ||
Type = typeof(IntGraphType), | ||
Resolver = new FuncFieldResolver<Edge<T>, int>(context => context.Source.Cursor) | ||
}); | ||
graphType.AddField(new() | ||
{ | ||
Name = nameof(Edge<T>.Node), | ||
ResolvedType = new NonNullGraphType(dataGraphType), | ||
Resolver = new FuncFieldResolver<Edge<T>, T>(context => context.Source.Node) | ||
}); | ||
|
||
return graphType; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2021 Samuel Abraham | ||
|
||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using TypeCache.GraphQL.Attributes; | ||
using static System.FormattableString; | ||
|
||
namespace TypeCache.GraphQL.Data; | ||
|
||
[GraphQLDescription("Information about pagination in a connection.")] | ||
public readonly record struct PageInfo( | ||
[GraphQLDescription("The first cursor value of the current page.")] int StartCursor | ||
, [GraphQLDescription("The last cursor value of the current page.")] int EndCursor | ||
, [GraphQLDescription("Whether a page exists before the current page.")] bool HasPreviousPage | ||
, [GraphQLDescription("Whether a page exists after the current page.")] bool HasNextPage) | ||
{ | ||
public PageInfo(uint offset, uint fetch, int totalCount) | ||
: this((int)offset + 1, (int)(offset + fetch), offset > 0, (offset + fetch) < totalCount) | ||
{ | ||
} | ||
} |
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
Oops, something went wrong.