【问题标题】:Save GraphQL query from Shopify API in postgres databsae将来自 Shopify API 的 GraphQL 查询保存在 postgres 数据库中
【发布时间】:2020-05-19 17:26:44
【问题描述】:

问题:如何将 GraphQL 查询保存到我的本地/postgres 数据库?

(假设我只想保存一两个字段——我会做更多,但我只是不知道如何开始。)

背景:

  • 我有一个嵌入式 shopify 应用程序,它与 webhook 一起使用以消费订单。我运行一项工作,数据完美地存储在我的数据库中。

  • 我已经设置了一个超级简单的应用程序,可以在模型中进行 graphql 查询。我已经设置好了,所以我可以在视图中看到 json 对象。但是我不是 100% 清楚我是如何从 graphql api 查询 -> 响应(在模型中 - 至少现在)-> 将数据属性保存到我的数据库中

  • 我正在将此作为一个新应用程序进行测试。所以,我没有拆分模型/控制器等,暂时只使用一个模型

  • 我想我只是有点迷茫。我需要经营一份工作吗?或者这是我可以在控制器(或模型)中做的事情。

型号

shop.rb

class Shop < ActiveRecord::Base
  include ShopifyApp::ShopSessionStorage
  has_many :orders

  def api_version
    ShopifyApp.configuration.api_version
  end

  session = ShopifyAPI::Session.new(domain: "bryanbeshore.myshopify.com", token: Shop.first.shopify_token, api_version: "2020-04")
  ShopifyAPI::Base.activate_session(session)

  client = ShopifyAPI::GraphQL.client

  SHOP_NAME_QUERY = client.parse <<-'GRAPHQL'
      {
    shop {
      name
    }
    orders(first: 100) {
      edges {
        node {
          id
          name
          createdAt
          shippingAddress {
            address1
            address2
            city
            province
            provinceCode
            zip
          }
        }
      }
    }
    }
  GRAPHQL
end

控制器

home_controller.rb

class HomeController < AuthenticatedController

  def index
    client = ShopifyAPI::GraphQL.client
    @shop_orders = client.query(Shop::SHOP_NAME_QUERY).data.to_json
  end
end

查看

app/views/home/index.html.erb

<p><%= @shop_orders %></p>

当前架构

ActiveRecord::Schema.define(version: 2020_05_06_181457) do

  create_table "orders", force: :cascade do |t|
    t.string "shopify_order_id", null: false
    t.string "shopify_order_name", default: ""
    t.datetime "shopify_order_created_at"
    t.integer "shop_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["shop_id"], name: "index_orders_on_shop_id"
    t.index ["shopify_order_id"], name: "index_orders_on_shopify_order_id", unique: true
  end

  create_table "shops", force: :cascade do |t|
    t.string "shopify_domain", null: false
    t.string "shopify_token", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["shopify_domain"], name: "index_shops_on_shopify_domain", unique: true
  end

end

【问题讨论】:

    标签: ruby-on-rails graphql shopify shopify-app shopify-api


    【解决方案1】:

    您正在查询Shop 订单,所以我将创建一个Order 模型/orders 表,确保它属于一家商店。

    • rails g model Order payload:jsonb shop:references,对于这个例子,我只是创建一个 jsonb 字段,我们将把整个 JSON 对象转储到其中。
    • rails db:migrate
    • models/order.rb 中添加belongs_to :shop
    • 确保Shop模型有这个has_many :orders,通常rails会为你添加它

    现在,当您从 Shopify 获取 JSON 负载时,循环遍历它并为您收到的每个订单创建一条新记录。

    所以在你用来查询订单的方法中添加这个。

    shopify_json_response.each do |item|
      orders.create(payload: item)
    end
    

    或多或少,这应该可以解决问题。 您不需要后台作业,但是当您想要处理不需要立即处理的数据时,后台作业是理想的。

    【讨论】:

    • 这或多或少起到了作用。对于您发布的shopify_json_response.each do ...,我有点困惑。我不应该将整个有效负载发布到 postgres 数据库吗?或者你正在用这个做其他事情?
    • 这取决于您的用例,我的假设是您想从 Shopify 获取订单并将它们存储在您的数据库中,以便您以后可以将它们用于业务逻辑,这就是为什么您从 Shopify 返回的有效负载 我正在创建新的本地记录。但是,如果您只想存储具有 100 个订单的整个有效负载,则可以创建一个 dump 模型,您可以使用 Dump.new(payload: shopify_json_response) 并且稍后可以访问它!我希望有所帮助:)
    猜你喜欢
    • 2021-01-15
    • 2017-05-29
    • 2019-04-06
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多