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

119
Visualizações
Dynamically create graphql query based on criterias from javascript

I am using GraphQl together with bigcommerce to grab products, and i need to find a sollution to make my query dynamic - its based on the length of an array of SKU's. Its no problem to make it work with one product.

let query = `
 query getProductBySku($sku: String) {
  site {
   product(sku: $sku) {
    id
    entityId
    name
    sku
    }
   }
  }
 `

And i figured out so far that i need to use something like aliases in graphql to be able to get several products in several response like this

 query productById {
  site {
   o1: product(entityId: 167) {
    id
    entityId
    sku
    name
   }
  o2: product(entityId: 168 ) {
   id
   entityId
   sku
   name
  }
 }
}

But i need to be able to make this alias query dynamic based on the lenght of the array. How would you approach to solve this problem? Is it possible to maybe create a loop that will write out the query before i will run it. Any help to let me in the right direction here would be highly appreciated! Thank you!

ps. im using javascript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For anyone curious i ended up being able to use .map to dynamically loop out the query and the run it :)

let query = `
 query productBySku {
  site {
   ${sku
    .map(
      (product: any, idx: number) =>
       `pro${idx}: product(sku: "${product}") {
       id
       entityId
       sku
       name
      }
     `
    )
  .join('')}
 }
}

`

about 4 years ago · Juan Pablo Isaza Relatório
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