[GH-ISSUE #94] Default values for Variables #59

Open
opened 2026-03-03 00:03:02 +03:00 by kerem · 0 comments
Owner

Originally created by @pavelmickevic on GitHub (Mar 15, 2025).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/94

I wish, I could define a query with variables, where some variables have the default value set

Original Example

works well. I love it.

import * as gql from 'gql-query-builder'

const query = gql.query({
  operation: 'userLogin',
  variables: {
    email: { value: 'jon.doe@example.com', required: true },
    password: { value: '123456', required: true }
  },
  fields: ['userId', 'token']
})

console.log(query)

// Output
query ($email: String!, $password: String!) {
  userLogin (email: $email, password: $password) {
    userId, token
  }
}

// Variables
{
  email: "jon.doe@example.com",
  password: "123456"
}

Mine Proposal

not available at the moment

import * as gql from 'gql-query-builder'

const { query, variables } = gql.query({
  operation: 'userLogin',
  variables: {
-    email: { value: 'jon.doe@example.com', required: true },
+    email: { value: 'jon.doe@example.com', required: true, backup: 'barry.smith@example.com' },
-    password: { value: '123456', required: true }
+    password: { value: '123456', required: true, backup: 'oneTwo' }
  },
  fields: ['userId', 'token']
})

console.log({ query, variables });

// Output
-query ($email: String!, $password: String!) {
+query ($email: String! = "barry.smith@example.com", $password: String! = "oneTwo") {
  userLogin (email: $email, password: $password) {
    userId, token
  }
}

// Variables
{
  email: "jon.doe@example.com",
  password: "123456"
}

Sometimes I wish I could include the variable values into default variable declaration, and not to have variables at all.

Originally created by @pavelmickevic on GitHub (Mar 15, 2025). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/94 I wish, I could define a query with variables, where some variables have the default value set ## Original Example ✅ works well. I love it. ```js import * as gql from 'gql-query-builder' const query = gql.query({ operation: 'userLogin', variables: { email: { value: 'jon.doe@example.com', required: true }, password: { value: '123456', required: true } }, fields: ['userId', 'token'] }) console.log(query) // Output query ($email: String!, $password: String!) { userLogin (email: $email, password: $password) { userId, token } } // Variables { email: "jon.doe@example.com", password: "123456" } ``` ## Mine Proposal ❌ not available at the moment ```diff import * as gql from 'gql-query-builder' const { query, variables } = gql.query({ operation: 'userLogin', variables: { - email: { value: 'jon.doe@example.com', required: true }, + email: { value: 'jon.doe@example.com', required: true, backup: 'barry.smith@example.com' }, - password: { value: '123456', required: true } + password: { value: '123456', required: true, backup: 'oneTwo' } }, fields: ['userId', 'token'] }) console.log({ query, variables }); // Output -query ($email: String!, $password: String!) { +query ($email: String! = "barry.smith@example.com", $password: String! = "oneTwo") { userLogin (email: $email, password: $password) { userId, token } } // Variables { email: "jon.doe@example.com", password: "123456" } ``` Sometimes I wish I could include the variable values into default variable declaration, **and not** to have variables at all.
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#59
No description provided.