CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 12:42:35 GMT
server: Fly/6f91d33b9d (2025-10-08)
content-type: text/html; charset=utf-8
content-encoding: gzip
via: 2 fly.io, 2 fly.io
fly-request-id: 01K79MPQ8SSBYGGDE51TMQJ0V4-bom
GraphQL fragments | Simon Willison’s TILs
GraphQL fragments
One of the scripts that builds and deploys datasette.io uses a GraphQL query to retrieve information from GitHub about the repositories used for the various Datasette tools and plugins.
That query was very big - over 4,000 lines long!
That's because it was shaped like this:
{
repo_0: repository(name: "datasette-query-history", owner: "bretwalker") {
id
nameWithOwner
createdAt
openGraphImageUrl
usesCustomOpenGraphImage
defaultBranchRef {
target {
oid
}
}
repositoryTopics(first: 100) {
totalCount
nodes {
topic {
name
}
}
}
openIssueCount: issues(states: [OPEN]) {
totalCount
}
closedIssueCount: issues(states: [CLOSED]) {
totalCount
}
releases(last: 1) {
totalCount
nodes {
tagName
}
}
}
# ...
repo_137: repository(name: "yaml-to-sqlite", owner: "simonw") {
id
nameWithOwner
createdAt
openGraphImageUrl
usesCustomOpenGraphImage
defaultBranchRef {
target {
oid
}
}
repositoryTopics(first: 100) {
totalCount
nodes {
topic {
name
}
}
}
openIssueCount: issues(states: [OPEN]) {
totalCount
}
closedIssueCount: issues(states: [CLOSED]) {
totalCount
}
releases(last: 1) {
totalCount
nodes {
tagName
}
}
}
}
That block was repeated for every repository - 138 in total!
I figured there was likely a way to do this more efficiently, and it turns out there is: GraphQL fragments.
Here's that example query rewritten to use fragments instead:
fragment repoFields on Repository {
id
nameWithOwner
createdAt
openGraphImageUrl
usesCustomOpenGraphImage
defaultBranchRef {
target {
oid
}
}
repositoryTopics(first: 100) {
totalCount
nodes {
topic {
name
}
}
}
openIssueCount: issues(states: [OPEN]) {
totalCount
}
closedIssueCount: issues(states: [CLOSED]) {
totalCount
}
releases(last: 1) {
totalCount
nodes {
tagName
}
}
}
{
repo_0: repository(name: "datasette-query-history", owner: "bretwalker") {
...repoFields
}
repo_137: repository(name: "yaml-to-sqlite", owner: "simonw") {
...repoFields
}
}
Now each additional repo added to the query is only 3 extra lines of GraphQL, not 30!
Related
- github Bulk fetching repository details with the GitHub GraphQL API - 2021-01-17
- github Searching for repositories by topic using the GitHub GraphQL API - 2020-10-09
- graphql Using curl to run GraphQL queries from the command line - 2022-02-21
- graphql get-graphql-schema - 2022-02-01
- github Accessing repository dependencies in the GitHub GraphQL API - 2020-04-30
- fly Using the undocumented Fly GraphQL API - 2022-01-21
- github Paginating through the GitHub GraphQL API with Python - 2020-07-09
- git Rewriting a repo to contain the history of just specific files - 2022-03-22
- clickhouse Querying the GitHub archive with the ClickHouse playground - 2022-12-31
- datasette Exploring Baseline with Datasette Lite - 2023-05-12
Created 2022-09-30T15:47:20-07:00 · Edit