graphql-functions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d540a69a12fee13eb1031809c05c9597dbb30dd1
4
+ data.tar.gz: 2471e588c8fb973ee297ce8edbaa3eb68e559580
5
+ SHA512:
6
+ metadata.gz: bb57a24ba4df58d1d5ef4f59779222758c77953a6ab78957b7aaf73a52af3862ccfa22c2d63505cb95b6691b9691d16f745d19bbfb5b237f7f92df7874829654
7
+ data.tar.gz: 812abcd4ad410039265a56ceeab95fc11400d3d180098ec41f3a270bcdb10c5f789d64e132e9053adf7c372ffa9d284122e43e21cdf0ab0c2f81301d628db0e0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
14
+ .rubocop-https---raw-githubusercontent-com-comparaonline-ruby-lint-master-rubocop-yml
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/comparaonline/ruby-lint/master/rubocop.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - '**'
7
+ - 'bin/**/*'
8
+
9
+ Metrics/LineLength:
10
+ Max: 100
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in graphql-functions.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Joaquín Moreira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # graphql-functions
2
+
3
+ Ruby gem made to simplify the standard query creation of the [graphql](http://graphql-ruby.org) gem in your `Active Record` models with predefined **functions**.
4
+
5
+ Using the provided functions your graphql **types** will gain standard and generic **query arguments** to limit the amount of rows, use an offset, or filter by an specific id among others; supporting queries like the following:
6
+
7
+ ```graphql
8
+ {
9
+ people(limit: 2) {
10
+ id,
11
+ first_name
12
+ }
13
+ }
14
+ ```
15
+
16
+ ```graphql
17
+ {
18
+ people(ids: [1, 4, 9]) {
19
+ id,
20
+ age
21
+ }
22
+ }
23
+ ```
24
+
25
+ ```graphql
26
+ {
27
+ person(id: 3) {
28
+ id,
29
+ last_name
30
+ }
31
+ }
32
+ ```
33
+
34
+ The full list of features is shown [here](#features).
35
+
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ ```ruby
42
+ gem 'graphql-functions'
43
+ ```
44
+
45
+ And then execute:
46
+ ```
47
+ $ bundle
48
+ ```
49
+
50
+ Or install it yourself as:
51
+ ```
52
+ $ gem install graphql-functions
53
+ ```
54
+
55
+ Please note that you should also have installed the base [graphql](http://graphql-ruby.org) gem.
56
+
57
+ ## Usage
58
+
59
+ First of all you have to honor the following files structure convention:
60
+
61
+ ```
62
+ app\
63
+ --controllers\
64
+ --models\
65
+ ----person.rb
66
+ --graphql\
67
+ ----types\
68
+ ------query_type.rb
69
+ ------person_type.rb
70
+ ----mutations\
71
+ ----functions\
72
+ ------person.rb
73
+ ------people.rb
74
+ ----schema.rb
75
+ ```
76
+
77
+ To help getting started is useful to use the `graphql` [generator](http://graphql-ruby.org/schema/generators#graphqlinstall).
78
+
79
+ Then you should use one of the two provided functions to the `query type` definition:
80
+
81
+ ```ruby
82
+ # app/graphql/types/query_type.rb
83
+ module Types
84
+ QueryType = GraphQL::ObjectType.define do
85
+ name "Query"
86
+
87
+ field :person, function: GraphQL::Functions::Element
88
+ field :people, function: GraphQL::Functions::Array
89
+ end
90
+ end
91
+ ```
92
+
93
+ If more specific logic is needed you may create a custom class with an inheritance from `GraphQL::Functions::Element` or `GraphQL::Functions::Array` like:
94
+
95
+ ```ruby
96
+ # app/graphql/functions/people.rb
97
+ module Functions
98
+ class People < GraphQL::Functions::Array
99
+ model ::Person
100
+ argument :country_code, types.String
101
+
102
+ def call(obj, args, ctx)
103
+ query = super(obj, args, ctx)
104
+ query.where(country_code: args[:countryCode]) if args[:countryCode]
105
+ query
106
+ end
107
+ end
108
+ end
109
+ ```
110
+
111
+ ```ruby
112
+ # app/graphql/types/query_type.rb
113
+ module Types
114
+ QueryType = GraphQL::ObjectType.define do
115
+ name "Query"
116
+
117
+ field :person, function: GraphQL::Functions::Element
118
+ field :people, function: Functions::People
119
+ end
120
+ end
121
+ ```
122
+
123
+ Above changes will in addition to the base query capabilities from `graphql-functions` allow the query to filter by countryCode like:
124
+
125
+ ```
126
+ {
127
+ query(countryCode: 'us', limit: 2, offset: 5) {
128
+ id,
129
+ first_name,
130
+ last_name
131
+ }
132
+ }
133
+ ```
134
+
135
+ ## Features
136
+
137
+ ### Element
138
+ Element function is intended to be used with a query with a single field output like `person` in the example: The only available argument is id:
139
+ - `id: Int`. Filter by the specified id.
140
+
141
+ ### Array
142
+ Array function add filters-like arguments to the query:
143
+ - `offset: Int`). Skip the specified argument of records.
144
+ - `limit: Int`). Do not return more rows than the argument.
145
+ - `ìds: [Int]`). Filter by the specified ids.
146
+
147
+
148
+ ## Development
149
+
150
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
151
+
152
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
153
+
154
+ ## Contributing
155
+
156
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql-functions.
157
+
158
+ ## License
159
+
160
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "graphql/functions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ database:
2
+ override:
3
+ - echo "Skipping database section."
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql/functions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphql-functions"
8
+ spec.version = GraphQL::Functions::VERSION
9
+ spec.authors = ["Joaquín Moreira", "Daniel Ortega"]
10
+ spec.email = ["jmoreira@comparaonline.com", "dortega@comparaonline.com"]
11
+
12
+ spec.summary = %q{Collection of GraphQL functions to give basic active record like support for graphql queries}
13
+ spec.description = %q{GraphQL functions made to simplify the standard query creation of the graphql gem in your Active Record models.
14
+ Using the provided functions your graphql types will gain standard and generic query arguments to limit the amount of rows, use an offset, or filter by an specific id among others.}
15
+ spec.homepage = "https://github.com/comparaonline/graphql-ruby-functions"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = '>= 2.2'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.2"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "sqlite3", "~> 1.3"
30
+ spec.add_development_dependency "byebug"
31
+
32
+ spec.add_dependency "activerecord", "~> 5.1"
33
+ spec.add_dependency "graphql", "~> 1.5"
34
+ end
@@ -0,0 +1,8 @@
1
+ require 'graphql/functions/version'
2
+ require 'graphql/functions/element'
3
+ require 'graphql/functions/array'
4
+
5
+ module GraphQL
6
+ module Functions
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'base'
2
+
3
+ module GraphQL
4
+ module Functions
5
+ class Array < Base
6
+ argument :ids, types[types.ID]
7
+ argument :limit, types.Int
8
+ argument :offset, types.Int
9
+
10
+ def call(_, args, _)
11
+ query = args[:ids] ? @model_class.where(id: args[:ids]) : @model_class.all
12
+ query = query.offset(args[:offset]) if args[:offset]
13
+ query = query.limit(args[:limit]) if args[:limit]
14
+ query
15
+ end
16
+
17
+ def type
18
+ @type ||= self.class.types[!type_class]
19
+ end
20
+
21
+ def type_class
22
+ "Types::#{@model_class}Type".constantize
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'graphql'
2
+ require 'active_record'
3
+
4
+ module GraphQL
5
+ module Functions
6
+ class Base < ::GraphQL::Function
7
+ class << self
8
+ def create
9
+ fail_on_model_not_set unless @model
10
+ new(@model)
11
+ end
12
+
13
+ def model(model)
14
+ fail_on_wrong_class_model unless model < ActiveRecord::Base
15
+ @model = model
16
+ end
17
+
18
+ private
19
+
20
+ def fail_on_model_not_set
21
+ raise(
22
+ ArgumentError,
23
+ "'model' not set. Forgot to add 'model ::ModelClass' ?"
24
+ )
25
+ end
26
+
27
+ def fail_on_wrong_class_model
28
+ raise(
29
+ ArgumentError,
30
+ "'model' superclass mismatch. It must be 'ActiveRecord::Base'"
31
+ )
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def initialize(model_class)
38
+ @model_class = model_class
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'base'
2
+
3
+ module GraphQL
4
+ module Functions
5
+ class Element < Base
6
+ argument :id, !types.ID
7
+
8
+ def call(_, args, _)
9
+ @model_class.find(args[:id])
10
+ end
11
+
12
+ def type
13
+ @type ||= "Types::#{@model_class}Type".constantize
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module GraphQL
2
+ module Functions
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-functions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joaquín Moreira
8
+ - Daniel Ortega
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.2'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sqlite3
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: byebug
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: activerecord
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '5.1'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '5.1'
98
+ - !ruby/object:Gem::Dependency
99
+ name: graphql
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '1.5'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.5'
112
+ description: |-
113
+ GraphQL functions made to simplify the standard query creation of the graphql gem in your Active Record models.
114
+ Using the provided functions your graphql types will gain standard and generic query arguments to limit the amount of rows, use an offset, or filter by an specific id among others.
115
+ email:
116
+ - jmoreira@comparaonline.com
117
+ - dortega@comparaonline.com
118
+ executables: []
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - ".gitignore"
123
+ - ".hound.yml"
124
+ - ".rspec"
125
+ - ".rubocop.yml"
126
+ - ".travis.yml"
127
+ - Gemfile
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - bin/console
132
+ - bin/setup
133
+ - circle.yml
134
+ - graphql-functions.gemspec
135
+ - lib/graphql/functions.rb
136
+ - lib/graphql/functions/array.rb
137
+ - lib/graphql/functions/base.rb
138
+ - lib/graphql/functions/element.rb
139
+ - lib/graphql/functions/version.rb
140
+ homepage: https://github.com/comparaonline/graphql-ruby-functions
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '2.2'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.6.12
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Collection of GraphQL functions to give basic active record like support
164
+ for graphql queries
165
+ test_files: []