I'm having trouble consistently using javascript variables inside graphql-tag queries and mutations when setting up an apollo server. Here's a specific issue I've encountered:
gql`
mutation SetDeviceFirebaseToken {
SetDeviceFirebaseToken(
internalDeviceId: ${internalDeviceId},
firebaseToken: ${firebaseToken}
)
}
`
The variables internalDeviceId
and firebaseToken
are both strings, but I keep receiving the GraphQL syntax error "Expected Name, found...". What is the most effective approach for incorporating JS variables within a graphql-tag query or mutation? For additional context, I am implementing this in an older nativescript-angular application, and below is the complete function where I am attempting to execute 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));
}
To make the above mutation work, I ended up replacing gql
with String.raw
. However, my initial hope was to utilize graphql-tag for this purpose. If I were to directly execute this mutation in Apollo, I would simply pass the strings as follows:
mutation SetDeviceFirebaseToken {
SetDeviceFirebaseToken(
internalDeviceId: "asfas9easefja9sefasef",
firebaseToken: "asefa9sefaefafe"
)
}