Skip to content

Instantly share code, notes, and snippets.

@socialwell-demos
Created June 9, 2022 09:52
Show Gist options
  • Save socialwell-demos/71ecf460f5ed1bd07d129636453c10d0 to your computer and use it in GitHub Desktop.
Save socialwell-demos/71ecf460f5ed1bd07d129636453c10d0 to your computer and use it in GitHub Desktop.
Keystone Virtual Field
import { list, graphql } from '@keystone-6/core';
import {
text,
virtual,
} from '@keystone-6/core/fields';
export const Producer = list({
fields: {
firstName: text({ validation: { isRequired: true }, isFilterable: true }),
middleName: text({ isFilterable: true }),
lastName: text({ isFilterable: true }),
fullName: virtual({
field: graphql.field({
type: graphql.String,
resolve: (item: any) =>
`${item.firstName} ${item.middleName} ${item.lastName}`,
}),
}),
orgName: virtual({
field: graphql.field({
type: graphql.String,
async resolve(item, args, context) {
const { org } = await context.query.Producer.findOne({
where: { id: item.id.toString() },
query: 'org { orgName }',
});
return org && org.orgName;
},
}),
}),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment