nuwave / nuwave/lighthouse

Handle incompatible aggregate function and column in `@orderBy` directive

Open
#2,487 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
3.5k
Forks
468
Avg merge
3h 9m
Merged PRs (30d)
2

Description

What problem does this feature proposal attempt to solve?

The orderBy directive provides 5 aggregate functions: AVG, MIN, MAX, SUM, COUNT. Not all of these functions can be used with all column types. This should be handled and should return a user friendly message in the response.

Specifically, AVG and SUM only work on numeric column types.

Which possible solutions should be considered?

Handling the specific issue mentioned with AVG and SUM on non-numeric types, could be solved simply with:

$relationModel = $builder->getModel()->$relation()->getRelated()->whereNotNull($relationColumn)->first();

if (! is_numeric($relationModel->$relationColumn) && in_array($aggregate, ['sum', 'avg'])) {
    throw new Error("$relation cannot be aggregated by SUM or AVG, please choose a different aggregate and try again.");
}

However, a more robust way of testing whether the aggregate function actually works with the specified column would be better.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the @orderBy directive and the code that applies AVG, MIN, MAX, SUM, and COUNT to relation columns. Determine how to validate aggregate and column compatibility, then verify that incompatible AVG or SUM requests return a user-friendly response error without affecting valid aggregates.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, laravel, php
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.