Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove resolveFields #6789

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-plums-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Removed the deprecated `resolveFields` from `context.query`, if you were still using it, you should switch to providing `the query option` to `context.query` or use `context.db` if you were providing `false`. The `context.query` functions will now also throw an error if an empty string is passed to `query` rather than silently returning what the `context.db` functions return, you must select at least one field or omit the `query` option to default to selecting the `id`.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const crudTests = (keystoneTestWrapper: any) => {
await expect(
context.query.Test.createOne({
data: { passwordRejectCommon: 'password' },
query: ``,
})
).rejects.toMatchInlineSnapshot(`
[GraphQLError: You provided invalid data for this operation.
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function makeCreateContext({
const dbAPIFactories = sudo ? sudoDbApiFactories : publicDbApiFactories;
for (const listKey of Object.keys(gqlNamesByList)) {
dbAPI[listKey] = dbAPIFactories[listKey](contextToReturn);
itemAPI[listKey] = itemAPIForList(listKey, contextToReturn, dbAPI[listKey]);
itemAPI[listKey] = itemAPIForList(listKey, contextToReturn);
}
return contextToReturn;
};
Expand Down
56 changes: 13 additions & 43 deletions packages/keystone/src/lib/context/itemAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,32 @@ export function getDbAPIFactory(
) as Record<keyof typeof api, any>;
}

function defaultQueryParam(query?: string, resolveFields?: string | false) {
if (query !== undefined && resolveFields !== undefined) {
throw new Error('query and resolveFields cannot both be passed to an Items API query');
}
if (query !== undefined) return query;
if (resolveFields !== undefined) return resolveFields;
return 'id';
}

/* NOTE
*
* The `resolveFields` param has been deprecated in favor of `query` (when selecting fields to
* query) or the new dbAPI which is available via `context.db.{List}`, which replaces
* the previous `resolveFields: false` behaviour.
*
* We'll be removing the option to use `resolveFields` entirely in a future release.
*/
export function itemAPIForList(
listKey: string,
context: KeystoneContext,
dbAPI: KeystoneDbAPI<Record<string, BaseGeneratedListTypes>>[string]
context: KeystoneContext
): KeystoneListsAPI<Record<string, BaseGeneratedListTypes>>[string] {
const f = (
operation: 'query' | 'mutation',
field: string,
dbAPIVersionOfAPI: (args: any) => Promise<any>
) => {
const f = (operation: 'query' | 'mutation', field: string) => {
const exec = executeGraphQLFieldWithSelection(context.graphql.schema, operation, field);
return ({
query,
resolveFields,
...args
}: { resolveFields?: false | string; query?: string } & Record<string, any> = {}) => {
const returnFields = defaultQueryParam(query, resolveFields);
if (returnFields) {
return exec(args, returnFields, context);
} else {
return dbAPIVersionOfAPI(args);
}
return ({ query, ...args }: { query?: string } & Record<string, any> = {}) => {
const returnFields = query ?? 'id';
return exec(args, returnFields, context);
};
};
const gqlNames = context.gqlNames(listKey);
return {
findOne: f('query', gqlNames.itemQueryName, dbAPI.findOne),
findMany: f('query', gqlNames.listQueryName, dbAPI.findMany),
findOne: f('query', gqlNames.itemQueryName),
findMany: f('query', gqlNames.listQueryName),
async count({ where = {} } = {}) {
const { listQueryCountName, whereInputName } = context.gqlNames(listKey);
const query = `query ($where: ${whereInputName}!) { count: ${listQueryCountName}(where: $where) }`;
const response = await context.graphql.run({ query, variables: { where } });
return response.count;
},
createOne: f('mutation', gqlNames.createMutationName, dbAPI.createOne),
createMany: f('mutation', gqlNames.createManyMutationName, dbAPI.createMany),
updateOne: f('mutation', gqlNames.updateMutationName, dbAPI.updateOne),
updateMany: f('mutation', gqlNames.updateManyMutationName, dbAPI.updateMany),
deleteOne: f('mutation', gqlNames.deleteMutationName, dbAPI.deleteOne),
deleteMany: f('mutation', gqlNames.deleteManyMutationName, dbAPI.deleteMany),
createOne: f('mutation', gqlNames.createMutationName),
createMany: f('mutation', gqlNames.createManyMutationName),
updateOne: f('mutation', gqlNames.updateMutationName),
updateMany: f('mutation', gqlNames.updateManyMutationName),
deleteOne: f('mutation', gqlNames.deleteMutationName),
deleteMany: f('mutation', gqlNames.deleteManyMutationName),
};
}
7 changes: 0 additions & 7 deletions packages/keystone/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ type ResolveFields = {
* @default 'id'
*/
readonly query?: string;
/**
* @deprecated
*
* resolveFields has been deprecated. Please use the `query` param to query fields,
* or `context.db.{List}` instead of passing `resolveFields: false`
*/
readonly resolveFields?: false | string;
};

export type KeystoneDbAPI<KeystoneListsTypeInfo extends Record<string, BaseGeneratedListTypes>> = {
Expand Down