0
votes

I have a a graphql query that has a data node that requires all quotation marks to be escaped like so: data: "{\"access_token\": \"SOME_TOKEN\"}".

However, in the program I am writing, SOME_TOKEN is dynamic and thus I need to use a variable to reference the access token. However, because the token is a string, it has quotation marks as part of the variable. I would usually do something like access_token_var.replace(""", "\") but because this is graphql-client, the entire thing is wrapped in a string.

I am getting the error Variable $access_token is declared by __anonymous__ but not used.

How can I escape the quotes that are included in the string variable?

Full query:

 CREATE_AUTHENTICATION_MUTATION ||=  <<-'GRAPHQL'
    mutation($access_token: String!, $refresh_token: String!, $user_id: ID!, $created_at: Int!, $__created: Int!, $expires_in: Int!){
        createUserAuthentication(input: {
            name: "my_test",
            data: "
{\"access_token\": $access_token, \"refresh_token\": $refresh_token, \"user_id\": $user_id, \"instance_url\": \"REDACTED\", \"created_at\": $created_at, \"__created\": $__created, \"token_type\": \"bearer\", \"expires_in\": $expires_in}"
         }){
            authenticationId
            clientMutationId
        }
    }
  GRAPHQL
1

1 Answers

0
votes

Your problem isn't the quotes, it's that data is just a string: no interpolation is taking place and so your resulting value will just be a string literal {"access_token": $access_token, ...}.

GraphQL uses javascript as a backend, so you can probably do something like the following:

...
data: "{\"access_token\": \"" + $access_token + "\", \"refresh_token\": \"" + $refresh_token + "\", \"user_id\": \"" + $user_id + "\", \"instance_url\": \"REDACTED\", \"created_at\": \"" + $created_at + "\", \"__created\": \"" + $__created + "\", \"token_type\": \"bearer\", \"expires_in\": \"" + $expires_in + "\"}"
...

Or even:

...
data: JSON.stringify({access_token: $access_token, refresh_token: $refresh_token, user_id: $user_id, instance_url: "REDACTED", created_at: $created_at, __created: $__created, token_type: "bearer", expires_in: $expires_in})
...

As per the example on here: https://graphql.org/graphql-js/mutations-and-input-types/ (search 'stringify')