question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose programatic API for executing graphql queries/mutations via the `keystone` instance

See original GitHub issue

Update: This feature has been implemented as keystone.executeQuery. See https://v5.keystonejs.com/keystone-alpha/keystone/#keystoneexecutequeryquerystring-options


Original Post:

A suggested API is:

    keystone.createItem('Session', {
      outputFragment: `
        id
        posts {
          id
          title
        }
      `,
      variables: {}
    });

Where createItem matches our GraphQL API naming for create<List> (so we’d have ones like keystone.update<List>/ keystone.delete<List>), and 'Session' is the list we’re operating on.

Internally, this should follow all the same rules as a HTTP request that triggers a query which adds an extra complication: How can we ensure that the Apollo Server injects the correct context and runs the query through all the same error formatting, etc? It doesn’t appear that apollo-graphql exposes a programatic API. Perhaps we have to fake it and do a localhost http request to the listening port internally? Is there an overhead with doing that?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
dcousenscommented, Aug 22, 2019

My own diy patch on a keystone instance takes the form of

keystone.query = function runQuery (query, variables = {}) {
  const context = keystone.getAccessContext(SCHEMA_NAME, {})

  return keystone._graphQLQuery[SCHEMA_NAME](query, context, variables)
    .then(({ errors, data }) => {
      if (errors) throw new Error(errors[0])
      return data
    })
}

Where SCHEMA_NAME comes from, is up to how you configure keystone and your app-graphql. I think it defaults to admin

This returns a Promise, so you can do await keystone.query('query ...', { ... }) etc

2reactions
molombycommented, Aug 21, 2019

Bumping this 👍🏼 – If we want app developers to use the GraphQL API as their primary way of interacting with the DB, we need an easily accessible, programatic interface to the schema. Usually this would be used without access control (although I guess it would be handy to sometimes impersonate a user. I imagine developers often also want to skips any hooks.

I agree with @gautamsi in that, some times you’ll have an existing context (inc. req object, etc.) and might want to reuse or pass it through. Far more often though, whether serving an HTTP request or operating in another context (like a worker), devs will want to query independently from their own context (if that make sense). Put another way, the privileges a user has are very different to the privileges needed by the code that serves that user.

Involving an HTTP stack or mocking up express-style req objects, etc. seems like a very bad idea to me. HTTP is network transport protocol, we’re communicate within a node thread. Conceptually, GraphQL has nothing to do with HTTP; it’s a query language. We should maintain that conceptual separation wherever possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing - Keystone 6 Documentation
The API for run-time functionality in your Keystone system. Use it to write business logic for access control, hooks, testing, GraphQL schema extensions,...
Read more >
4 Simple Ways to Call a GraphQL API
If the query successfully executes and returns the GraphQL data from the server, we can map over the response data and display the...
Read more >
@keystone-6-master/example-extend-graphql-schema ... - npm
This project demonstrates how to extend the GraphQL API provided by Keystone with custom queries and mutations using ...
Read more >
GraphQL - Strapi Developer Docs
The GraphQL API reference describes queries, mutations and parameters you can use to interact with your API using Strapi's GraphQL plugin. # Usage....
Read more >
GraphQL API - Dagster Docs
Dagster exposes a GraphQL API that allows clients to interact with Dagster programmatically. The API allows users to: Query information about Dagster runs,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found