Skip to content

Commit

Permalink
Foreign key _list relationships now work with non-integer keys, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 13, 2020
1 parent 3169910 commit 5699e62
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
37 changes: 19 additions & 18 deletions datasette_graphql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ async def schema_for_database(datasette, database=None, tables=None):
to_add.append(
(
"resolve_{}".format(meta.graphql_name),
make_table_resolver(datasette, db.name, table, table_classes, meta),
make_table_resolver(
datasette, db.name, table, table_classes, table_metadata
),
)
)
# *_row field
Expand All @@ -158,7 +160,7 @@ async def schema_for_database(datasette, database=None, tables=None):
pk_column_type = graphene.Int()
else:
pk_column_type = types[meta.columns[pk]]
table_row_kwargs[pk] = pk_column_type
table_row_kwargs[meta.graphql_columns.get(pk, pk)] = pk_column_type
to_add.append(
(
"{}_row".format(meta.graphql_name),
Expand All @@ -179,7 +181,7 @@ async def schema_for_database(datasette, database=None, tables=None):
db.name,
table,
table_classes,
meta,
table_metadata,
pk_args=meta.pks,
return_first_row=True,
),
Expand Down Expand Up @@ -345,15 +347,7 @@ def make_table_node_class(
table_dict[
"resolve_{}_list".format(table_metadata[fk.table].graphql_name)
] = make_table_resolver(
datasette,
db.name,
fk.table,
table_classes,
fk_meta,
default_where="[{}] = ".format(fk.column)
+ "{root."
+ fk.other_column
+ "}",
datasette, db.name, fk.table, table_classes, table_metadata, related_fk=fk,
)

table_dict["from_row"] = classmethod(
Expand All @@ -377,13 +371,15 @@ def make_table_resolver(
database_name,
table_name,
table_classes,
meta,
default_where=None,
table_metadata,
related_fk=None,
pk_args=None,
return_first_row=False,
):
from datasette.views.table import TableView

meta = table_metadata[table_name]

async def resolve_table(
root,
info,
Expand Down Expand Up @@ -432,17 +428,22 @@ async def resolve_table(
if search and meta.supports_fts:
qs["_search"] = search

if related_fk:
related_column = meta.graphql_columns.get(
related_fk.column, related_fk.column
)
related_other_column = table_metadata[
related_fk.other_table
].graphql_columns.get(related_fk.other_column, related_fk.other_column)
qs[related_column] = getattr(root, related_other_column)

if where:
qs["_where"] = where

if sort:
qs["_sort"] = column_name_rev[sort]
elif sort_desc:
qs["_sort_desc"] = column_name_rev[sort_desc]

if default_where:
qs["_where"] = default_where.format(root=root)

path_with_query_string = "/{}/{}.json?{}".format(
database_name, table_name, urllib.parse.urlencode(qs)
)
Expand Down
6 changes: 3 additions & 3 deletions examples/nested.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
repo {
name
license {
key
_key
name
}
owner {
Expand All @@ -24,7 +24,7 @@
}
}
```
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20issues%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20user%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20repo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20owner%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20issues%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20user%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20repo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20owner%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)

Expected output:

Expand All @@ -41,7 +41,7 @@ Expected output:
"repo": {
"name": "datasette",
"license": {
"key": "apache2",
"_key": "apache2",
"name": "Apache 2"
},
"owner": {
Expand Down
8 changes: 4 additions & 4 deletions examples/nullable.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
nodes {
name
license {
key
_key
name
}
}
}
}
```
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20repos%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20repos%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)

Expected output:

Expand All @@ -24,14 +24,14 @@ Expected output:
{
"name": "datasette",
"license": {
"key": "apache2",
"_key": "apache2",
"name": "Apache 2"
}
},
{
"name": "dogspotter",
"license": {
"key": "mit",
"_key": "mit",
"name": "MIT"
}
},
Expand Down
42 changes: 38 additions & 4 deletions examples/related.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
nodes {
name
license {
key
_key
name
}
}
Expand All @@ -36,9 +36,19 @@
}
}
}
licenses_with_repos: licenses {
nodes {
name
repos_list {
nodes {
full_name
}
}
}
}
}
```
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20users%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20repos_list%28first%3A%201%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20totalCount%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20endCursor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20users_with_dogspotter%3A%20users%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20repos_list%28filter%3A%20%7Bname%3A%20%7Beq%3A%20%22dogspotter%22%7D%7D%2C%20sort%3A%20id%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20totalCount%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20endCursor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0A%7B%0A%20%20%20%20users%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20repos_list%28first%3A%201%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20totalCount%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20endCursor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20users_with_dogspotter%3A%20users%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20repos_list%28filter%3A%20%7Bname%3A%20%7Beq%3A%20%22dogspotter%22%7D%7D%2C%20sort%3A%20id%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20totalCount%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20endCursor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20licenses_with_repos%3A%20licenses%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20repos_list%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20full_name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)

Expected output:

Expand All @@ -58,7 +68,7 @@ Expected output:
{
"name": "dogspotter",
"license": {
"key": "mit",
"_key": "mit",
"name": "MIT"
}
}
Expand All @@ -77,7 +87,7 @@ Expected output:
{
"name": "datasette",
"license": {
"key": "apache2",
"_key": "apache2",
"name": "Apache 2"
}
}
Expand Down Expand Up @@ -115,6 +125,30 @@ Expected output:
}
}
]
},
"licenses_with_repos": {
"nodes": [
{
"name": "Apache 2",
"repos_list": {
"nodes": [
{
"full_name": "simonw/datasette"
}
]
}
},
{
"name": "MIT",
"repos_list": {
"nodes": [
{
"full_name": "cleopaws/dogspotter"
}
]
}
}
]
}
}
```
6 changes: 3 additions & 3 deletions examples/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ query specific_repo($name: String) {
nodes {
name
license {
key
_key
}
}
}
}
```
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0Aquery%20specific_repo%28%24name%3A%20String%29%20%7B%0A%20%20%20%20repos%28filter%3A%20%7Bname%3A%20%7Beq%3A%20%24name%7D%7D%29%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20key%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)
[Try this query](https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%0Aquery%20specific_repo%28%24name%3A%20String%29%20%7B%0A%20%20%20%20repos%28filter%3A%20%7Bname%3A%20%7Beq%3A%20%24name%7D%7D%29%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20license%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_key%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A)
Variables:
```json+variables
{
Expand All @@ -29,7 +29,7 @@ Expected output:
{
"name": "datasette",
"license": {
"key": "apache2"
"_key": "apache2"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def build_database(db):
pk="id",
)
db["licenses"].insert_all(
[{"key": "mit", "name": "MIT"}, {"key": "apache2", "name": "Apache 2"},],
pk="key",
[{"$key": "mit", "name": "MIT"}, {"$key": "apache2", "name": "Apache 2"},],
pk="$key",
)
db["repos"].insert_all(
[
Expand Down

0 comments on commit 5699e62

Please sign in to comment.