Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Resolve two GraphQL schema fields using one endpoint

There's a situation where there are two possible types to fill data property. I have made a union type for that (ComponentItem) to determine which field needs to be returned. The first schema (ComponentItem1) should just be a hardcoded list but the second one (ComponentItem2) is more dynamic where it gets a searchTerm from query and it actually calls an endpoint to fill the list and also has hasNextPage property.

Here are the schemas I made:

type Component {
  id
  title
  data: ComponentItem
}
union ComponentItem = ComponentItem1 | ComponentItem2
type ComponentItem1 {
    list: [List!]!
}

type ComponentItem2 {
    hasNextPage: Boolean;
    list: [List!]!
}

In the resolver I'm resolving the union type in order to generate proper __typename:

const resolver: {
    ComponentItem: {
        __resolveType(object) {
            if(object.searchTerm){
               return "ComponentItem2"
            }
            return "ComponentItem1"
        },
    },
}

What I'm currently doing is to resolve the list and hasNextPage individually but in this scenario I'm sending the request to the same endpoint twice.

const resolver = {
   ...resolver,
   ComponentItem2: {
     list: async (root, __, context) => {
         const result = await fetch('search-endpoint')
         return result?.items || []
     }
     hasNextPage: async (root, __, context) => {
         const result = await fetch('search-endpoint')
         return result?.hasNextPage || false
     }

   }
   
}

My question is how it is possible to share that result in another field resolver (Other than using "context"). Or if there's any better way to handle this situation let me know.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!