A GraphQL endpoint for Datasette
Project description
datasette-graphql
A GraphQL endpoint for Datasette
Try out a live demo at datasette-graphql-demo.datasette.io/graphql.
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
.
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
}
}
}
}
Filtering tables
You can filter the rows returned for a specific table using the filters:
argument. This accepts a list of filters, where a filter is a string of the form column=value
or column__op=value
. For example, to return just repositories with the Apache 2 license and more than 10 stars:
{
repos(filters: ["license=apache-2.0", "stargazers_count__gt=10"]) {
nodes {
full_name
stargazers_count
license {
key
}
}
}
}
This is the same format used for querystring arguments to the Datasette table view, see column filter arguments in the Datasette documentation.
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
By default this plugin 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: *
You can turn these headers off using the disable_cors
plugin setting:
{
"plugins": {
"datasette-graphql": {
"disable_cors": true
}
}
}
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file datasette-graphql-0.5.tar.gz
.
File metadata
- Download URL: datasette-graphql-0.5.tar.gz
- Upload date:
- Size: 9.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4ec47f9ad286c32aad3a4f3724d8134c6a9fea3f229c43214c230a4c1031937 |
|
MD5 | cf64692b794213c4299f436c1ee633c4 |
|
BLAKE2b-256 | 3c7560fb92f87e8eba9c7c8dc545623a93b4b8467d0f486515fca004a793143e |
File details
Details for the file datasette_graphql-0.5-py3-none-any.whl
.
File metadata
- Download URL: datasette_graphql-0.5-py3-none-any.whl
- Upload date:
- Size: 8.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9f8d3afc72c8e42dcf5d4b8d03b1a54d5ab8e2a7de8b0df2aaa0d3dbe044ea3 |
|
MD5 | ae88155d6f69248ced6f9cc7babb8170 |
|
BLAKE2b-256 | 3a596975aac169ff99aff69b602a20fc0a63c0f72a34c5feae673afd090c0f0d |