diff --git a/src/Directory.Build.props b/src/Directory.Build.props index c606c0d8e..52dccac63 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;NU5104;CS1573 - 21.3.1 + 22.0.0 1.0.0 EntityFrameworkCore, EntityFramework, GraphQL true diff --git a/src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Single.cs b/src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Single.cs index 220636596..36ccbc029 100644 --- a/src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Single.cs +++ b/src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Single.cs @@ -9,10 +9,11 @@ public FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, 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(field); } @@ -23,10 +24,11 @@ public FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, 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(field); } @@ -37,10 +39,11 @@ public FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, 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(field); } @@ -50,7 +53,8 @@ FieldType BuildSingleField( Func, IQueryable> resolve, Func, TReturn, Task>? mutate, Type? graphType, - bool nullable) + bool nullable, + bool omitQueryArguments) where TReturn : class { Guard.AgainstWhiteSpace(nameof(name), name); @@ -58,11 +62,10 @@ FieldType BuildSingleField( graphType ??= GraphTypeFinder.FindGraphType(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( async context => { @@ -77,7 +80,10 @@ FieldType BuildSingleField( } query = includeAppender.AddIncludes(query, context); - query = query.ApplyGraphQlArguments(context, names, false); + if (!omitQueryArguments) + { + query = query.ApplyGraphQlArguments(context, names, false); + } QueryLogger.Write(query); @@ -112,5 +118,10 @@ FieldType BuildSingleField( throw new ExecutionError("Not found"); }) }; + if (!omitQueryArguments) + { + type.Arguments = ArgumentAppender.GetQueryArguments(hasId, false); + } + return type; } } \ No newline at end of file diff --git a/src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs b/src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs index 684d3727d..ab945060c 100644 --- a/src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs +++ b/src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs @@ -68,7 +68,8 @@ public FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, 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); } \ No newline at end of file diff --git a/src/GraphQL.EntityFramework/GraphApi/IEfGraphQLService_Single.cs b/src/GraphQL.EntityFramework/GraphApi/IEfGraphQLService_Single.cs index 63be2b613..fcf9368f4 100644 --- a/src/GraphQL.EntityFramework/GraphApi/IEfGraphQLService_Single.cs +++ b/src/GraphQL.EntityFramework/GraphApi/IEfGraphQLService_Single.cs @@ -8,7 +8,8 @@ FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, TReturn, Task>? mutate = null, Type? graphType = null, - bool nullable = false) + bool nullable = false, + bool omitQueryArguments = false) where TReturn : class; FieldBuilder AddSingleField( @@ -17,6 +18,7 @@ FieldBuilder AddSingleField( Func, IQueryable> resolve, Func, TReturn, Task>? mutate = null, Type? graphType = null, - bool nullable = false) + bool nullable = false, + bool omitQueryArguments = false) where TReturn : class; } \ No newline at end of file diff --git a/src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs b/src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs index 15eb6cc3e..b4e902cb6 100644 --- a/src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs +++ b/src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs @@ -34,9 +34,10 @@ public FieldBuilder AddSingleField( Func, 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 AddIncludes(IQueryable query, IResolveFieldContext context) where TItem : class => diff --git a/src/Tests/IntegrationTests/IntegrationTests.SchemaPrint.verified.txt b/src/Tests/IntegrationTests/IntegrationTests.SchemaPrint.verified.txt index 0eb7e9e3b..52d29b653 100644 --- a/src/Tests/IntegrationTests/IntegrationTests.SchemaPrint.verified.txt +++ b/src/Tests/IntegrationTests/IntegrationTests.SchemaPrint.verified.txt @@ -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!]! diff --git a/src/Tests/IntegrationTests/IntegrationTests.Single_NoArgs.verified.txt b/src/Tests/IntegrationTests/IntegrationTests.Single_NoArgs.verified.txt new file mode 100644 index 000000000..c9c15acd1 --- /dev/null +++ b/src/Tests/IntegrationTests/IntegrationTests.Single_NoArgs.verified.txt @@ -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' + } + ] +} \ No newline at end of file diff --git a/src/Tests/IntegrationTests/IntegrationTests.cs b/src/Tests/IntegrationTests/IntegrationTests.cs index 7aed4fb81..0fc387e10 100644 --- a/src/Tests/IntegrationTests/IntegrationTests.cs +++ b/src/Tests/IntegrationTests/IntegrationTests.cs @@ -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() { diff --git a/src/Tests/IntegrationTests/Query.cs b/src/Tests/IntegrationTests/Query.cs index 9de537dd7..90ac1dd34 100644 --- a/src/Tests/IntegrationTests/Query.cs +++ b/src/Tests/IntegrationTests/Query.cs @@ -83,6 +83,11 @@ public Query(IEfGraphQLService 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,