simonw / datasette-graphql

Datasette plugin providing an automatic GraphQL API for your SQLite databases
https://datasette-graphql-demo.datasette.io/
Apache License 2.0
98 stars 6 forks source link

Columns of views have incorrect datatype #87

Open ad-si opened 2 years ago

ad-si commented 2 years ago

As far as I can tell all columns of views (which don't have any type affinity per default) are coerced to TEXT by datasette-graphql.

One way to figure out the correct datatype would be to inspect all values (or a sample?) and use that information:

sqlite> select typeof(name), typeof(name_length) from songs_view;
text|integer
text|integer
text|integer
simonw commented 2 years ago

I'm not seeing that for this view: https://datasette-graphql-demo.datasette.io/github/repos_starred

Here's the generated GraphQL query for it: https://datasette-graphql-demo.datasette.io/graphql/github?query=%7B%0A%20%20repos_starred%20%7B%0A%20%20%20%20totalCount%0A%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20endCursor%0A%20%20%20%20%7D%0A%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20starred_at%0A%20%20%20%20%20%20starred_by%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20node_id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20full_name%0A%20%20%20%20%20%20private%0A%20%20%20%20%20%20owner%0A%20%20%20%20%20%20html_url%0A%20%20%20%20%20%20description_%0A%20%20%20%20%20%20fork%0A%20%20%20%20%20%20created_at%0A%20%20%20%20%20%20updated_at%0A%20%20%20%20%20%20pushed_at%0A%20%20%20%20%20%20homepage%0A%20%20%20%20%20%20size%0A%20%20%20%20%20%20stargazers_count%0A%20%20%20%20%20%20watchers_count%0A%20%20%20%20%20%20language%0A%20%20%20%20%20%20has_issues%0A%20%20%20%20%20%20has_projects%0A%20%20%20%20%20%20has_downloads%0A%20%20%20%20%20%20has_wiki%0A%20%20%20%20%20%20has_pages%0A%20%20%20%20%20%20forks_count%0A%20%20%20%20%20%20archived%0A%20%20%20%20%20%20disabled%0A%20%20%20%20%20%20open_issues_count%0A%20%20%20%20%20%20license%0A%20%20%20%20%20%20topics%0A%20%20%20%20%20%20forks%0A%20%20%20%20%20%20open_issues%0A%20%20%20%20%20%20watchers%0A%20%20%20%20%20%20default_branch%0A%20%20%20%20%20%20permissions%0A%20%20%20%20%20%20organization%0A%20%20%20%20%20%20temp_clone_token%0A%20%20%20%20%20%20allow_squash_merge%0A%20%20%20%20%20%20allow_merge_commit%0A%20%20%20%20%20%20allow_rebase_merge%0A%20%20%20%20%20%20delete_branch_on_merge%0A%20%20%20%20%20%20network_count%0A%20%20%20%20%20%20subscribers_count%0A%20%20%20%20%20%20parent%0A%20%20%20%20%20%20source%0A%20%20%20%20%20%20allow_auto_merge%0A%20%20%20%20%20%20allow_forking%0A%20%20%20%20%20%20visibility%0A%20%20%20%20%20%20is_template%0A%20%20%20%20%20%20template_repository%0A%20%20%20%20%20%20allow_update_branch%0A%20%20%20%20%20%20use_squash_pr_title_as_default%0A%20%20%20%20%20%20web_commit_signoff_required%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D

Here's what I see in GraphiQL:

image
simonw commented 2 years ago

The definition of that view is:

CREATE VIEW repos_starred AS select
  stars.starred_at,
  starring_user.login as starred_by,
  repos.*
from
  repos
  join stars on repos.id = stars.repo
  join users as starring_user on stars.user = starring_user.id
  join users on repos.owner = users.id
order by
  starred_at desc;

Maybe the column type affinities are making it through because SQLite can track which original table they came from?

simonw commented 2 years ago

@ad-si can you provide SQL that produces a small example database that exhibits this bug?

ad-si commented 1 year ago
CREATE VIEW multi_type AS
SELECT 1 AS col UNION
SELECT 2.2 AS col UNION
SELECT 'three' AS col UNION
SELECT NULL AS col
image

But since GraphQL does not support multi-type fields (or unions on scalars) converting to String is probably the best workaround…

ad-si commented 1 year ago

This is what I was talking about at the beginning:

CREATE VIEW length AS
SELECT 3 as col UNION
SELECT 4 as col
image

Type of col is actually INTEGER, but should (can?) the GraphQL endpoint be able to pick that up. I'm not sure …