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

Nested fields and only resolve when they're queried #1360

Open
alber70g opened this issue Dec 16, 2024 · 1 comment
Open

Nested fields and only resolve when they're queried #1360

alber70g opened this issue Dec 16, 2024 · 1 comment

Comments

@alber70g
Copy link

alber70g commented Dec 16, 2024

I feel like I'm missing something very obvious.

I want to fulfill a query like this one:

query {
  transactions(<some args>){
    edges {
      node {
        sender
        creationtime
        cmd {
          signers {
          id
        }
      }
    }
  }
}

Now I like to only fulfil the signers part when it's requested as it's quite expensive.

So my simple mind thinks I can just do signers: t.field({ resolve() { /* etc */ } }) like:

export default builder.prismaNode(Prisma.ModelName.Transaction, {
  description: 'A transaction.',
  fields: (t) => ({
    // fields sender, creationtime, etc
    // ...
    cmd: t.field({
      resolve(parent) {
         const someResult = fromDB();
         return {
           ...someResult,
// here is what I want to do
           signers: t.field({
             type: [Signer],
             resolve(parent) {
               const signersResult = fromDB();
               return signersResult;
             }
           })
// this doesn't seem to work?
         }
      }
    })

But it seems not to work... What is going on here?

@hayes
Copy link
Owner

hayes commented Dec 16, 2024

GraphQL already works that way by default. It will only run the resolvers when the data is requested.

You can't just define more fields in a resolver though. The schema is generated before resolving queries, if you define the fields inside of a resolver, you aren't defining the fields until you are actually executing a query, which doesn't really make sense.

Just move that field definition to where ever you are defining your cmd type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants