From b62581d8e024b24f1d292f2994d0f958e58e0c40 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 28 Aug 2020 19:52:35 +0200 Subject: [PATCH] Variables fix. --- .../Contents/GraphQL/GraphQLModel.cs | 2 +- .../Contents/GraphQL/GraphQLQuery.cs | 6 +-- .../Contents/ContentsController.cs | 18 ++++++--- .../Contents/Models/GraphQLGetDto.cs | 31 ++++++++++++++ .../Contents/Models/GraphQLPostDto.cs | 32 +++++++++++++++ .../TestSuite.ApiTests/ContentQueryTests.cs | 40 +++++++++++-------- 6 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLGetDto.cs create mode 100644 backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLPostDto.cs diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs index 8bdfc9219..cd7912a4e 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs @@ -170,7 +170,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL context.Setup(execution); execution.Schema = graphQLSchema; - execution.Inputs = query.Variables?.ToInputs(); + execution.Inputs = query.Inputs; execution.Query = query.Query; }).ConfigureAwait(false); diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLQuery.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLQuery.cs index 91d17ec15..b25276d16 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLQuery.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLQuery.cs @@ -5,7 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using Newtonsoft.Json.Linq; +using GraphQL; namespace Squidex.Domain.Apps.Entities.Contents.GraphQL { @@ -13,10 +13,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL { public string OperationName { get; set; } - public string NamedQuery { get; set; } - public string Query { get; set; } - public JObject Variables { get; set; } + public Inputs? Inputs { get; set; } } } diff --git a/backend/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs b/backend/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs index 7c5f30c0f..65ab2cfaf 100644 --- a/backend/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs +++ b/backend/src/Squidex/Areas/Api/Controllers/Contents/ContentsController.cs @@ -55,9 +55,11 @@ namespace Squidex.Areas.Api.Controllers.Contents [Route("content/{app}/graphql/")] [ApiPermission] [ApiCosts(2)] - public async Task GetGraphQL(string app, [FromQuery] GraphQLQuery? queries = null) + public async Task GetGraphQL(string app, [FromQuery] GraphQLGetDto? queries = null) { - var (hasError, response) = await graphQl.QueryAsync(Context, queries ?? new GraphQLQuery()); + var request = queries?.ToQuery() ?? new GraphQLQuery(); + + var (hasError, response) = await graphQl.QueryAsync(Context, request); if (hasError) { @@ -85,9 +87,11 @@ namespace Squidex.Areas.Api.Controllers.Contents [Route("content/{app}/graphql/")] [ApiPermission] [ApiCosts(2)] - public async Task PostGraphQL(string app, [FromBody] GraphQLQuery query) + public async Task PostGraphQL(string app, [FromBody] GraphQLPostDto query) { - var (hasError, response) = await graphQl.QueryAsync(Context, query); + var request = query?.ToQuery() ?? new GraphQLQuery(); + + var (hasError, response) = await graphQl.QueryAsync(Context, request); if (hasError) { @@ -115,9 +119,11 @@ namespace Squidex.Areas.Api.Controllers.Contents [Route("content/{app}/graphql/batch")] [ApiPermission] [ApiCosts(2)] - public async Task PostGraphQLBatch(string app, [FromBody] GraphQLQuery[] batch) + public async Task PostGraphQLBatch(string app, [FromBody] GraphQLPostDto[] batch) { - var (hasError, response) = await graphQl.QueryAsync(Context, batch); + var request = batch.Select(x => x.ToQuery()).ToArray(); + + var (hasError, response) = await graphQl.QueryAsync(Context, request); if (hasError) { diff --git a/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLGetDto.cs b/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLGetDto.cs new file mode 100644 index 000000000..7a0de20a7 --- /dev/null +++ b/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLGetDto.cs @@ -0,0 +1,31 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using GraphQL; +using Squidex.Domain.Apps.Entities.Contents.GraphQL; +using Squidex.Infrastructure.Reflection; + +namespace Squidex.Areas.Api.Controllers.Contents.Models +{ + public class GraphQLGetDto + { + public string OperationName { get; set; } + + public string Query { get; set; } + + public string Variables { get; set; } + + public GraphQLQuery ToQuery() + { + var query = SimpleMapper.Map(this, new GraphQLQuery()); + + query.Inputs = Variables?.ToInputs(); + + return query; + } + } +} diff --git a/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLPostDto.cs b/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLPostDto.cs new file mode 100644 index 000000000..c2a4e89da --- /dev/null +++ b/backend/src/Squidex/Areas/Api/Controllers/Contents/Models/GraphQLPostDto.cs @@ -0,0 +1,32 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using GraphQL; +using Newtonsoft.Json.Linq; +using Squidex.Domain.Apps.Entities.Contents.GraphQL; +using Squidex.Infrastructure.Reflection; + +namespace Squidex.Areas.Api.Controllers.Contents.Models +{ + public class GraphQLPostDto + { + public string OperationName { get; set; } + + public string Query { get; set; } + + public JObject Variables { get; set; } + + public GraphQLQuery ToQuery() + { + var query = SimpleMapper.Map(this, new GraphQLQuery()); + + query.Inputs = Variables?.ToInputs(); + + return query; + } + } +} diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs index 73b3d8a58..5e758311d 100644 --- a/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs +++ b/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs @@ -185,16 +185,20 @@ namespace TestSuite.ApiTests var query = new { query = @" - { - queryMyReadsContents(filter: ""data/number/iv gt 3 and data/number/iv lt 7"", orderby: ""data/number/iv asc"") { - id, - data { - number { - iv + query ContentsQuery($filter: String!) { + queryMyReadsContents(filter: $filter, orderby: ""data/number/iv asc"") { + id, + data { + number { + iv + } } } - } - }" + }", + variables = new + { + filter = @"data/number/iv gt 3 and data/number/iv lt 7" + } }; var result = await _.Contents.GraphQlAsync(query); @@ -210,16 +214,20 @@ namespace TestSuite.ApiTests var query = new { query = @" - { - queryMyReadsContents(filter: ""data/number/iv gt 3 and data/number/iv lt 7"", orderby: ""data/number/iv asc"") { - id, - data { - number { - iv + query ContentsQuery($filter: String!) { + queryMyReadsContents(filter: $filter, orderby: ""data/number/iv asc"") { + id, + data { + number { + iv + } } } - } - }" + }", + variables = new + { + filter = @"data/number/iv gt 3 and data/number/iv lt 7" + } }; var result = await _.Contents.GraphQlGetAsync(query);