Can't pass in variables.
dChunikhin opened this issue · 5 comments
Hello. Help me, please.
gql.query({
operation: 'user',
variables: { id: 333 },
fields: ['name', 'balance'],
}).query;
returns
query ($id: Int) { user (id: $id) { name, balance } }
instead of
query ($id: Int) { user (id: 333) { name, balance } }
Shortly, your feature doesn't inject variables values into resulting query string.
Or, if it is predicted behavior, why you did it? It's expected that we will interpolate variables into query string, aren't we?
Since the query is a string, we could run into malformed query string and will have to escape the data passed, in your case id
to be escaped for any special characters colliding with GQL syntax. Hence we pass the variables as JSON in payload.
You can learn more here: https://graphql.org/learn/queries/#variables
So, then i should interpolate vars from object variables into query string manually, before making request?
Did not understand your question there. You have to just pass your data (variable) to the query/mutation function and it'll automatically form the JSON payload containing the actual GraphQL query string and variables.
For example:
import axios from "axios";
import { mutation } from "gql-query-builder";
async function saveThought() {
try {
const response = await axios.post(
"http://api.example.com/graphql",
mutation({
operation: "thoughtCreate",
variables: {
name: "Tyrion Lannister",
thought: "I drink and I know things."
},
fields: ["id"]
})
);
console.log(response);
} catch (error) {
console.log(error);
}
}
Feel free to let me know if you still have any doubts or questions.
My question was about data in a body of request. But, i have figured out alredy, so thx you for operative help!