kirillplatonov / shopify_graphql

Less painful way to work with Shopify Graphql API in Ruby.
MIT License
59 stars 9 forks source link

Automatically `snake_case`s response fields #39

Closed k0va1 closed 4 months ago

k0va1 commented 4 months ago

Shopify returns all response fields in camelCase, but it'd be great to convert them to snake_cased ones automatically

kirillplatonov commented 4 months ago

The main reason why I didn't implemented that in the first place is that Graphql responses can be deeply nested. And when parsing response we not only need to switch to snake case, but also flatten data to feel more like a regular Ruby object.

For example query like this:

{
  markets(first: 50) {
    nodes {
      id
      enabled
      name
      handle
      webPresence {
        defaultLocale
        alternateLocales
        domain {
          url
        }
      }
    }
  }
}

Will be parsed into flat snake case object:

def parse_data(data)
  return [] if data.blank?

  data.compact.map do |node|
    OpenStruct.new(
      id: node.id,
      enabled: node.enabled,
      name: node.name,
      handle: node.handle,
      default_locale: node.webPresence&.defaultLocale,
      alternate_locales: node.webPresence&.alternateLocales,
      domain: node.webPresence&.domain&.url
    )
  end
end

Doing this automatically seems not possible. And changing only case automatically means redundant data traversing since we'll need to do it again during parsing.

Do you have any ideas how it can be solved?

k0va1 commented 4 months ago

Alright, got u. Data flattering is the case, I don't see any easy solution to this issue either. At least now 😀