[GH-ISSUE #62] Default mutation adapter ignores variable name #38

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

Originally created by @platformjb on GitHub (Jan 8, 2022).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/62

When generating a mutation and passing in an array of options, the name field is ignored for variables:

Mutation generation:

import { mutation } from 'gql-query-builder';

mutation(itemIds.map((_, index) => ({
    operation: `delete_${index}: deleteThing`,
    fields: [
        {
            userErrors: ['message', 'path'],
        },
    ],
    variables: {
        [`input${index}`]: {
            name: 'input',
            type: `DeleteThingInput`,
            required: true
        }
    }
}))).query;

Expected output:

mutation ($input0: DeleteThingInput!, $input1: DeleteThingInput!, $input2: DeleteThingInput!) {
    delete_0: deleteThing (input: $input0) {
      userErrors { message, path }
    }
    delete_1: deleteThing (input: $input1) {
      userErrors { message, path }
    }
    delete_2: deleteThing (input: $input2) {
      userErrors { message, path }
    }
}

Actual output:

mutation ($input0: DeleteThingInput!, $input1: DeleteThingInput!, $input2: DeleteThingInput!) {
    delete_0: deleteThing (input0: $input0) {
      userErrors { message, path }
    }
    delete_1: deleteThing (input1: $input1) {
      userErrors { message, path }
    }
    delete_2: deleteThing (input2: $input2) {
      userErrors { message, path }
    }
}

When provided, the variable name should be used as input to each mutation, rather than the key. The problematic code appears to be: github.com/atulmy/gql-query-builder@485e7db7e7/src/adapters/DefaultMutationAdapter.ts (L49-L57)

The queryDataNameAndArgumentMap in src/Utils.ts could be used to solve the issue, as it already handles name.

Originally created by @platformjb on GitHub (Jan 8, 2022). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/62 When generating a mutation and passing in an array of options, the `name` field is ignored for variables: Mutation generation: ```js import { mutation } from 'gql-query-builder'; mutation(itemIds.map((_, index) => ({ operation: `delete_${index}: deleteThing`, fields: [ { userErrors: ['message', 'path'], }, ], variables: { [`input${index}`]: { name: 'input', type: `DeleteThingInput`, required: true } } }))).query; ``` Expected output: ```gql mutation ($input0: DeleteThingInput!, $input1: DeleteThingInput!, $input2: DeleteThingInput!) { delete_0: deleteThing (input: $input0) { userErrors { message, path } } delete_1: deleteThing (input: $input1) { userErrors { message, path } } delete_2: deleteThing (input: $input2) { userErrors { message, path } } } ``` Actual output: ```gql mutation ($input0: DeleteThingInput!, $input1: DeleteThingInput!, $input2: DeleteThingInput!) { delete_0: deleteThing (input0: $input0) { userErrors { message, path } } delete_1: deleteThing (input1: $input1) { userErrors { message, path } } delete_2: deleteThing (input2: $input2) { userErrors { message, path } } } ``` When provided, the variable `name` should be used as input to each mutation, rather than the key. The problematic code appears to be: https://github.com/atulmy/gql-query-builder/blob/485e7db7e794a1453b578288ec53222c195d3087/src/adapters/DefaultMutationAdapter.ts#L49-L57 The `queryDataNameAndArgumentMap` in `src/Utils.ts` could be used to solve the issue, as it already handles `name`.
kerem closed this issue 2026-03-03 00:02:54 +03:00
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#38
No description provided.