Skip to content

Commit

Permalink
Relationship field updates (keystonejs#6514)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored and Nikitoring committed Sep 14, 2021
1 parent 745f047 commit 1075ac1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-terms-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Removed `defaultValue` and the undocumented `withMeta` option from the `relationship` field. To re-create `defaultValue`, you can use `resolveInput` though note that if you're using autoincrement ids, you need to return the id as number, not a string like you would provide to GraphQL, e.g. `{ connect: { id: 1 } }` rather than `{ connect: { id: "1" } }`. If you were using `withMeta: false`, please open an issue with your use case.
11 changes: 9 additions & 2 deletions examples-staging/ecommerce/schemas/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ export const Product = list({
price: integer(),
user: relationship({
ref: 'User.products',
defaultValue: ({ context }) =>
context.session?.itemId ? { connect: { id: context.session?.itemId } } : null,
hooks: {
resolveInput({ operation, resolvedData, context }) {
// Default to the currently logged in user on create.
if (operation === 'create' && !resolvedData.user && context.session?.itemId) {
return { connect: { id: context.session?.itemId } };
}
return resolvedData.user;
},
},
}),
},
});
13 changes: 10 additions & 3 deletions examples-staging/roles/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ export const lists = createSchema({
fieldMode: args => (permissions.canManageAllTodos(args) ? 'edit' : 'read'),
},
},
// Always default new todo items to the current user; this is important because users
// without canManageAllTodos don't see this field when creating new items
defaultValue: ({ context: { session } }) => ({ connect: { id: session.itemId } }),
hooks: {
resolveInput({ operation, resolvedData, context }) {
if (operation === 'create' && !resolvedData.assignedTo) {
// Always default new todo items to the current user; this is important because users
// without canManageAllTodos don't see this field when creating new items
return { connect: { id: context.session.itemId } };
}
return resolvedData.assignedTo;
},
},
}),
},
}),
Expand Down
24 changes: 15 additions & 9 deletions examples/default-values/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ export const lists = createSchema({
assignedTo: relationship({
ref: 'Person.tasks',
many: false,
// Dynamic default: Find an anonymous user and assign the task to them
defaultValue: async ({ context }) => {
const anonymous = await context.lists.Person.findMany({
where: { name: { equals: 'Anonymous' } },
});
if (anonymous.length > 0) {
return { connect: { id: anonymous[0].id } };
}
// If we don't have an anonymous user return undefined so as not to apply any default
hooks: {
// Dynamic default: Find an anonymous user and assign the task to them
async resolveInput({ context, operation, resolvedData }) {
if (operation === 'create' && !resolvedData.assignedTo) {
const anonymous = await context.db.lists.Person.findMany({
where: { name: { equals: 'Anonymous' } },
});
if (anonymous.length > 0) {
return { connect: { id: anonymous[0].id } };
}
}
// If we don't have an anonymous user we return the value
// that was passed in(which might be nothing) so as not to apply any default
return resolvedData.assignedTo;
},
},
}),
// Dynamic default: We set the due date to be 7 days in the future
Expand Down
37 changes: 12 additions & 25 deletions packages/keystone/src/fields/types/relationship/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
fieldType,
graphql,
AdminMetaRootVal,
FieldDefaultValue,
} from '../../../types';
import { resolveView } from '../../resolve-view';

Expand Down Expand Up @@ -56,16 +55,12 @@ export type RelationshipFieldConfig<TGeneratedListTypes extends BaseGeneratedLis
ui?: {
hideCreate?: boolean;
};
defaultValue?: FieldDefaultValue<Record<string, any>, TGeneratedListTypes>;
withMeta?: boolean;
} & (SelectDisplayConfig | CardsDisplayConfig | CountDisplayConfig);

export const relationship =
<TGeneratedListTypes extends BaseGeneratedListTypes>({
many = false,
ref,
defaultValue,
withMeta = true,
...config
}: RelationshipFieldConfig<TGeneratedListTypes>): FieldTypeFunc =>
meta => {
Expand Down Expand Up @@ -173,23 +168,18 @@ export const relationship =
return value.findMany(args);
},
}),
extraOutputFields: withMeta
? {
[`${meta.fieldKey}Count`]: graphql.field({
type: graphql.Int,
args: {
where: graphql.arg({ type: graphql.nonNull(listTypes.where), defaultValue: {} }),
},
resolve({ value }, args) {
return value.count({
where: args.where,
});
},
}),
}
: {},
__legacy: {
defaultValue,
extraOutputFields: {
[`${meta.fieldKey}Count`]: graphql.field({
type: graphql.Int,
args: {
where: graphql.arg({ type: graphql.nonNull(listTypes.where), defaultValue: {} }),
},
resolve({ value }, args) {
return value.count({
where: args.where,
});
},
}),
},
});
}
Expand Down Expand Up @@ -227,8 +217,5 @@ export const relationship =
return value();
},
}),
__legacy: {
defaultValue,
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,6 @@ describe('Type Generation', () => {
foo(where: ZipWhereInput! = {}, orderBy: [ZipOrderByInput!]! = [], take: Int, skip: Int! = 0): [Zip!]
fooCount(where: ZipWhereInput! = {}): Int
}"
`);
});

test('to-many relationships can have meta disabled', () => {
const schema = getSchema(relationship({ many: true, ref: 'Zip', withMeta: false }));

expect(printType(schema.getType('Test')!)).toMatchInlineSnapshot(`
"type Test {
id: ID!
foo(where: ZipWhereInput! = {}, orderBy: [ZipOrderByInput!]! = [], take: Int, skip: Int! = 0): [Zip!]
}"
`);
});
});
Expand Down

0 comments on commit 1075ac1

Please sign in to comment.