[GH-ISSUE #85] Generate code from a raw query #52

Open
opened 2026-03-03 00:03:00 +03:00 by kerem · 1 comment
Owner

Originally created by @mateus4k on GitHub (Aug 29, 2023).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/85

Problem

Converting a GraphQL raw query to gql.query() is not the easiest task. I often see people getting confused in its use, especially when defining variables and fields.

What do you think we have a way to convert a complete query into code?

Example:

Input:

query {
  thoughts {
    id,
    name,
    thought
  }
}

Output:

const query = gql.query({
  operation: 'thoughts',
  fields: ['id', 'name', 'thought']
});
Originally created by @mateus4k on GitHub (Aug 29, 2023). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/85 ### Problem Converting a GraphQL raw query to `gql.query()` is not the easiest task. I often see people getting confused in its use, especially when defining `variables` and `fields`. What do you think we have a way to convert a complete query into code? ### Example: Input: ```graphql query { thoughts { id, name, thought } } ``` Output: ```ts const query = gql.query({ operation: 'thoughts', fields: ['id', 'name', 'thought'] }); ```
Author
Owner

@pavelmickevic commented on GitHub (Mar 15, 2025):

great idea, just lemme suggest the proper design expectation:

import * as gql from 'gql-query-builder';
import { gql } from 'graphql'; 

// read more https://www.npmjs.com/package/graphql-tag

const options = gql.parse(
    gql` # you want to wrap it into a template literal tag, else you will have a poor IDE support on the following lines
      query {
        thoughts {
          id,
          name,
          thought
        }
      }
    `
);

console.log({ options });
// outputs 
{
    options: {
      operation: 'thoughts',
      fields: ['id', 'name', 'thought']
    }
}

and ofcourse, we want the full support of queries/mutations/subscriptions, w/o variables, mandatories, aliases and stuff.

Looks massive

<!-- gh-comment-id:2726090904 --> @pavelmickevic commented on GitHub (Mar 15, 2025): great idea, just lemme suggest the proper design expectation: ```js import * as gql from 'gql-query-builder'; import { gql } from 'graphql'; // read more https://www.npmjs.com/package/graphql-tag const options = gql.parse( gql` # you want to wrap it into a template literal tag, else you will have a poor IDE support on the following lines query { thoughts { id, name, thought } } ` ); console.log({ options }); ``` ```json5 // outputs { options: { operation: 'thoughts', fields: ['id', 'name', 'thought'] } } ``` and ofcourse, we want the full support of queries/mutations/subscriptions, w/o variables, mandatories, aliases and stuff. Looks massive
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#52
No description provided.