Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
How to go about using javascript variables inside a graphql-tag mutation

I'm using graphql-tag to setup queries and mutations to an apollo server and haven't been able to use javascript variables inside the tag consistently or successfully. Here's an example:

gql`
  mutation SetDeviceFirebaseToken {
    SetDeviceFirebaseToken(
      internalDeviceId: ${internalDeviceId},
      firebaseToken: ${firebaseToken}
      )
    }
`

internalDeviceId and firebaseToken are just strings, but I keep getting the GraphQL syntax error "Expected Name, found...". What's the best way to go about using JS variables inside a graphql-tag query or mutation? For context, I'm setting this up in an older nativescript-angular app and here is the full function that is attempting to send the mutation:

/**
   * Call SetDeviceFirebaseToken mutation
   *
   * @param {string} internalDeviceId
   * @param {string} firebaseToken
   */
  public setDeviceFirebaseToken(internalDeviceId: string, firebaseToken: string, jwt: string): Observable<Object> {
    return this.http.post(this._carebearApiUrl, {
      query: gql`
        mutation SetDeviceFirebaseToken {
          SetDeviceFirebaseToken(
            internalDeviceId: ${internalDeviceId},
            firebaseToken: ${firebaseToken}
            )
          }
      `
    }, this.graphqlRequestHeaders(jwt));
  }

I can make the above mutation work by just swapping out gql with String.raw, but I was hoping to utilize graphql-tag for this. If I were to execute this mutation in Apollo directly, I would just pass the strings like so:

mutation SetDeviceFirebaseToken {
  SetDeviceFirebaseToken(
    internalDeviceId: "asfas9easefja9sefasef",
    firebaseToken: "asefa9sefaefafe"
    )
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

why don't you pass the parameters into the mutation function itself, e.g

gql"mutation SetDeviceFirebaseToken($internalDeviceId: String!, $firebaseToken: String!) { SetDeviceFirebaseToken(internalDeviceId: $internalDeviceId, firebaseToken: $firebaseToken) .... .... } Then passing the variable from the client-side like this:

{
"internalDeviceId": "something",
"firebaseToken": "something"
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

GraphQL probably expects a string like this:

`
internalDeviceId: "a-nice-uuid"
`

But you are instead giving it this:

`
internalDeviceId: a-nice-uuid
`

You missed out on the quotes around the template literal:

`
internalDeviceId: "${internalDeviceId}"
`
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda