Skip to main content

A GraphQL endpoint for Datasette

Project description

datasette-graphql

PyPI Changelog License

A GraphQL endpoint for Datasette

Read more about this project: GraphQL in Datasette with the new datasette-graphql plugin

Try out a live demo at datasette-graphql-demo.datasette.io/graphql

GraphiQL animated demo

Installation

Install this plugin in the same environment as Datasette.

$ pip install datasette-graphql

Usage

This plugin sets up /graphql as a GraphQL endpoint for the first attached database.

If you have multiple attached databases each will get its own endpoint at /graphql/name_of_database.

Querying for tables and columns

Individual tables (and SQL views) can be queried like this:

{
  repos {
    nodes {
      id
      full_name
      description
    }
  }
}

In this example query the underlying database table is called repos and its columns include id, full_name and description.

Fetching a single record

If you only want to fetch a single record - for example if you want to fetch a row by its primary key - you can use the tablename_get field:

{
  repos_get(id: 107914493) {
    id
    full_name
    description
  }
}

The tablename_get field accepts the primary key column (or columns) as arguments. It also supports the same filter:, search:, sort: and sort_desc: arguments as the tablename field, described below.

Accessing nested objects

If a column is a foreign key to another table, you can request columns from the table pointed to by that foreign key using a nested query like this:

{
  repos {
    nodes {
      id
      full_name
      owner {
        id
        login
      }
    }
  }
}

Accessing related objects

If another table has a foreign key back to the table you are accessing, you can fetch rows from that related table.

Consider a users table which is related to repos - a repo has a foreign key back to the user that owns the repository. The users object type will have a repos_list field which can be used to access those related repos:

{
  users(first: 1, search:"simonw") {
    nodes {
      name
      repos_list(first: 5) {
        totalCount
        nodes {
          full_name
        }
      }
    }
  }
}

Filtering tables

You can filter the rows returned for a specific table using the filter: argument. This accepts a filter object mapping columns to operations. For example, to return just repositories with the Apache 2 license and more than 10 stars:

{
  repos(filter: {license: {eq: "apache-2.0"}, stargazers_count: {gt: 10}}) {
    nodes {
      full_name
      stargazers_count
      license {
        key
      }
    }
  }
}

See table filters examples for more operations, and column filter arguments in the Datasette documentation for details of how those operations work.

Sorting

You can set a sort order for results from a table using the sort: or sort_desc: arguments. The value for this argument should be the name of the column you wish to sort (or sort-descending) by.

{
  repos(sort_desc: stargazers_count) {
    nodes {
      full_name
      stargazers_count
    }
  }
}

Pagination

By default the first 10 rows will be returned. You can control this using the first: argument.

{
  repos(first: 20) {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      full_name
      stargazers_count
      license {
        key
      }
    }
  }
}

The totalCount field returns the total number of records that match the query.

Requesting the pageInfo.endCursor field provides you with the value you need to request the next page. You can pass this to the after: argument to request the next page.

{
  repos(first: 20, after: "134874019") {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      full_name
      stargazers_count
      license {
        key
      }
    }
  }
}

The hasNextPage field tells you if there are any more records.

Search

If a table has been configured to use SQLite full-text search you can execute searches against it using the search: argument:

{
  repos(search: "datasette") {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      full_name
      description
    }
  }
}

The sqlite-utils Python library and CLI tool can be used to add full-text search to an existing database table.

Auto camelCase

The names of your columns and tables default to being matched by their representations in GraphQL.

If you have tables with names_like_this you may want to work with them in GraphQL using namesLikeThis, for consistency with GraphQL and JavaScript conventions.

You can turn on automatic camelCase using the "auto_camelcase" plugin configuration setting in metadata.json, like this:

{
    "plugins": {
        "datasette-graphql": {
            "auto_camelcase": true
        }
    }
}

CORS

This plugin obeys the --cors option passed to the datasette command-line tool. If you pass --cors it adds the following CORS HTTP headers to allow JavaScript running on other domains to access the GraphQL API:

access-control-allow-headers: content-type
access-control-allow-method: POST
access-control-allow-origin: *

Still to come

See issues for a full list. Planned improvements include:

  • Canned query support
  • Ability to allowlist specific tables, views and canned queries

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-graphql
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and tests:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette-graphql-0.9.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

datasette_graphql-0.9-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file datasette-graphql-0.9.tar.gz.

File metadata

  • Download URL: datasette-graphql-0.9.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for datasette-graphql-0.9.tar.gz
Algorithm Hash digest
SHA256 37b02d20571eaecc329f96796d48af25645906927702aa66f1c4b98c323e21a3
MD5 581b4e852a038e74690474e65310352d
BLAKE2b-256 a6fbb0feff363d22f6eb112b28e59dbdd675306f340863b2453a8789fb1b880c

See more details on using hashes here.

File details

Details for the file datasette_graphql-0.9-py3-none-any.whl.

File metadata

  • Download URL: datasette_graphql-0.9-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for datasette_graphql-0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5159a71a69e904c2422d059469aeee74cc7c18062b4ce48c01998c5331312f15
MD5 ef6ed16ab71c4c79a2fa32584f2fc275
BLAKE2b-256 0632324306f8cef9c0b45a46a1ba12907a1e0c531f5a0871bbe97c031ddceec8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page