introproventures / graphql-jpa-query

Generate GraphQL Query Api for your JPA Entity Models
https://github.com/introproventures/graphql-jpa-query
Apache License 2.0
195 stars 54 forks source link

Add optional feature for aggregate count query support on plural entity object types #488

Closed igdianov closed 4 months ago

igdianov commented 4 months ago

Added an optional feature to generate aggregate count query for plural entities object types. The aggregate feature can be enabled via GraphQLSchemaBuilderCustomizer:

@Bean
GraphQLJPASchemaBuilderCustomizer graphQLJPASchemaBuilderCustomizer() {
   return builder ->
      builder
         .name("Query")
         .enableAggregate(true);
});

The GraphQL Schema builder will generate aggregate selection field to be able to request counts. The count can be grouped by entity fields and nested associations, i.e.

query {
  Tasks {
    aggregate {
      countTasks: count
      countProcessVariables: count(of: processVariables)
      countTaskVariables: count(of: variables)
      countTasksGroupedByStatus: group {
        status: by(field: status)
        count
      }
      countProcessVariablesGroupedByTaskName: group {
        name: by(field: name)
        count(of: processVariables)
      }
      countTaskProcessVariablesGroupedByVariableNameAndValue: processVariables {
        name: by(field: name)
        value: by(field: value)
        count
      }
      countTaskVariablesGroupedByVariableName: variables {
        name: by(field: name)
        count
      }
    }
  }
}

Will return counts by different groups including nested associations:

{
  "data": {
    "Tasks": {
      "aggregate": {
        "countTasks": 1000,
        "countProcessVariables": 6,
        "countTaskVariables": 42,
        "countTasksGroupedByStatus": [
          {
            "status": "COMPLETED",
            "count": 335
          },
          {
            "status": "ASSIGNED",
            "count": 337
          },
          {
            "status": "CREATED",
            "count": 328
          }
        ],
        "countProcessVariablesGroupedByTaskName": [
          {
            "name": "Solarbreeze",
            "count": 6
          }
        ],
        "countTaskProcessVariablesGroupedByVariableNameAndValue": [
          {
            "name": "applicationId",
            "value": "232951752337576",
            "count": 1
          },
          {
            "name": "moduleid",
            "value": "LBU",
            "count": 1
          },
          {
            "name": "isApproved",
            "value": true,
            "count": 1
          },
          {
            "name": "approverlist",
            "value": [
              "andrelaksmana"
            ],
            "count": 1
          },
          {
            "name": "applicationDate",
            "value": "2023-10-22T00:00:00.000+0000",
            "count": 1
          },
          {
            "name": "nullable",
            "value": null,
            "count": 1
          }
        ],
        "countTaskVariablesGroupedByVariableName": [
          {
            "name": "accountNumber",
            "count": 7
          },
          {
            "name": "variable1",
            "count": 5
          },
          {
            "name": "variable2",
            "count": 6
          },
          {
            "name": "variable3",
            "count": 6
          },
          {
            "name": "variable4",
            "count": 6
          },
          {
            "name": "variable5",
            "count": 6
          },
          {
            "name": "variable7",
            "count": 6
          }
        ]
      }
    }
  }
}
codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 91.10512% with 33 lines in your changes are missing coverage. Please review.

Project coverage is 77.55%. Comparing base (c5527d4) to head (3bec49e). Report is 5 commits behind head on master.

Files Patch % Lines
.../jpa/query/schema/impl/GraphQLJpaQueryFactory.java 77.98% 12 Missing and 12 partials :warning:
.../query/schema/impl/GraphQLJpaQueryDataFetcher.java 95.00% 4 Missing and 1 partial :warning:
...jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java 97.27% 1 Missing and 3 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #488 +/- ## ============================================ + Coverage 76.73% 77.55% +0.82% - Complexity 1231 1300 +69 ============================================ Files 79 79 Lines 5372 5704 +332 Branches 745 759 +14 ============================================ + Hits 4122 4424 +302 - Misses 928 943 +15 - Partials 322 337 +15 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.