Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum support #36

Closed
franciscolourenco opened this issue May 6, 2020 · 11 comments
Closed

Enum support #36

franciscolourenco opened this issue May 6, 2020 · 11 comments
Assignees
Labels

Comments

@franciscolourenco
Copy link

franciscolourenco commented May 6, 2020

Describe the bug
Hasura's on_conflict argument expect enums to be provided without quotes, however gotql does use quotes for every argument value. This result in an error (see screenshots).

To Reproduce

gotQl.mutation(
  process.env.GRAPHQL_HOST,
  {
    operation: {
      name: 'insert_example',
      args: {
        objects: {id, status, price},
        on_conflict: {constraint: 'example_pkey', update_columns: ['status', 'price']},
      },
      fields: ['affected_rows'],
    }
  },
)

Current behavior
Part of the query:

on_conflict: {constraint: \"example_pkey\", update_columns: [\"status\", \"price\"]}

Expected behavior
Part of the query:

on_conflict: {constraint: example_pkey, update_columns: [status, price]}

Screenshots
image
image
image

@franciscolourenco
Copy link
Author

I guess I found my answer: https://github.com/khaosdoctor/gotql#enum-args

@franciscolourenco
Copy link
Author

Re-opening because this:

on_conflict: {
  constraint: {value: 'subscriptions_pkey', escape: false},
  update_columns: [
    {value: 'status', escape: false},
    {value: 'price', escape: false},
  ],
},

results in this query:

on_conflict: { constraint: subscriptions_pkey, update_columns: [\"[object Object]\",\"[object Object]\"] })

@franciscolourenco
Copy link
Author

on_conflict: {
  constraint: {value: 'subscriptions_pkey', escape: false},
  update_columns: {
    value: ['status', 'price'],
    escape: false,
  },
},

almost works:

on_conflict: { constraint: subscriptions_pkey, update_columns: status,price }

@khaosdoctor
Copy link
Owner

khaosdoctor commented May 6, 2020

This is intended. Escaped values will not be processed, for you to obtain the correct representation, you need to transform your query into:

on_conflict: {
  constraint: {value: 'subscriptions_pkey', escape: false},
  update_columns: {
    value: '[status,price]',
    escape: false,
  },
},

@khaosdoctor khaosdoctor added wontfix and removed bug labels May 6, 2020
@khaosdoctor khaosdoctor self-assigned this May 6, 2020
@franciscolourenco
Copy link
Author

Thanks. It might be worth to enrich the docs with this example. This edge case makes the concept of this library a bit more verbose than expected. Have you thought of providing a helper function for escaped values? Example:

import {literal} from 'gotql'

on_conflict: {
  constraint: literal('subscriptions_pkey'),
  update_columns: literal(['status','price']),
},

@franciscolourenco
Copy link
Author

or with a tagged template literal

import {literal} from 'gotql'

on_conflict: {
  constraint: literal`subscriptions_pkey`,
  update_columns: [literal`status`, literal`price`],
},

@khaosdoctor
Copy link
Owner

Thanks. It might be worth to enrich the docs with this example.

Yeah, I'll update the README with some examples like these to make it clearer.

This edge case makes the concept of this library a bit more verbose than expected. Have you thought of providing a helper function for escaped values?

I thought about it recently, when I created it there weren't this many resources on ECMA yet. However, the whole idea of gotql is not to make queries smaller, but clearer to understand and to manipulate. Even with simple queries, the JSON-Like format is more verbose than the common syntax, however, it is way more processable and usable than strings.

But that might be a good solution to escaped values, I think I'll add it as a function but keeping the support for {value: x, escape: b} for now, then I'll release a V2 with no support to those at all.

or with a tagged template literal

import {literal} from 'gotql'

on_conflict: {
  constraint: literal`subscriptions_pkey`,
  update_columns: [literal`status`, literal`price`],
},

This is cleaner, but unfortunately, it isn't possible. The parser parses the object as a whole, so when you put [literal`status`] the brackets [] identify an array and the parser will identify the outside object as an array regardless of what comes inside, it'd be a big change in the way it works.

For now, the best course of action is to work as I said before, but I'll implement a literal function along with some refactoring (to move libs and helpers outside some files) which converts whatever is inside to {value: x, escape: b}.

Thanks for the heads up and the solution

@khaosdoctor
Copy link
Owner

Actually, thanks for using the lib and providing such useful feedback and bug reports in general, you're evolving it so other people can benefit from it as well. Since I'm not actively using gotQL a lot, this is very helpful to those who will use in the future and allow the community to grow and the lib to be even better.

Thanks a lot!

@franciscolourenco
Copy link
Author

Thank you for creating and maintaining :)
All the helper function needs to do really is return an object {value: x, escape: b}

@khaosdoctor
Copy link
Owner

Yep, just what I was thinking :D

@khaosdoctor
Copy link
Owner

Closed in #47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants