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 src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;NU5104;CS1573</NoWarn>
<Version>21.3.1</Version>
<Version>22.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>EntityFrameworkCore, EntityFramework, GraphQL</PackageTags>
<ImplicitUsings>true</ImplicitUsings>
Expand Down
31 changes: 21 additions & 10 deletions src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public FieldBuilder<object, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, object>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class
{
var field = BuildSingleField(name, resolve, mutate, graphType, nullable);
var field = BuildSingleField(name, resolve, mutate, graphType, nullable, omitQueryArguments);
graph.AddField(field);
return new FieldBuilderEx<object, TReturn>(field);
}
Expand All @@ -23,10 +24,11 @@ public FieldBuilder<object, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, object>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class
{
var field = BuildSingleField(name, resolve, mutate, graphType, nullable);
var field = BuildSingleField(name, resolve, mutate, graphType, nullable, omitQueryArguments);
graph.AddField(field);
return new FieldBuilderEx<object, TReturn>(field);
}
Expand All @@ -37,10 +39,11 @@ public FieldBuilder<TSource, TReturn> AddSingleField<TSource, TReturn>(
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, TSource>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class
{
var field = BuildSingleField(name, resolve, mutate, graphType, nullable);
var field = BuildSingleField(name, resolve, mutate, graphType, nullable, omitQueryArguments);
graph.AddField(field);
return new FieldBuilderEx<TSource, TReturn>(field);
}
Expand All @@ -50,19 +53,19 @@ FieldType BuildSingleField<TSource, TReturn>(
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, TSource>, TReturn, Task>? mutate,
Type? graphType,
bool nullable)
bool nullable,
bool omitQueryArguments)
where TReturn : class
{
Guard.AgainstWhiteSpace(nameof(name), name);

graphType ??= GraphTypeFinder.FindGraphType<TReturn>(nullable);

var hasId = keyNames.ContainsKey(typeof(TReturn));
return new()
var type = new FieldType
{
Name = name,
Type = graphType,
Arguments = ArgumentAppender.GetQueryArguments(hasId, false),
Resolver = new FuncFieldResolver<TSource, TReturn?>(
async context =>
{
Expand All @@ -77,7 +80,10 @@ FieldType BuildSingleField<TSource, TReturn>(
}

query = includeAppender.AddIncludes(query, context);
query = query.ApplyGraphQlArguments(context, names, false);
if (!omitQueryArguments)
{
query = query.ApplyGraphQlArguments(context, names, false);
}

QueryLogger.Write(query);

Expand Down Expand Up @@ -112,5 +118,10 @@ FieldType BuildSingleField<TSource, TReturn>(
throw new ExecutionError("Not found");
})
};
if (!omitQueryArguments)
{
type.Arguments = ArgumentAppender.GetQueryArguments(hasId, false);
}
return type;
}
}
5 changes: 3 additions & 2 deletions src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public FieldBuilder<TSource, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, TSource>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class =>
GraphQlService.AddSingleField(this, name, resolve, mutate, graphType, nullable);
GraphQlService.AddSingleField(this, name, resolve, mutate, graphType, nullable, omitQueryArguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ FieldBuilder<object, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, object>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class;

FieldBuilder<TSource, TReturn> AddSingleField<TSource, TReturn>(
Expand All @@ -17,6 +18,7 @@ FieldBuilder<TSource, TReturn> AddSingleField<TSource, TReturn>(
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
Func<ResolveEfFieldContext<TDbContext, TSource>, TReturn, Task>? mutate = null,
Type? graphType = null,
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class;
}
5 changes: 3 additions & 2 deletions src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public FieldBuilder<object, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, object>, TReturn, Task>? mutate = null,
Type? graphType = null,
string name = nameof(TReturn),
bool nullable = false)
bool nullable = false,
bool omitQueryArguments = false)
where TReturn : class =>
GraphQlService.AddSingleField(this, name, resolve, mutate, graphType, nullable);
GraphQlService.AddSingleField(this, name, resolve, mutate, graphType, nullable, omitQueryArguments);

public IQueryable<TItem> AddIncludes<TItem>(IQueryable<TItem> query, IResolveFieldContext context)
where TItem : class =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ type Query {
parentEntitiesFiltered(id: ID, ids: [ID!], where: [WhereExpression!], orderBy: [OrderBy!], skip: Int, take: Int): [FilterParent!]!
parentEntitiesConnectionFiltered(after: String, first: Int, before: String, last: Int, where: [WhereExpression!], orderBy: [OrderBy!], ids: [ID!]): FilterParentConnection!
parentEntity(id: ID, ids: [ID!], where: [WhereExpression!]): Parent!
parentEntityWithNoArgs: Parent!
parentEntityNullable(id: ID, ids: [ID!], where: [WhereExpression!]): Parent
interfaceGraphConnection(after: String, first: Int, before: String, last: Int, where: [WhereExpression!], orderBy: [OrderBy!], ids: [ID!]): InterfaceConnection!
manyToManyLeftEntities(id: ID, ids: [ID!], where: [WhereExpression!], orderBy: [OrderBy!], skip: Int, take: Int): [ManyToManyLeft!]!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
target:
{
"data": {
"parentEntityWithNoArgs": {
"property": "Value1"
}
}
},
sql: [
{
HasTransaction: false,
Text:
SELECT TOP(2) [p].[Id], [p].[Property]
FROM [ParentEntities] AS [p]
WHERE [p].[Id] = 'Guid_1'
}
]
}
19 changes: 19 additions & 0 deletions src/Tests/IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,25 @@ public async Task Single_Found()
await RunQuery(database, query, null, null, false, new object[] { entity1, entity2 });
}

[Fact]
public async Task Single_NoArgs()
{
var query = """
{
parentEntityWithNoArgs {
property
}
}
""";
var entity1 = new ParentEntity
{
Id = Guid.Parse("00000000-0000-0000-0000-000000000001"),
Property = "Value1"
};
await using var database = await sqlInstance.Build();
await RunQuery(database, query, null, null, false, new object[] { entity1 });
}

[Fact]
public async Task SingleNullable_NotFound()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/IntegrationTests/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public Query(IEfGraphQLService<IntegrationDbContext> efGraphQlService) :
name: "parentEntity",
resolve: context => context.DbContext.ParentEntities);

AddSingleField(
name: "parentEntityWithNoArgs",
resolve: context => context.DbContext.ParentEntities.Where(_ => _.Id == new Guid("00000000-0000-0000-0000-000000000001")),
omitQueryArguments: true);

AddSingleField(
name: "parentEntityNullable",
resolve: context => context.DbContext.ParentEntities,
Expand Down