ashkan18 / graphlient

Ruby GraphQL Client
MIT License
251 stars 43 forks source link

Unable to pass literal values / constants in Ruby #104

Closed andreibondarev closed 1 year ago

andreibondarev commented 1 year ago

Description I'm working on writing a Ruby wrapper for a GraphQL API. Some of the endpoints accept things like this (from the docs):

{
  Get {
    Article(sort: [{
      order: asc          # <-- This is the issue

and

{
  Get {
    Article(where: {
        ...
        operator: Equal,           # <-- This is the issue

Passing "asc", 'asc' or a variable with the name asc throws a GraphQL error. Respectively -- passing 'Equal', "Equal" errors out as well.

I was able to get it working by defining an Equal class, module, constant or OpenStruct and passing it in:

class Equal; end;

but this feels extremely hacky.

Is there a cleaner way to do this? The problem is that I can't hardcode order: Equal because the value is dynamic.

Here's my current code where I parse the query:

    def get_query(params, fields)
      client.graphql.parse """
        query {
          Get {
              Question(#{params}) {
                #{fields.join(' ')}
              }
          }
        }
      """
    end

params = { where: { operator: 'equal', field: 'name' }, sort: { order: 'asc'} }
get_query(params)

Thank you!

dblock commented 1 year ago

Did you find your answer @andreibondarev ?

andreibondarev commented 1 year ago

@dblock I didn't. Instead of passing a hash I'm passing a string in the following format now:

{
  Get {
    Article(
      where: '{ path: ["id"], operator: Equal, valueString: "..." }'
    )
  }
}

This is where it's being used: https://github.com/andreibondarev/weaviate-ruby/blob/master/lib/weaviate/query.rb#L127-L149

dblock commented 1 year ago

Thanks. I think we should support this. If you have time @andreibondarev write some failing specs, and maybe even implement it? We could probably emit a class at runtime or something like that.