Giter Site home page Giter Site logo

cpfergus1 / solidus_graphql_api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from solidusio/solidus_graphql_api

0.0 0.0 0.0 1.94 MB

GraphQL comes to Solidus!

License: BSD 3-Clause "New" or "Revised" License

Ruby 84.35% HTML 14.65% Shell 1.00%

solidus_graphql_api's Introduction

Maintainability Test Coverage CircleCI Gem Version

SolidusGraphqlApi

Provides a graphql api for the Solidus ecommerce framework.

Installation

Add solidus_graphql_api to your Gemfile:

gem 'solidus_graphql_api'

Bundle your dependencies:

bundle

Unlike the REST API which has a variety of endpoints, the GraphQL API has a single endpoint accessible under /graphql.

For example in development you can use:

POST http://localhost:3000/graphql

Documentation

The Solidus GraphQL API documentation can be found here.

Customizations

You can extend the gem functionality through decorators, just like Solidus.

For example, assuming we are placing our grapqhl decorators in app/graphql/types:

Adding a new field

module 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 field

Like for adding a new field, we modify the name field in the same way:

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 field

module 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 Type

Let'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 Query

module 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 SolidusGraphqlApi::Types::Query for examples.

Development

Testing the extension

First bundle your dependencies, then run bin/rake. bin/rake will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using bin/rake extension:test_app.

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 sandbox

To run this extension in a sandboxed Solidus application, you can run bin/sandbox. The path for the sandbox app is ./sandbox and bin/rails will forward any Rails commands to sandbox/bin/rails.

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

Releasing new versions

Your new extension version can be released using gem-release like this:

bundle exec gem bump -v VERSION --tag --push --remote upstream && gem release

License

Copyright (c) 2020 Nebulab, released under the New BSD License.

solidus_graphql_api's People

Contributors

alessiorocco avatar christianrimondi avatar rainerdema avatar aldesantis avatar dependabot-preview[bot] avatar samuelmartini avatar sinetheta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.