How to fetch and pass the dynamic data into the custom field (Keystone.js 6)?

51 views Asked by At

I need to pass some dynamic data from another list field into the my custom field. Let say I need to improve existing keystone example with the Rating custom field and pass into it some User items.

So I've created the "myCustomProperty" property and trying to pass Users data into the component. If I passing the static data, then it's ok and I can render this data into the component. But I if try to fetch Users data dynamically, I faces with errors like context is undefined or something like that. I understand that on this stage I can't access the context, but what the correct way to pass dynamic data into the custom field?

Below I wrote the example, how I tried to realize my idea.


const fetchPosts = async () => {
  return await context.query.User.findMany({
    query: 'id title',
  });
};

export const lists: Lists = {
  Post: list({
    access: allowAll,
    fields: {
      rating: stars({
        ui: {
          description: 'A star rating, with a scale of 5',
        },
        // myCustomProperty: 'Text is passed successfully',
        myCustomProperty: fetchPosts(),
      }),
      },
    },
  }),
  User: list({
    access: allowAll,
    fields: {
      title: text(),
    },
  }),
});

Also I tried to get access to context through the "getContext" function:

import { getContext } from '@keystone-6/core/context';
import config from './keystone';
import * as PrismaModule from '.prisma/client';
const context = getContext(config, PrismaModule);

But the imported config from the keystone.ts returns me the "undefined" value.

I read the documentation many times and did not find any solution. I found the list hooks and the field hooks. But it's working only with the action(right before or after the click on button).

But I want to fetch the Users data by default without any actions.

0

There are 0 answers