[GH-ISSUE #8] Custom types #6

Closed
opened 2026-03-03 00:02:40 +03:00 by kerem · 2 comments
Owner

Originally created by @Zuieni on GitHub (Mar 31, 2019).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/8

Hello!

I'm using the Crate application and it is really good as a starting point for new projects! So thanks for that 😊

In the Crate code for the API part, I wanted to create a new custom type for my query. But it seems that gql-query-builder does not support custom types yet.

I wanted to get this working right now so I did the following not so good looking change in the queryDataType function 😑

// Get GraphQL equivalent type of data passed (String, Int, Float, Boolean)
function queryDataType(variable: any) {
  let type = "String";

  const value = typeof variable === "object" ? variable.value : variable;

  if (variable.type !== undefined) {
    type = variable.type;
  } else {
    switch (typeof value) {
      case "object":
        type = "Object";
        break;

      case "boolean":
        type = "Boolean";
        break;

      case "number":
        type = value % 1 === 0 ? "Int" : "Float";
        break;
    }
  }

  if (typeof variable === "object" && variable.required) {
    type += "!";
  }

  return type;
}

As you can see, I only added a variable.type check/assing for now to make it work.
Is this something you would consider adding to this lib?

Originally created by @Zuieni on GitHub (Mar 31, 2019). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/8 Hello! I'm using the [Crate](https://github.com/atulmy/crate) application and it is really good as a starting point for new projects! So thanks for that :blush: In the Crate code for the API part, I wanted to create a new custom type for my query. But it seems that gql-query-builder does not support custom types yet. I wanted to get this working right now so I did the following not so good looking change in the queryDataType function 😑 ``` // Get GraphQL equivalent type of data passed (String, Int, Float, Boolean) function queryDataType(variable: any) { let type = "String"; const value = typeof variable === "object" ? variable.value : variable; if (variable.type !== undefined) { type = variable.type; } else { switch (typeof value) { case "object": type = "Object"; break; case "boolean": type = "Boolean"; break; case "number": type = value % 1 === 0 ? "Int" : "Float"; break; } } if (typeof variable === "object" && variable.required) { type += "!"; } return type; } ``` As you can see, I only added a variable.type check/assing for now to make it work. Is this something you would consider adding to this lib?
kerem 2026-03-03 00:02:40 +03:00
Author
Owner

@atulmy commented on GitHub (Mar 31, 2019):

Glad you found the library useful. Actually we used to support in previous releases before migrating to TypeScript:

// Get GraphQL equivalent type of data passed (String, Int, Float, Boolean)
function queryDataType (data) {
  switch (typeof data) {
    case 'object':
      const type = data._type
      delete data._type
      return type

    case 'boolean':
      return 'Boolean'

    case 'number':
      return (data % 1 === 0) ? 'Int' : 'Float'

    case 'string':
    default:
      return 'String'
  }
}

Thanks for pointing it out, I'll make necessary changes and bring this feature back with fixes in code suggested by you.

<!-- gh-comment-id:478375604 --> @atulmy commented on GitHub (Mar 31, 2019): Glad you found the library useful. Actually we used to support in previous [releases](https://github.com/atulmy/gql-query-builder/blob/f874410933acd850251f70cde03f121ea92bddd3/index.js#L46) before migrating to TypeScript: ``` // Get GraphQL equivalent type of data passed (String, Int, Float, Boolean) function queryDataType (data) { switch (typeof data) { case 'object': const type = data._type delete data._type return type case 'boolean': return 'Boolean' case 'number': return (data % 1 === 0) ? 'Int' : 'Float' case 'string': default: return 'String' } } ``` Thanks for pointing it out, I'll make necessary changes and bring this feature back with fixes in code suggested by you.
Author
Owner

@atulmy commented on GitHub (Apr 10, 2019):

@Zuieni, you can update to the latest version gql-query-builder@3.0.5 for custom types.

Usage example:

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

const query = gql.mutation({
  operation: "userPhoneNumber",
  variables: {
    phone: {
      value: { prefix: "+91", number: "9876543210" },
      type: "PhoneNumber",
      required: true
    }
  },
  fields: ["id"]
})

console.log(query)

// Output
mutation ($phone: PhoneNumber!) {
  userPhoneNumber (phone: $phone) {
    id
  }
}

// Variables
{
  phone: { 
    prefix: "+91", number: "9876543210" 
  }
}
<!-- gh-comment-id:481789433 --> @atulmy commented on GitHub (Apr 10, 2019): @Zuieni, you can update to the latest version `gql-query-builder@3.0.5` for custom types. Usage example: ```javascript import * as gql from 'gql-query-builder' const query = gql.mutation({ operation: "userPhoneNumber", variables: { phone: { value: { prefix: "+91", number: "9876543210" }, type: "PhoneNumber", required: true } }, fields: ["id"] }) console.log(query) // Output mutation ($phone: PhoneNumber!) { userPhoneNumber (phone: $phone) { id } } // Variables { phone: { prefix: "+91", number: "9876543210" } } ```
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#6
No description provided.