simonw / datasette-graphql

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

Expose the GraphQL schema somewhere #41

Closed simonw closed 4 years ago

simonw commented 4 years ago

From this Twitter conversation I learned how to output a GraphQL schema: https://twitter.com/simonw/status/1292193658700144640

Example output here: https://gist.github.com/simonw/21b2f0878d4e4e20d514b8544b296393 - a snippet:

schema {
  query: Query
}

input IntegerOperations {
  eq: Int
  not: Int
  gt: Int
  gte: Int
  lt: Int
  lte: Int
  in: [Int]
  notin: [Int]
  arraycontains: Int
  isnull: Boolean
  notnull: Boolean
  isblank: Boolean
  notblank: Boolean
}

type PageInfo {
  endCursor: String
  hasNextPage: Boolean
}

type Query {
  users(filter: [usersFilter], where: String, first: Int, after: String, sort: usersSort, sort_desc: usersSortDesc, search: String): usersCollection
  users_get(id: Int, filter: [usersFilter], where: String, after: String, sort: usersSort, sort_desc: usersSortDesc, search: String): users
  repos(filter: [reposFilter], where: String, first: Int, after: String, sort: reposSort, sort_desc: reposSortDesc, search: String): reposCollection
  repos_get(id: Int, filter: [reposFilter], where: String, after: String, sort: reposSort, sort_desc: reposSortDesc, search: String): repos
  licenses(filter: [licensesFilter], where: String, first: Int, after: String, sort: licensesSort, sort_desc: licensesSortDesc, search: String): licensesCollection
simonw commented 4 years ago

Here's partial code for outputting it:

from graphql import print_schema

    schema = await schema_for_database(datasette, database=database)

    if request.args.get("schema"):
        return Response.text(print_schema(schema))
simonw commented 4 years ago

According to https://github.com/graphql/graphql-spec/issues/203#issuecomment-242140024 the internal standard file extension for these at Facebook is .graphql.

I'm going to use the URL to the database-specific endpoint with graphql on the end for this, e.g.

https://datasette-graphql-demo.datasette.io/graphql/github.graphql

simonw commented 4 years ago

Demo: https://datasette-graphql-demo.datasette.io/graphql/github.graphql