simonw / datasette-graphql

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

{{ graphql(query) }} template tag #50

Closed simonw closed 4 years ago

simonw commented 4 years ago

Exposing graphql to templates means custom templates will be able to populate their context using a query embedded in the template itself - something like this:

{% set user = graphql("""
{
  users_row(id: 9599) {
    id
    name
    contributors_list {
      nodes {
        contributions
        repo_id {
          full_name
        }
      }
    }
  }
}
""", "github")["users_row"] %}
simonw commented 4 years ago

Prior art: datasette-template-sql: https://github.com/simonw/museums/blob/18c797958e98da5b21d1993b22ec89c5e9fccb1d/templates/row-browse-museums.html#L68-L78

    <section class="section nearby-section">
        {% for museum in sql("""
            select *, haversine(latitude, longitude, :latitude, :longitude, 'mi') as distance_mi
            from museums
            where id != :id
            and permanently_closed is null order by distance_mi limit 3
        """, {
            "latitude": museum.latitude,
            "longitude": museum.longitude,
            "id": museum.id
        }) %}