tacone / loki

A docker / react / svelte / graphql / postgraphile starter app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please don't allow null lists

tacone opened this issue · comments

Null lists may break the code when it expects an array.

Take for example:

CREATE OR REPLACE VIEW public.submissions_statistics_view
AS SELECT a.age,
    c.country,
    e.experience_rating,
    g.gender,
    t.total_submissions
   FROM ( SELECT array_agg(age_stats.*) AS age
           FROM age_stats) a,
    ( SELECT array_agg(country_stats.*) AS country
           FROM country_stats) c,
    ( SELECT array_agg(experience_rating_stats.*) AS experience_rating
           FROM experience_rating_stats) e,
    ( SELECT array_agg(gender_stats.*) AS gender
           FROM gender_stats) g,
    ( SELECT count(*)::integer AS total_submissions
           FROM submissions) t;

And this query:

query MyQuery {
  submissionsStatistics {
    age {
      count
      ratio
      value
    }
    totalSubmissions
  }
}

The result with be this:

{
  "data": {
    "submissionsStatistics": {
      "age": null,
      "totalSubmissions": 0
    }
  }
}

Rather than this:

{
  "data": {
    "submissionsStatistics": {
      "age": [],
      "totalSubmissions": 0
    }
  }
}

Running the query in a Sql IDE does return the expected result though:

image

For this reason I'm going opinionated on this and just return [] wherever I find a null list, even if it's nullable.