Skip to content

Commit a9b1aa5

Browse files
committed
simplify connection
1 parent be69f2b commit a9b1aa5

2 files changed

Lines changed: 14 additions & 39 deletions

File tree

src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_NavigationConnection.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace GraphQL.EntityFramework
1111
partial class EfGraphQLService<TDbContext>
1212
where TDbContext : DbContext
1313
{
14-
MethodInfo addEnumerableConnection = typeof(EfGraphQLService<TDbContext>)
14+
static MethodInfo addEnumerableConnection = typeof(EfGraphQLService<TDbContext>)
1515
.GetMethod("AddEnumerableConnection", BindingFlags.Instance| BindingFlags.NonPublic)!;
1616

1717
public void AddNavigationConnectionField<TSource, TReturn>(
@@ -80,31 +80,5 @@ void AddEnumerableConnection<TSource, TGraph, TReturn>(
8080

8181
field.AddWhereArgument(hasId, arguments);
8282
}
83-
84-
static void SetField(object builder, object fieldType)
85-
{
86-
var fieldTypeField = builder.GetType()
87-
.GetProperty("FieldType", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)!;
88-
fieldTypeField.SetValue(builder, fieldType);
89-
}
90-
91-
//TODO: can return null
92-
static object GetFieldType<TSource>(string name, Type graphType)
93-
{
94-
var makeGenericType = typeof(ConnectionBuilder<>).MakeGenericType(typeof(TSource));
95-
var genericMethodInfo = makeGenericType
96-
.GetMethods()
97-
.Single(method => method.Name == "Create" &&
98-
method.IsGenericMethod &&
99-
method.GetGenericArguments().Length == 1);
100-
var genericMethod = genericMethodInfo.MakeGenericMethod(graphType);
101-
dynamic? x = genericMethod.Invoke(null, new object[] { name }) ?? null;
102-
x?.Bidirectional();
103-
return x?.FieldType!;
104-
}
105-
106-
class FakeGraph : GraphType
107-
{
108-
}
10983
}
11084
}

src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_QueryableConnection.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
45
using GraphQL.Builders;
56
using GraphQL.Types;
67
using Microsoft.EntityFrameworkCore;
@@ -10,6 +11,9 @@ namespace GraphQL.EntityFramework
1011
partial class EfGraphQLService<TDbContext>
1112
where TDbContext : DbContext
1213
{
14+
static MethodInfo addQueryableConnection = typeof(EfGraphQLService<TDbContext>)
15+
.GetMethod("AddQueryableConnection", BindingFlags.Instance| BindingFlags.NonPublic)!;
16+
1317
public void AddQueryConnectionField<TReturn>(
1418
IComplexGraphType graph,
1519
string name,
@@ -20,7 +24,9 @@ public void AddQueryConnectionField<TReturn>(
2024
string? description = null)
2125
where TReturn : class
2226
{
23-
BuildQueryConnectionField(graph, name, resolve, arguments, pageSize, itemGraphType, description);
27+
itemGraphType ??= GraphTypeFinder.FindGraphType<TReturn>();
28+
var addConnectionT = addQueryableConnection.MakeGenericMethod(typeof(object), itemGraphType, typeof(TReturn));
29+
addConnectionT.Invoke(this, new object?[] { graph, name, resolve, arguments, pageSize, description });
2430
}
2531

2632
public void AddQueryConnectionField<TSource, TReturn>(
@@ -33,27 +39,22 @@ public void AddQueryConnectionField<TSource, TReturn>(
3339
string? description = null)
3440
where TReturn : class
3541
{
36-
BuildQueryConnectionField(graph, name, resolve, arguments, pageSize, itemGraphType, description);
42+
itemGraphType ??= GraphTypeFinder.FindGraphType<TReturn>();
43+
var addConnectionT = addQueryableConnection.MakeGenericMethod(typeof(TSource), itemGraphType, typeof(TReturn));
44+
addConnectionT.Invoke(this, new object?[] { graph, name, resolve, arguments, pageSize, description });
3745
}
3846

39-
void BuildQueryConnectionField<TSource, TReturn>(
47+
void AddQueryableConnection<TSource, TGraph, TReturn>(
4048
IComplexGraphType graph,
4149
string name,
4250
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve,
4351
IEnumerable<QueryArgument>? arguments,
4452
int pageSize,
45-
Type? itemGraphType,
4653
string? description)
54+
where TGraph : IGraphType
4755
where TReturn : class
4856
{
49-
Guard.AgainstWhiteSpace(nameof(name), name);
50-
Guard.AgainstNegative(nameof(pageSize), pageSize);
51-
52-
itemGraphType ??= GraphTypeFinder.FindGraphType<TReturn>();
53-
var fieldType = GetFieldType<TSource>(name, itemGraphType);
54-
55-
var builder = ConnectionBuilder<TSource>.Create<FakeGraph>(name);
56-
SetField(builder, fieldType);
57+
var builder = ConnectionBuilder<TSource>.Create<TGraph>(name);
5758
if (description != null)
5859
{
5960
builder.Description(description);

0 commit comments

Comments
 (0)