[GH-ISSUE #15] Can't pass in variables. #9

Closed
opened 2026-03-03 00:02:41 +03:00 by kerem · 5 comments
Owner

Originally created by @dChunikhin on GitHub (Jul 2, 2019).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/15

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.

Originally created by @dChunikhin on GitHub (Jul 2, 2019). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/15 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.
kerem 2026-03-03 00:02:41 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@dChunikhin commented on GitHub (Jul 2, 2019):

Or, if it is predicted behavior, why you did it? It's expected that we will interpolate variables into query string, aren't we?

<!-- gh-comment-id:507662593 --> @dChunikhin commented on GitHub (Jul 2, 2019): Or, if it is predicted behavior, why you did it? It's expected that we will interpolate variables into query string, aren't we?
Author
Owner

@atulmy commented on GitHub (Jul 2, 2019):

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

Screen Shot 2019-07-02 at 6 16 18 AM
<!-- gh-comment-id:507672726 --> @atulmy commented on GitHub (Jul 2, 2019): 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 <img width="744" alt="Screen Shot 2019-07-02 at 6 16 18 AM" src="https://user-images.githubusercontent.com/685786/60515689-0c2d5800-9c91-11e9-9ae0-d7677c27651e.png">
Author
Owner

@dChunikhin commented on GitHub (Jul 2, 2019):

So, then i should interpolate vars from object variables into query string manually, before making request?

<!-- gh-comment-id:507690248 --> @dChunikhin commented on GitHub (Jul 2, 2019): So, then i should interpolate vars from object **variables** into query string manually, before making request?
Author
Owner

@atulmy commented on GitHub (Jul 2, 2019):

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.

<!-- gh-comment-id:507708000 --> @atulmy commented on GitHub (Jul 2, 2019): 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](https://github.com/atulmy/gql-query-builder#example-with-axios): ```javascript 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.
Author
Owner

@dChunikhin commented on GitHub (Jul 2, 2019):

My question was about data in a body of request. But, i have figured out alredy, so thx you for operative help!

<!-- gh-comment-id:507723668 --> @dChunikhin commented on GitHub (Jul 2, 2019): My question was about data in a body of request. But, i have figured out alredy, so thx you for operative help!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gql-query-builder#9
No description provided.