-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for getContext (#7997)
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
126 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: "Get Context" | ||
description: "getContext is a function exported by @keystone-6/core/context to support operations without starting a HTTP server" | ||
--- | ||
|
||
Keystone's command line interface is helpful, but sometimes you don't want to startup the HTTP server, or kickoff any build processes. | ||
Maybe you need to insert data into the database, maybe you want to use Keystone with a custom protocol or a tiny REST API. | ||
*Or maybe you want to write unit tests.* | ||
|
||
If you have previously run `keystone build` or `keystone dev`, then you can use `getContext`. | ||
If you change your configuration, you should [rebuild your project](../guides/cli) before using `getContext`. | ||
|
||
Using the `getContext` function does not use the typical Keystone entry point - it is a function and only requires that your Prisma client has been built and can be provided as a parameter with your Keystone configuration. | ||
|
||
```typescript | ||
import { getContext } from '@keystone-6/core/context'; | ||
import config from './keystone'; | ||
import * as PrismaModule from '.prisma/client'; | ||
|
||
const context = getContext(config, PrismaModule); | ||
|
||
// ... whatever you need | ||
``` | ||
|
||
{% hint kind="tip" %} | ||
For inspiration, see [the script example project](https://github.com/keystonejs/keystone/tree/main/examples/script-field), and how we use [`tsx`](https://github.com/esbuild-kit/tsx) to seed the database with some data. | ||
{% /hint %} | ||
|
||
A context created in this way does not have an implicit `session`, nor is it a `sudo()` context. | ||
For more information about how to use a context, please see the [overview](./overview). | ||
|
||
{% hint kind="warn" %} | ||
The `context` returned from `getContext` is not a global - every time you instantiate it, you are instantiating a Prisma Client. | ||
Without careful usage, you may encounter the warnings that there are too many instances of your Prisma Client. | ||
{% /hint %} | ||
|
||
## Related resources | ||
|
||
{% related-content %} | ||
{% well | ||
heading="Example Project: Script" | ||
href="https://github.com/keystonejs/keystone/tree/main/examples/script" | ||
target="_blank" %} | ||
Shows you how to write tests against the GraphQL API to your Keystone system. Builds on the Authentication example project. | ||
Shows you how to write code to interact with Keystone without using the standard `@keystone-6/core/scripts/cli` tools. | ||
{% /well %} | ||
{% well | ||
heading="Query API Reference" | ||
href="/docs/context/query" %} | ||
A programmatic API for running CRUD operations against your GraphQL API. For each list in your system you get an API at `context.query.<listName>`. | ||
{% /well %} | ||
{% well | ||
heading="Context API Reference" | ||
href="/docs/context/overview" %} | ||
The API for run-time functionality in your Keystone system. Use it to write business logic for access control, hooks, testing, GraphQL schema extensions, and more. | ||
{% /well %} | ||
{% /related-content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Base Project - Script | ||
|
||
This project demonstrates how to write code to interact with Keystone without using the standard `@keystone-6/core/scripts/cli` tools. | ||
|
||
The `getContext` function does not ahdere to the typical Keystone entry-point and requires that your Prisma schema has been previously built by Keystone. | ||
|
||
## Instructions | ||
|
||
To run this project, clone the Keystone repository locally, run `yarn` at the root of this repository, then navigate to this directory and run: | ||
|
||
```shell | ||
yarn dev | ||
``` | ||
|
||
This will start the Admin UI at [localhost:3000](http://localhost:3000). | ||
You can use the Admin UI to create items in your database. | ||
|
||
You can also access a GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql), which allows you to directly run GraphQL queries and mutations. | ||
|
||
Congratulations, you're now up and running with Keystone! 🚀 | ||
|
||
## Try it out in CodeSandbox 🧪 | ||
|
||
You can play with this example online in a web browser using the free [codesandbox.io](https://codesandbox.io/) service. To launch this example, open the URL <https://githubbox.com/keystonejs/keystone/tree/main/examples/script>. You can also fork this sandbox to make your own changes. | ||
|
||
## Next steps | ||
|
||
Experiment with the code in this example to see how Keystone works, familiarise yourself with the Admin UI, and learn about the GraphQL Playground. | ||
|
||
When you’ve got the hang of this base project, try a [feature project](../) to learn Keystone’s advanced features and take your knowledge to the next level. |
065695a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
keystone-next-docs – ./
www.keystonejs.com
keystonejs.com
keystone-next-docs-thinkmill.vercel.app
keystone-next-docs-git-main-thinkmill.vercel.app
keystone-6.vercel.app
next.keystonejs.com
demo.keystonejs.com