Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda