Example Queries
In [1]:
Copied!
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)
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)
View schema.¶
In [2]:
Copied!
execute("""{
count
schema {
names
types
partitioning
}
}""")
execute("""{
count
schema {
names
types
partitioning
}
}""")
Out[2]:
{'count': 41700,
'schema': {'names': ['latitude',
'longitude',
'state',
'city',
'county',
'zipcode'],
'types': ['float64', 'float64', 'string', 'string', 'string', 'int32'],
'partitioning': []}}
Find California counties with the most cities.¶
filterstate by "CA"groupby county- aggregate distinct cities
projectcity countorderby city counts descending- access
columnscountyis still known in the schema- cities is a new
columnaccessed through an inline fragment
In [3]:
Copied!
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
}
}
}
}
}
}
}""")
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
}
}
}
}
}
}
}""")
Out[3]:
{'filter': {'group': {'project': {'order': {'columns': {'county': {'values': ['Los Angeles',
'San Bernardino',
'San Diego',
'Orange',
'Riverside']}},
'cities': {'values': [132, 73, 52, 47, 42]}}}}}}
Find states with cities which match the name of their county.¶
filterusingwhereexpression, because comparing two columns is not a "simple" queryColumn.distinctinstead ofgroup, because no other aggregates are needed
In [4]:
Copied!
execute("""{
filter(where: {eq: [{name: "county"}, {name: "city"}]}) {
columns {
state {
distinct {
values
}
}
}
}
}""")
execute("""{
filter(where: {eq: [{name: "county"}, {name: "city"}]}) {
columns {
state {
distinct {
values
}
}
}
}
}""")
Out[4]:
{'filter': {'columns': {'state': {'distinct': {'values': ['VA',
'OH',
'IN',
'WI',
'MO',
'NE',
'LA',
'NM',
'HI',
'OR',
'VT',
'WV',
'NC',
'FL',
'MN',
'KS',
'AR',
'OK',
'TX',
'ID',
'AK',
'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.
projectcolumn matching names instead of filteringgroupby state- aggregate whether there are
anymatches
- aggregate whether there are
filterfor no matches- access column
In [5]:
Copied!
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
}
}
}
}
}
}""")
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
}
}
}
}
}
}""")
Out[5]:
{'project': {'group': {'filter': {'columns': {'state': {'values': ['GA',
'DC']}}}}}}