#Query Virtual Fields in GraphQL

1 messages · Page 1 of 1 (latest)

rapid kayak
#

I've gut a virtual field, that get's it's data in the afterRead hook

{
      name: 'events',
      type: 'relationship',
      relationTo: 'events',
      admin: {
        readOnly: true,
      },
      virtual: true,
      hooks: {
        afterRead: [
          async ({ value, req }) => {
            // Format date for display
            const events = await req.payload.find({
              collection: 'events',
            })
            return events.docs
          },
        ],
      },
    },

This is in a block, which will be used in a Blocks field.

In the rest api, I do get the desired output. In the graphql Playground, I get the correct type completion, but the result is null

Am I using the wrong hook? Is this even possible at all with graphql?

Thanks!

loud oceanBOT
rapid kayak
#

When logging the result in the hook

console.log(`Got ${events.docs.length} events`)

And running the query, I see the logs in the console ...

#

Interestingly, I also don't see any results in the UI

spark seal
#

are you prehaps looking for a join field instead?

#

I'm otherwise confused by the block setup

rapid kayak
#

Perhaps I need to elaborate a bit

I've got a pages collection, in which is a blocks field
I've also got av events collection, in which I have various events. I don't want the events to be limited to one page though

I now want to have a block I can put on a page, that returns all events that are in the future, so I dont have to manually add or remove them from the block

rapid kayak
#

I mean, if there is a better solution using join i'm not aware of, I'll gladly use that instead of this hook thing 😄

spark seal
#

this seems very hacky