From 4524256ccd3baf66181f6e3c50a71382d8e4cc84 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Fri, 17 Feb 2023 18:32:43 +0800 Subject: [PATCH] fix: Fixed group by query --- bin/generator/generator.dart | 38 ++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/bin/generator/generator.dart b/bin/generator/generator.dart index e1733885..0521890c 100644 --- a/bin/generator/generator.dart +++ b/bin/generator/generator.dart @@ -793,9 +793,27 @@ extension ModelFluentGenerator on Generator { .property('map') .call([localMapCompiler]); - final fieldsExpression = + code.Expression fieldsExpression = (fields == null || fields.isEmpty) ? modelScalarFields : localFields; + if (field.name.toLowerCase().startsWith('groupby')) { + final enumMapCompiler = code.Method((updates) { + updates.requiredParameters.add(code.Parameter((updates) { + updates.name = 'e'; + })); + updates.body = + code.refer('GraphQLField', packages.graphql).newInstance([ + code + .refer('e') + .property('originalName') + .ifNullThen(code.refer('e').property('name')) + ]).code; + updates.lambda = true; + }).closure; + fieldsExpression = + code.refer('by').property('map').call([enumMapCompiler]); + } + expressions.add(code.declareFinal('fields').assign(fieldsExpression)); code.Reference type = code.refer('Map'); @@ -847,17 +865,21 @@ extension ModelFluentGenerator on Generator { })); updates.lambda = true; - final compiler = code.refer('compiler').call([code.refer('json')]); + final call = field.outputType.isList + ? code.refer('json').property('cast').call([]) + : code.refer('json'); + + final compiler = code.refer('compiler').call([call]); final thrown = code.refer('Exception').newInstance([ code.literalString('Unable to parse response'), ]).thrown; - updates.body = code - .refer('json') - .isA(type) - .conditional(compiler, - _isOperationReturnTypeNullable(field) ? code.literalNull : thrown) - .code; + final other = + _isOperationReturnTypeNullable(field) ? code.literalNull : thrown; + final $type = field.outputType.isList ? code.refer('Iterable') : type; + + updates.body = + code.refer('json').isA($type).conditional(compiler, other).code; }); final returns = code