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

Include foreign keys in suggested table queries #67

Closed simonw closed 3 years ago

simonw commented 3 years ago

https://datasette-graphql-demo.datasette.io/github/contributors links to this example query: https://datasette-graphql-demo.datasette.io/graphql/github?query=%7B%0A%20%20contributors%20%7B%0A%20%20%20%20totalCount%0A%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20endCursor%0A%20%20%20%20%7D%0A%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20contributions%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D

{
  contributors {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      contributions
    }
  }
}

It omits the foreign keys which for a many-to-many table are the most interesting things about it!

simonw commented 3 years ago

Here's a better query, using the label logic:

{
  contributors {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      contributions
      repo_id {
        id
        name
      }
      user_id {
        id
        name
      }
    }
  }
}
simonw commented 3 years ago

await db.label_column_for_table(table) is the documented method for finding the label column (if one exists): https://docs.datasette.io/en/stable/internals.html#database-introspection

simonw commented 3 years ago

Demo: https://datasette-graphql-demo.datasette.io/github/issue_comments now has a link in the cog table actions menu to this: https://datasette-graphql-demo.datasette.io/graphql/github?query=%7B%0A%20%20issue_comments%20%7B%0A%20%20%20%20totalCount%0A%20%20%20%20pageInfo%20%7B%0A%20%20%20%20%20%20hasNextPage%0A%20%20%20%20%20%20endCursor%0A%20%20%20%20%7D%0A%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20html_url%0A%20%20%20%20%20%20issue_url%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20node_id%0A%20%20%20%20%20%20created_at%0A%20%20%20%20%20%20updated_at%0A%20%20%20%20%20%20author_association%0A%20%20%20%20%20%20body%0A%20%20%20%20%20%20reactions%0A%20%20%20%20%20%20performed_via_github_app%0A%20%20%20%20%20%20user%20%7B%0A%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20issue%20%7B%0A%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D

{
  issue_comments {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      html_url
      issue_url
      id
      node_id
      created_at
      updated_at
      author_association
      body
      reactions
      performed_via_github_app
      user {
        id
        name
      }
      issue {
        id
        title
      }
    }
  }
}
simonw commented 3 years ago

The demo on https://datasette-graphql-demo.datasette.io/fixtures/repos shows how it correctly converts columns with invalid-to-GraphQL names like $key into a safe GraphQL name _key:

{
  repos {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      id
      full_name
      name
      tags
      owner {
        id
        name
      }
      license {
        _key
        name
      }
    }
  }
}
simonw commented 3 years ago

Demo of the fix: https://datasette-graphql-demo.datasette.io/graphql/fixtures?query=%7B%0A%20%20%20%20users_in%3A%20users(filter%3A%20%7Bname%3A%20%7Bin%3A%20%5B%22cleopaws%22%5D%7D%7D)%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20users_in_integers%3A%20users(filter%3A%20%7Bid%3A%20%7Bin%3A%20%5B1%2C%202%5D%7D%7D)%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20users_notin%3A%20users(filter%3A%20%7Bname%3A%20%7Bnotin%3A%20%5B%22cleopaws%22%5D%7D%7D)%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20users_notin_integers%3A%20users(filter%3A%20%7Bid%3A%20%7Bnotin%3A%20%5B2%5D%7D%7D)%20%7B%0A%20%20%20%20%20%20%20%20nodes%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A