shopify-hotwire-sample icon indicating copy to clipboard operation
shopify-hotwire-sample copied to clipboard

Include GraphQL

Open Kurbm opened this issue 2 years ago • 2 comments

Hey @kirillplatonov

I would like to implement your graphql example into the hotwire sample (https://github.com/kirillplatonov/shopify_graphql).

But I'm not so sure how to actually do it.

I already installed your gem and added fields and a get query to a graphql folder. For now a super simple one

Product fields

` module ShopifyGraphql class ProductFields FRAGMENT = <<~GRAPHQL fragment ProductFields on Product { id title } GRAPHQL

def self.parse(data)
  OpenStruct.new(
    id: data.id,
    title: data.title
  )
end

end end `

Get Products

`module ShopifyGraphql class GetProducts include Query

QUERY = <<~GRAPHQL
  #{ProductFields::FRAGMENT}
  query{
    products(first: 10, query: "title:*rolf*") {
      edges {
        node {
          id
          title
        }
      }
    }
  }
GRAPHQL

def call
  response = execute(QUERY)
  response.data = parse_data(response.data)
  response
end

private

def parse_data(data)
  unless data.node
    raise ResourceNotFound.new(200, "Products not found")
  end

  OpenStruct.new(
    products: ProductFields.parse(data.node),
  )
end

end end `

But now I'm a bit stuck, how I actually could run this query from a controller. E.g. someone would search for a partial product title in a search form and should see the result than as a list.

Could you may help me out, to figure out, where I would need to call the function?

Potential call function

products = GetProducts.call.data @title = products.title

Thanks and best, Kevin

Kurbm avatar Sep 02 '22 15:09 Kurbm

Hey Kevin,

I will add ShopifyGraphql and sample query to this repo 👌

kirillplatonov avatar Sep 07 '22 14:09 kirillplatonov

Amazing. Thanks a lot.

Kurbm avatar Sep 08 '22 15:09 Kurbm