No commit activity in last 3 years
No release in over 3 years
This gem allows you to share arguments between multiple fields while using graphql.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.14
~> 10.0
~> 3.0

Runtime

>= 1.6.0
 Project Readme

shared-arguments

Build Status

This gem allows share arguments within GraphQL fields for graphql-ruby.

Created by

Sponsored by Indoorway

Contents

  • Description
  • Installation
  • Setup
  • Sample use case

Description

Sometimes you want to use the same arguments for multiple fields in custom object type. To avoid repeating those declarations and DRY-up your schema, I introduce you shared_arguments field.

This gem is mostly usable when you have:

  • many fields with few repeating arguments
  • few fields with many repeating arguments
  • many fields with many repeating arguments

Installation

gem install shared-arguments
require 'graphql/shared_arguments

With Gemfile

gem 'shared-arguments', require: 'graphql/shared_examples'

Setup

Include SharedArguments in your schema

#graphql/your_schema.rb

YourSchema = GraphQL::Schema.define do
  use GraphQL::SharedArguments.new
end

Sample use case

#graphql/types/analytics_type.rb

Types::AnalyticsType = GraphQL::ObjectType.define do
    name 'Analytics'
    
    field :invoices, types.[Types::Invoice] do
       argument :from, !Types::DateType
       argument :to, !Types::DateType
       argument :companyID, types.ID
    end
    
    field :registeredUsers, types.[Types::User] do
       argument :from, !Types::DateType
       argument :to, !Types::DateType
       argument :companyID, types.ID
    end
    
    field :downloadsAmount, types.Int do
       argument :from, !Types::DateType
       argument :to, !Types::DateType
       argument :userAmount, types.ID
    end
end

This can be shortened like in below example

#graphql/types/analytics_type.rb

Types::AnalyticsType = GraphQL::ObjectType.define do
    name 'Analytics'
    
    field :invoices, types.[Types::Invoice]
    
    field :registeredUsers, types.[Types::User]
    
    field :downloadsAmount, types.Int do
       argument :userAmount, types.ID
    end
    
    shared_arguments do
       argument :from, !Types::DateType
       argument :to, !Types::DateType
    end
    
    shared_arguments except: %i(downloadsAmount) do
       argument :companyID, types.ID
    end
end

shared_arguments field takes both except and only keyword with array of symbols so you can omit or select specified fields.

Any contributions/suggestions/opened issues are welcomed.

License: MIT