import ibis
from strawberry.utils.str_converters import to_camel_case
from graphique import GraphQL
def execute(query):
result = app.schema.execute_sync(query, root_value=app.root_value)
for error in result.errors or []:
raise ValueError(error)
return result.data
table = ibis.read_parquet("../../tests/fixtures/zipcodes.parquet")
# example projection: camel-cased fields (not relevant in this dataset)
_ = table.select({to_camel_case(name): table[name] for name in table.columns})
app = GraphQL(table)Example Queries
View schema.
execute("""{
count
schema {
names
types
partitioning
}
}"""){'count': 41700,
'schema': {'names': ['latitude',
'longitude',
'state',
'city',
'county',
'zipcode'],
'types': ['float64', 'float64', 'string', 'string', 'string', 'int32'],
'partitioning': []}}
California counties with the most cities.
- filter state by “CA”
- group by county
- aggregate distinct cities
- project city count
- order by city counts descending
- access columns
countyis still known in the schema- cities is a new column accessed through an inline fragment
execute("""{
filter(state: {eq: "CA"}) {
group(by: "county", aggregate: {collect: {name: "city", distinct: true}}) {
project(columns: {alias: "cities", array: {length: {name: "city"}}}) {
order(by: "-cities", limit: 5) {
columns {
county {
values
}
}
cities: column(name: "cities") {
... on BigIntColumn {
values
}
}
}
}
}
}
}"""){'filter': {'group': {'project': {'order': {'columns': {'county': {'values': ['Los Angeles',
'San Bernardino',
'San Diego',
'Orange',
'Riverside']}},
'cities': {'values': [132, 73, 52, 47, 42]}}}}}}
States with cities which match the name of their county.
- filter using
whereexpression, because comparing two columns is not a “simple” query Column.distinctinstead of group, because no other aggregates are needed
execute("""{
filter(where: {eq: [{name: "county"}, {name: "city"}]}) {
columns {
state {
distinct {
values
}
}
}
}
}"""){'filter': {'columns': {'state': {'distinct': {'values': ['VT',
'WV',
'NC',
'FL',
'MN',
'KS',
'AR',
'OK',
'TX',
'ID',
'AK',
'VA',
'OH',
'IN',
'WI',
'MO',
'NE',
'LA',
'NM',
'HI',
'OR',
'PR',
'MA',
'RI',
'NH',
'ME',
'CT',
'MD',
'KY',
'SD',
'CO',
'UT',
'AZ',
'NV',
'CA',
'WA',
'NJ',
'NY',
'PA',
'DE',
'SC',
'AL',
'TN',
'MS',
'MI',
'IA',
'ND',
'MT',
'IL',
'WY']}}}}}
States which have no cities which match the name of their county.
The opposite of the previous example. Filtering rows would drop needed data; the “zeros” have to be counted.
- project column matching names instead of filtering
- group by state
- aggregate whether there are any matches
- filter for no matches
- access column
execute("""{
project(columns: {alias: "match", eq: [{name: "county"}, {name: "city"}]}) {
group(by: "state", aggregate: {any: {name: "match"}}) {
filter(where: {inv: {name: "match"}}) {
columns {
state {
values
}
}
}
}
}
}"""){'project': {'group': {'filter': {'columns': {'state': {'values': ['DC',
'GA']}}}}}}