Skip to content

Commit

Permalink
Remove legacy keystone.executeGraphQL (#5247)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 26, 2021
1 parent b8cd13f commit 2a1fc41
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-jobs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone-legacy': major
---

Removed legacy method `keystone.executeGraphQL()`.
4 changes: 2 additions & 2 deletions packages/fields/src/types/Virtual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ keystone.createList('Example', {
### Server-side queries

The `item` argument to the resolver function is the raw database representation of the item, so related items will not be directly available on this object.
If you need to access data beyond what lives on the `item` you can execute a [server-side GraphQL query](/docs/discussions/server-side-graphql.md) using `context.executeGraphQL()`.
If you need to access data beyond what lives on the `item` you can execute a [server-side GraphQL query](/docs/discussions/server-side-graphql.md) using `context.graphql.raw()`.

```js
const { Virtual } = require('@keystone-next/fields-legacy');
Expand All @@ -114,7 +114,7 @@ keystone.createList('Example', {
virtual: {
type: Virtual,
resolver: async (item, args, context) => {
const { data, errors } = await context.executeGraphQL({ query: `{ ... }` })
const { data, errors } = await context.graphql.raw({ query: `{ ... }` })
...
}
},
Expand Down
25 changes: 10 additions & 15 deletions packages/keystone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ _**Default:**_ `['public']`

## Methods

| Method | Description |
| ---------------- | ------------------------------------------------------------------- |
| `connect` | Manually connect to Adapter. |
| `createList` | Add a list to the `Keystone` schema. |
| `disconnect` | Disconnect from the adapter. |
| `prepare` | Manually prepare `Keystone` middlewares. |
| `createContext` | Create a `context` object that can be used with `executeGraphQL()`. |
| `executeGraphQL` | Execute a server-side GraphQL operation within the given context. |
| Method | Description |
| --------------- | ------------------------------------------------------------------------ |
| `connect` | Manually connect to Adapter. |
| `createList` | Add a list to the `Keystone` schema. |
| `disconnect` | Disconnect from the adapter. |
| `prepare` | Manually prepare `Keystone` middlewares. |
| `createContext` | Create a `context` object that can be used with `context.graphql.raw()`. |

<!--
## Super secret methods
Expand Down Expand Up @@ -182,7 +181,7 @@ For more information about the first four arguments, please see the [Apollo docs

### `createContext({ schemaName, authentication, skipAccessControl })`

Create a `context` object that can be used with `executeGraphQL()`.
Create a `context` object that can be used with `context.graphql.raw()`.

#### Usage

Expand All @@ -193,7 +192,7 @@ const { gql } = require('apollo-server-express');
const context = keystone.createContext().sudo()

// Execute a GraphQL operation with no access control
const { data, errors } = await keystone.executeGraphQL({ context, query: gql` ... `, variables: { ... }})
const { data, errors } = await context.graphql.raw({ context, query: gql` ... `, variables: { ... }})
```

#### Config
Expand All @@ -204,10 +203,6 @@ const { data, errors } = await keystone.executeGraphQL({ context, query: gql` ..
| `authentication` | `Object` | `{}` | `{ item: { id }, listAuthKey: "" }`. Specifies the item to be used in access control checks. |
| `skipAccessControl` | `Boolean` | `false` | Set to `true` to skip all access control checks. |

### `executeGraphQL({ context, query, variables })`

Execute a server-side GraphQL query within the given context.

#### Usage

```javascript
Expand All @@ -217,7 +212,7 @@ const { gql } = require('apollo-server-express');
const context = keystone.createContext().sudo()

// Execute a GraphQL operation with no access control
const { data, errors } = await keystone.executeGraphQL({ context, query: gql` ... `, variables: { ... }})
const { data, errors } = await context.graphql.raw({ query: gql` ... `, variables: { ... }})
```

#### Config
Expand Down
22 changes: 0 additions & 22 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { gql } = require('apollo-server-express');
const memoize = require('micro-memoize');
const { execute } = require('graphql');
const { GraphQLUpload } = require('graphql-upload');
const { objMerge, flatten, unique, filterValues } = require('@keystone-next/utils-legacy');
const {
Expand Down Expand Up @@ -152,31 +151,10 @@ module.exports = class Keystone {
} = {}) => this.createContext({ schemaName, authentication, skipAccessControl });
context.sudo = () =>
this.createContext({ schemaName, authentication, skipAccessControl: true });
context.executeGraphQL = ({ context = defaults.context, query, variables }) =>
this.executeGraphQL({ context, query, variables });
context.gqlNames = listKey => this.lists[listKey].gqlNames;
return context;
}

executeGraphQL({ context, query, variables }) {
if (!context) {
context = this.createContext({});
}

const schema = this._schemas[context.schemaName];
if (!schema) {
throw new Error(
`No executable schema named '${context.schemaName}' is available. Have you setup '@keystone-next/app-graphql-legacy'?`
);
}

if (typeof query === 'string') {
query = gql(query);
}

return execute(schema, query, null, context, variables);
}

createList(key, config, { isAuxList = false } = {}) {
const { getListByKey, adapter } = this;
const isReservedName = !isAuxList && key[0] === '_';
Expand Down
4 changes: 2 additions & 2 deletions packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"express": "^4.17.1",
"express-pino-logger": "^6.0.0",
"falsey": "^1.0.0",
"graphql": "^15.5.0",
"graphql-type-json": "^0.3.2",
"graphql-upload": "^11.0.0",
"micro-memoize": "^4.0.9",
Expand All @@ -29,7 +28,8 @@
},
"devDependencies": {
"@keystone-next/fields-legacy": "^23.1.0",
"@keystone-next/test-utils-legacy": "*"
"@keystone-next/test-utils-legacy": "*",
"graphql": "^15.5.0"
},
"repository": "https://github.com/keystonejs/keystone/tree/master/packages/keystone"
}
8 changes: 4 additions & 4 deletions packages/server-side-graphql-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ title: Server-side graphQL client

# Server-side graphQL client

This library provides wrapper functions around `keystone.executeGraphQL` to make it easier to run server-side queries and mutations without needing to write boilerplate GraphQL.
This library provides wrapper functions around `context.graphql.raw` to make it easier to run server-side queries and mutations without needing to write boilerplate GraphQL.

Creating a new `User` item would be written as follows using `keystone.executeGraphQL`:
Creating a new `User` item would be written as follows using `context.graphql.raw`:

```js
const { data, errors } = await keystone.executeGraphQL({
const { data, errors } = await context.graphql.raw({
query: `mutation ($item: createUserInput){
createUser(data: $item) {
id
Expand All @@ -35,7 +35,7 @@ const user = await createItem({
});
```

There are three key differences between `keystone.executeGraphQL` and `createItem` (and other functions from this package):
There are three key differences between `context.graphql.raw` and `createItem` (and other functions from this package):

1. If there is an error, `createItem` will be thrown as an exception, rather than providing the error as a return value.
2. `createItem` runs with _access control disabled_. This is suitable for use cases such as seeding data or other server side scripts where the query is triggered by the system, rather than a specific user. This can be controlled with the `context` option.
Expand Down

1 comment on commit 2a1fc41

@vercel
Copy link

@vercel vercel bot commented on 2a1fc41 Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.