问题:如何将GraphQL查询保存到我的本地/ postgres数据库?
(假设我只想保存一两个字段,我会做更多的事情,但是我不确定如何开始。)
背景:
我有一个嵌入式shopify应用程序,可与webhooks配合使用以消费订单。我完成了一项工作,并且数据完美地存储在数据库中。
我已经建立了一个超级简单的应用程序,可以在模型中进行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
Controller
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
问题:如何将GraphQL查询保存到我的本地/ postgres数据库? (假设我只想保存一两个字段,我会做更多的事情,但是我不确定如何开始。)...
您正在查询Shop
订单,因此我将创建Order
模型/ orders
表,确保它属于商店。