在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):solidusio-contrib/solidus_graphql_api开源软件地址(OpenSource Url):https://github.com/solidusio-contrib/solidus_graphql_api开源编程语言(OpenSource Language):Ruby 85.1%开源软件介绍(OpenSource Introduction):SolidusGraphqlApiProvides a graphql api for the Solidus ecommerce framework. Supported Versions
InstallationAdd solidus_graphql_api to your Gemfile: gem 'solidus_graphql_api' Bundle your dependencies and run the installation generator: bin/rails generate solidus_graphql_api:install Unlike the REST API which has a variety of endpoints, the GraphQL API has a
single endpoint accessible under For example in development you can use:
DocumentationThe Solidus GraphQL API documentation can be found here. CustomizationsYou can extend the gem functionality through decorators, just like Solidus. For example, assuming we are placing our grapqhl decorators in Adding a new fieldmodule Graphql
module Types
module ProductDecorator
def self.prepended(base)
base.field :test, GraphQL::Types::String, null: true
end
def test
'test'
end
SolidusGraphqlApi::Types::Product.prepend self
end
end
end or also, if we want to add the taxon relation to the type product: module Graphql
module Types
module ProductDecorator
def self.prepended(base)
base.field :taxons, SolidusGraphqlApi::Types::Taxon.connection_type, null: true
end
def taxons
SolidusGraphqlApi::BatchLoader.for(object, :taxons)
end
SolidusGraphqlApi::Types::Product.prepend self
end
end
end Modifying an existing fieldLike for adding a new field, we modify the module Graphql
module Types
module ProductDecorator
def self.prepended(base)
base.field :name, GraphQL::Types::String, null: true
end
def name
object.concat(' ', 'Graphql')
end
SolidusGraphqlApi::Types::Product.prepend self
end
end
end Removing a fieldmodule Graphql
module Types
module ProductDecorator
def self.prepended(base)
base.remove_field :name
end
SolidusGraphqlApi::Types::Product.prepend self
end
end
end Adding a new TypeLet's say we want the Product type to return its stock_items: First we create a StockItem type: module Graphql
module Types
class StockItem < SolidusGraphqlApi::Types::Base::RelayNode
description 'StockItem.'
field :count_on_hand, Integer, null: false
end
end
end And in the product decorator type: require_relative 'stock_item'
module Graphql
module Types
module ProductDecorator
def self.prepended(base)
base.field :stock_items, Graphql::Types::StockItem.connection_type, null: false
end
def stock_items
object.stock_items
end
SolidusGraphqlApi::Types::Product.prepend self
end
end
end The query may look something like: query productBySlug ($slug: String!) {
productBySlug (slug: $slug) {
stockItems {
nodes {
countOnHand
}
}
}
} Adding a new Querymodule Graphql
module Types
module QueryDecorator
def self.prepended(base)
base.field :taxons, SolidusGraphqlApi::Types::Taxon.connection_type, null: false
end
def taxons
Spree::Taxon.all
end
SolidusGraphqlApi::Types::Query.prepend self
end
end
end In your application you probably want to create a query object to retrieves the taxons.
Check DevelopmentTesting the extensionFirst bundle your dependencies, then run bundle
bin/rake To run Rubocop static code analysis run bundle exec rubocop When testing your application's integration with this extension you may use its factories. Simply add this require statement to your spec_helper: require 'solidus_graphql_api/factories' Running the sandboxTo run this extension in a sandboxed Solidus application, you can run Here's an example: $ bin/rails server
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop Updating the changelogBefore and after releases the changelog should be updated to reflect the up-to-date status of the project: bin/rake changelog
git add CHANGELOG.md
git commit -m "Update the changelog" Releasing new versionsPlease refer to the dedicated page on Solidus wiki. LicenseCopyright (c) 2020-2021 Nebulab, released under the New BSD License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论