Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pages/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public class Query :
public Query(IEfGraphQLService efGraphQlService) :
base(efGraphQlService)
{
AddQueryField<CompanyGraph, Company>(
AddQueryField(
name: "companies",
resolve: context =>
{
Expand Down
20 changes: 10 additions & 10 deletions pages/defining-graphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public class Query :
public Query(IEfGraphQLService graphQlService) :
base(graphQlService)
{
AddSingleField<CompanyGraph, Company>(
name: "company",
AddSingleField(
resolve: context =>
{
var dataContext = (DataContext) context.UserContext;
return dataContext.Companies;
});
AddQueryField<CompanyGraph, Company>(
},
name: "company");
AddQueryField(
name: "companies",
resolve: context =>
{
Expand All @@ -79,7 +79,7 @@ public class Query :
}
}
```
<sup>[snippet source](/src/Snippets/RootQuery.cs#L6-L29)</sup>
<sup>[snippet source](/src/Snippets/RootQuery.cs#L6-L31)</sup>
<!-- endsnippet -->

`AddQueryField` will result in all matching being found and returned.
Expand All @@ -99,10 +99,10 @@ public class CompanyGraph :
{
Field(x => x.Id);
Field(x => x.Content);
AddNavigationField<EmployeeGraph, Employee>(
AddNavigationField<Employee>(
name: "employees",
resolve: context => context.Source.Employees);
AddNavigationConnectionField<EmployeeGraph, Employee>(
AddNavigationConnectionField(
name: "employeesConnection",
resolve: context => context.Source.Employees,
includeNames: new[] {"Employees"});
Expand All @@ -129,7 +129,7 @@ public class Query :
public Query(IEfGraphQLService graphQlService) :
base(graphQlService)
{
AddQueryConnectionField<CompanyGraph, Company>(
AddQueryConnectionField(
name: "companies",
resolve: context =>
{
Expand Down Expand Up @@ -227,7 +227,7 @@ public class CompanyGraph :
public CompanyGraph(IEfGraphQLService graphQlService) :
base(graphQlService)
{
AddNavigationConnectionField<EmployeeGraph, Employee>(
AddNavigationConnectionField(
name: "employees",
resolve: context => context.Source.Employees);
}
Expand Down Expand Up @@ -297,5 +297,5 @@ Field<ListGraphType<EmployeeSummaryGraph>>(
};
});
```
<sup>[snippet source](/src/SampleWeb/Query.cs#L65-L95)</sup>
<sup>[snippet source](/src/SampleWeb/Query.cs#L65-L97)</sup>
<!-- endsnippet -->
41 changes: 6 additions & 35 deletions src/GraphQL.EntityFramework/EfGraphQLService_Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,10 @@ namespace GraphQL.EntityFramework
{
partial class EfGraphQLService
{
public FieldType AddNavigationField<TGraph, TReturn>(
ObjectGraphType graph,
string name,
Func<ResolveFieldContext<object>, TReturn> resolve,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var field = BuildNavigationField(name, resolve, includeNames, typeof(TGraph), arguments);
return graph.AddField(field);
}

public FieldType AddNavigationField<TSource, TReturn>(
ObjectGraphType<TSource> graph,
Type graphType,
public FieldType AddNavigationField<TSource, TReturn>(ObjectGraphType<TSource> graph,
string name,
Func<ResolveFieldContext<TSource>, TReturn> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TReturn : class
Expand All @@ -35,11 +20,10 @@ public FieldType AddNavigationField<TSource, TReturn>(
return graph.AddField(field);
}

public FieldType AddNavigationField<TReturn>(
ObjectGraphType graph,
Type graphType,
public FieldType AddNavigationField<TReturn>(ObjectGraphType graph,
string name,
Func<ResolveFieldContext<object>, TReturn> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TReturn : class
Expand All @@ -49,20 +33,6 @@ public FieldType AddNavigationField<TReturn>(
return graph.AddField(field);
}

public FieldType AddNavigationField<TSource, TGraph, TReturn>(
ObjectGraphType<TSource> graph,
string name,
Func<ResolveFieldContext<TSource>, TReturn> resolve,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var field = BuildNavigationField(name, resolve, includeNames, typeof(TGraph), arguments);
return graph.AddField(field);
}

FieldType BuildNavigationField<TSource, TReturn>(
string name,
Func<ResolveFieldContext<TSource>, TReturn> resolve,
Expand All @@ -72,8 +42,8 @@ FieldType BuildNavigationField<TSource, TReturn>(
where TReturn : class
{
Guard.AgainstNullWhiteSpace(nameof(name), name);
Guard.AgainstNull(nameof(graphType), graphType);
Guard.AgainstNull(nameof(resolve), resolve);
graphType = GraphTypeFinder.FindGraphType<TReturn>(graphType);
return new FieldType
{
Name = name,
Expand All @@ -87,6 +57,7 @@ FieldType BuildNavigationField<TSource, TReturn>(
{
return result;
}

return null;
})
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GraphQL.Builders;
using GraphQL.Types;

namespace GraphQL.EntityFramework
{
partial class EfGraphQLService
{
public ConnectionBuilder<TGraph, object> AddNavigationConnectionField<TGraph, TReturn>(
public void AddNavigationConnectionField<TReturn>(
ObjectGraphType graph,
string name,
Func<ResolveFieldContext<object>, IEnumerable<TReturn>> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null,
int pageSize = 10)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var connection = BuildListConnectionField<object, TGraph, TReturn>(name, resolve, includeNames, pageSize);
var connection = BuildListConnectionField(name, resolve, includeNames, pageSize, graphType);
var field = graph.AddField(connection.FieldType);
field.AddWhereArgument(arguments);
return connection;
}

public ConnectionBuilder<TGraph, TSource> AddNavigationConnectionField<TSource, TGraph, TReturn>(
public void AddNavigationConnectionField<TSource, TReturn>(
ObjectGraphType<TSource> graph,
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null,
int pageSize = 10)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var connection = BuildListConnectionField<TSource, TGraph, TReturn>(name, resolve, includeNames, pageSize);
var connection = BuildListConnectionField(name, resolve, includeNames, pageSize, graphType);
var field = graph.AddField(connection.FieldType);
field.AddWhereArgument(arguments);
return connection;
}

ConnectionBuilder<TGraph, TSource> BuildListConnectionField<TSource, TGraph, TReturn>(
ConnectionBuilder<FakeGraph, TSource> BuildListConnectionField<TSource, TReturn>(
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
IEnumerable<string> includeName,
int pageSize)
where TGraph : ObjectGraphType<TReturn>, IGraphType
int pageSize,
Type graphType)
where TReturn : class
{
Guard.AgainstNullWhiteSpace(nameof(name), name);
Guard.AgainstNull(nameof(resolve), resolve);
Guard.AgainstNegative(nameof(pageSize), pageSize);
var builder = ConnectionBuilder.Create<TGraph, TSource>();
graphType = GraphTypeFinder.FindGraphType<TReturn>(graphType);
var fieldType = GetFieldType<TSource>(name, graphType);
var builder = ConnectionBuilder<FakeGraph, TSource>.Create(name);
builder.PageSize(pageSize);
builder.Name(name);
SetField(builder, fieldType);
IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
builder.ResolveAsync(async context =>
{
var enumerable = resolve(context);
enumerable = enumerable.ApplyGraphQlArguments(context);
enumerable = await filters.ApplyFilter(enumerable,context.UserContext);
enumerable = await filters.ApplyFilter(enumerable, context.UserContext);
var page = enumerable.ToList();

return ConnectionConverter.ApplyConnectionContext(
Expand All @@ -73,5 +74,22 @@ ConnectionBuilder<TGraph, TSource> BuildListConnectionField<TSource, TGraph, TRe
});
return builder;
}

static void SetField(object builder, object fieldType)
{
var fieldTypeField = builder.GetType().GetProperty("FieldType", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
fieldTypeField.SetValue(builder, fieldType);
}

static object GetFieldType<TSource>(string name, Type graphType)
{
var makeGenericType = typeof(ConnectionBuilder<,>).MakeGenericType(graphType, typeof(TSource));
dynamic x = makeGenericType.GetMethod("Create").Invoke(null, new object[] {name});
return x.FieldType;
}

class FakeGraph : GraphType
{
}
}
}
49 changes: 4 additions & 45 deletions src/GraphQL.EntityFramework/EfGraphQLService_NavigationList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,11 @@ namespace GraphQL.EntityFramework
{
partial class EfGraphQLService
{
public FieldType AddNavigationField<TGraph, TReturn>(
ObjectGraphType graph,
string name,
Func<ResolveFieldContext<object>, IEnumerable<TReturn>> resolve,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var field = BuildNavigationField<object, TGraph, TReturn>(name, resolve, includeNames, arguments);
return graph.AddField(field);
}

public FieldType AddNavigationField<TSource, TReturn>(
ObjectGraphType<TSource> graph,
Type graphType,
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TReturn : class
Expand All @@ -35,11 +21,10 @@ public FieldType AddNavigationField<TSource, TReturn>(
return graph.AddField(field);
}

public FieldType AddNavigationField<TReturn>(
ObjectGraphType graph,
Type graphType,
public FieldType AddNavigationField<TReturn>(ObjectGraphType graph,
string name,
Func<ResolveFieldContext<object>, IEnumerable<TReturn>> resolve,
Type graphType = null,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TReturn : class
Expand All @@ -57,37 +42,11 @@ FieldType BuildNavigationField<TSource, TReturn>(
IEnumerable<QueryArgument> arguments)
where TReturn : class
{
Guard.AgainstNull(nameof(graphType), graphType);
graphType = GraphTypeFinder.FindGraphType<TReturn>(graphType);
var listGraphType = MakeListGraphType(graphType);
return BuildNavigationField(name, resolve, includeNames, listGraphType, arguments);
}

public FieldType AddNavigationField<TSource, TGraph, TReturn>(
ObjectGraphType<TSource> graph,
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
IEnumerable<QueryArgument> arguments = null,
IEnumerable<string> includeNames = null)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
Guard.AgainstNull(nameof(graph), graph);
var field = BuildNavigationField<TSource, TGraph, TReturn>(name, resolve, includeNames, arguments);
return graph.AddField(field);
}

FieldType BuildNavigationField<TSource, TGraph, TReturn>(
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
IEnumerable<string> includeNames,
IEnumerable<QueryArgument> arguments)
where TGraph : ObjectGraphType<TReturn>, IGraphType
where TReturn : class
{
var listGraphType = typeof(ListGraphType<TGraph>);
return BuildNavigationField(name, resolve, includeNames, listGraphType, arguments);
}

FieldType BuildNavigationField<TSource, TReturn>(
string name,
Func<ResolveFieldContext<TSource>, IEnumerable<TReturn>> resolve,
Expand Down
Loading