activecube-graphql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 70fc67a7b3e554daec4571dbf025259ea90a132e86fc50d631322c2a39a75631
4
+ data.tar.gz: 0eb39688b70e8a042ef2f39f21f4909e934037001cb42f67370fb5d162ce85df
5
+ SHA512:
6
+ metadata.gz: 780293f1d8ead61547734a77c2222449388a6af845fc5c73d5f19e7cb0b9e5cc0f8d7ac048584ad0f16b3ec6b1e3cb58fa0f81b661616ebcefa50370a4fc0c6b
7
+ data.tar.gz: 14e018eaa4b571fd3d630cabb9cc1ef2ec51fbbf1650391c69c3f6ded87ed5c9699d77e4b47a4c28547e590e2ab3a7433065a432b0df6d69b70947bd797d227e
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,31 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
6
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
7
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
8
+ </content>
9
+ <orderEntry type="jdk" jdkName="RVM: ruby-2.6.3" jdkType="RUBY_SDK" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ <orderEntry type="library" scope="PROVIDED" name="activecube (v0.1.2, RVM: ruby-2.6.3) [gem]" level="application" />
12
+ <orderEntry type="library" scope="PROVIDED" name="activemodel (v6.0.2.1, RVM: ruby-2.6.3) [gem]" level="application" />
13
+ <orderEntry type="library" scope="PROVIDED" name="activerecord (v6.0.2.1, RVM: ruby-2.6.3) [gem]" level="application" />
14
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v6.0.2.1, RVM: ruby-2.6.3) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.3, RVM: ruby-2.6.3) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.6, RVM: ruby-2.6.3) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.3, RVM: ruby-2.6.3) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="graphql (v1.10.3, RVM: ruby-2.6.3) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.8.2, RVM: ruby-2.6.3) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.14.0, RVM: ruby-2.6.3) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, RVM: ruby-2.6.3) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.9.0, RVM: ruby-2.6.3) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.9.1, RVM: ruby-2.6.3) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.9.0, RVM: ruby-2.6.3) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.9.1, RVM: ruby-2.6.3) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.9.2, RVM: ruby-2.6.3) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.3.6, RVM: ruby-2.6.3) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v1.2.6, RVM: ruby-2.6.3) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="zeitwerk (v2.2.2, RVM: ruby-2.6.3) [gem]" level="application" />
30
+ </component>
31
+ </module>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.3.3-p222" project-jdk-type="RUBY_SDK" />
7
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/activecube-graphql.iml" filepath="$PROJECT_DIR$/.idea/activecube-graphql.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,26 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectId" id="1YAAllwtnwKyBVlMjptPbbPRXH8" />
4
+ <component name="ProjectViewState">
5
+ <option name="hideEmptyMiddlePackages" value="true" />
6
+ <option name="showLibraryContents" value="true" />
7
+ <option name="showMembers" value="true" />
8
+ </component>
9
+ <component name="PropertiesComponent">
10
+ <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
11
+ <property name="nodejs_npm_path_reset_for_default_project" value="true" />
12
+ <property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
13
+ </component>
14
+ <component name="RunDashboard">
15
+ <option name="ruleStates">
16
+ <list>
17
+ <RuleState>
18
+ <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
19
+ </RuleState>
20
+ <RuleState>
21
+ <option name="name" value="StatusDashboardGroupingRule" />
22
+ </RuleState>
23
+ </list>
24
+ </option>
25
+ </component>
26
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at astudnev@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in activecube-graphql.gemspec
6
+ gemspec
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activecube-graphql (0.1.0)
5
+ activecube (~> 0.1.2)
6
+ graphql (~> 1.9)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activecube (0.1.2)
12
+ activerecord (>= 5.2)
13
+ activemodel (6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ activerecord (6.0.2.1)
16
+ activemodel (= 6.0.2.1)
17
+ activesupport (= 6.0.2.1)
18
+ activesupport (6.0.2.1)
19
+ concurrent-ruby (~> 1.0, >= 1.0.2)
20
+ i18n (>= 0.7, < 2)
21
+ minitest (~> 5.1)
22
+ tzinfo (~> 1.1)
23
+ zeitwerk (~> 2.2)
24
+ concurrent-ruby (1.1.6)
25
+ diff-lcs (1.3)
26
+ graphql (1.10.3)
27
+ i18n (1.8.2)
28
+ concurrent-ruby (~> 1.0)
29
+ minitest (5.14.0)
30
+ rake (10.5.0)
31
+ rspec (3.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.1)
36
+ rspec-support (~> 3.9.1)
37
+ rspec-expectations (3.9.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.2)
44
+ thread_safe (0.3.6)
45
+ tzinfo (1.2.6)
46
+ thread_safe (~> 0.1)
47
+ zeitwerk (2.2.2)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ activecube-graphql!
54
+ bundler (~> 1.17)
55
+ rake (~> 10.0)
56
+ rspec (~> 3.0)
57
+
58
+ BUNDLED WITH
59
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 astudnev
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.
@@ -0,0 +1,88 @@
1
+ # Activecube::Graphql
2
+
3
+ The gem simplifies building GraphQL interfaces to analytical databases (OLAP).
4
+ For OLAP queries we use [Activecube](https://github.com/bitquery/activecube) gem.
5
+ Graphql is implemented by [Graphql](https://github.com/rmosolgo/graphql-ruby) gem.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'activecube-graphql'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install activecube-graphql
23
+
24
+ ## Usage
25
+
26
+
27
+ ### Field mapping
28
+
29
+ Activecube must be mapped to a field using the following construct:
30
+
31
+ ```ruby
32
+ field_class Activecube::Graphql::CubeField
33
+
34
+ field :cube_name, [Response], cube: CubeClass, null: true do
35
+ # cube arguments go here...
36
+ end
37
+ ```
38
+
39
+ where:
40
+
41
+ * **CubeClass** is the class of your Activecube ( typically defined in Models)
42
+ * **Response** is the class for response. Typically it lists all possible metrics and dimensions
43
+
44
+ **NOTE!** All fields that used as dimensions and metrics, MUST have an extra field 'ast_node' defined as:
45
+
46
+ ```ruby
47
+ field :field, Types::Dimension::Field, extras: [:ast_node], null: true do
48
+ argument :select, [FieldSelector], required: false
49
+ end
50
+ ```
51
+
52
+
53
+ ### Connection mapping
54
+
55
+ If you have multiple database connections for many cubes, it can be convinient to define the mapping
56
+ on the top level of GraphQL type:
57
+
58
+ ```ruby
59
+
60
+ field :connect, CubeFieldClass, null: false do
61
+ argument :database, Types::Enum::Databases, required: false
62
+ end
63
+
64
+ def your_field method, *args
65
+ {
66
+ database: args[0] ? args[0][:database].to_sym : method.to_sym
67
+ }
68
+ end
69
+ ```
70
+
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bitquery/activecube-graphql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the Activecube::Graphql project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bitquery/activecube-graphql/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "activecube/graphql/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activecube-graphql"
8
+ spec.version = Activecube::Graphql::VERSION
9
+ spec.authors = ["Aleksey Studnev"]
10
+ spec.email = ["astudnev@gmail.com"]
11
+
12
+ spec.summary = %q{Multi-Dimensional Queries using GraphQL}
13
+ spec.description = %q{This GEM adapts the GraphQL interface to Activecube multi-dimensional queries. Now you can use GraphQL
14
+ to query cubes}
15
+ spec.homepage = "https://github.com/bitquery/activecube-graphql"
16
+ spec.license = "MIT"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency 'activecube', '~> 0.1.2'
28
+ spec.add_runtime_dependency 'graphql', '~> 1.9'
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.17"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activecube/graphql"
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__)
@@ -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
@@ -0,0 +1,2 @@
1
+ require "activecube/graphql/version"
2
+ require "activecube/graphql/cube_field"
@@ -0,0 +1,59 @@
1
+ require "activecube/graphql/parse_tree"
2
+ require "activecube/graphql/response_builder"
3
+
4
+ require "graphql/schema/member"
5
+ require "graphql/schema/field_extension"
6
+ require "graphql/schema/field"
7
+ require "graphql/schema/argument"
8
+ require "graphql/execution/errors"
9
+
10
+
11
+ module Activecube
12
+ module Graphql
13
+ class CubeField < GraphQL::Schema::Field
14
+ argument_class GraphQL::Schema::Argument
15
+
16
+ attr_reader :cube
17
+ # Override #initialize to take a new argument cube:
18
+ def initialize(*args, cube: nil, **kwargs, &block)
19
+ @cube = cube
20
+ # Pass on the default args:
21
+ super(*args, **kwargs, &block)
22
+ append_extra :irep_node
23
+ end
24
+
25
+ def resolve_field_method(obj, ruby_kwargs, ctx)
26
+
27
+ return super unless cube
28
+
29
+ irep_node = ruby_kwargs[:irep_node]
30
+ tree = ParseTree.new cube, irep_node
31
+
32
+ database = obj.object.kind_of?(Hash) && obj.object[:database]
33
+ response = database ? cube.connected_to(database: database) do
34
+ execute_query(tree)
35
+ end : execute_query(tree)
36
+
37
+ ResponseBuilder.new tree, response
38
+
39
+ rescue ArgumentError => ex
40
+ raise GraphQL::ExecutionError, "Error executing #{cube.name}: #{ex.message}"
41
+ end
42
+
43
+ private
44
+
45
+ def execute_query tree
46
+ cube_query = tree.build_query
47
+ puts cube_query.to_sql
48
+ cube_query.query
49
+ end
50
+
51
+ def append_extra extra
52
+ unless @extras.include? extra
53
+ @extras << extra
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,138 @@
1
+ module Activecube
2
+ module Graphql
3
+ class ParseTree
4
+
5
+ class Element
6
+
7
+ attr_reader :arguments, :ast_node, :cube, :parent, :name, :definition, :key,
8
+ :children, :metric, :dimension, :field
9
+ def initialize cube, context_node, parent = nil
10
+
11
+ @cube = cube
12
+ @parent = parent
13
+
14
+ @name = context_node.name
15
+ @key = parent ? (parent.key ? "#{parent.key}.#{name}" : name ) : nil
16
+
17
+ @arguments = context_node.arguments.to_h
18
+
19
+ @ast_node = context_node.ast_node
20
+
21
+ if parent
22
+ @definition = context_node.definitions.first.name
23
+ if parent.dimension
24
+ @field = @definition
25
+ elsif parent.metric
26
+ raise ArgumentError, "Unexpected metric #{key} for cube #{cube.name}"
27
+ else
28
+ if !(@metric = cube.metrics[definition.to_sym]) && !(@dimension = cube.dimensions[definition.to_sym])
29
+ raise ArgumentError, "Metric or dimension #{definition} for #{key} not defined for cube #{cube.name}"
30
+ end
31
+ end
32
+ end
33
+
34
+ @children = context_node.typed_children.values.map(&:values).flatten.collect do |child|
35
+ Element.new cube, child, self
36
+ end
37
+
38
+ end
39
+
40
+
41
+ def append_query query
42
+ if parent
43
+
44
+ if metric
45
+ query = measure(metric, query)
46
+ end
47
+
48
+ if dimension
49
+ query = slice(dimension, query)
50
+ end
51
+
52
+ else
53
+ query = apply_args query, ( arguments && arguments.except('options'))
54
+ query = apply_args query, ( arguments && arguments['options'] )
55
+ end
56
+
57
+ children.each do |child|
58
+ query = child.append_query query
59
+ end
60
+
61
+ query
62
+ end
63
+
64
+ def measure metric, query
65
+ query.measure({key => apply_args(metric)})
66
+ end
67
+
68
+ def slice dimension, query
69
+ if children.empty?
70
+ query.slice({key => apply_args(dimension)})
71
+ else
72
+ query.slice Hash[children.collect do |field|
73
+ [field.key, field.apply_args(dimension[field.field.to_sym])]
74
+ end]
75
+ end
76
+ end
77
+
78
+ def apply_args element, args = self.arguments
79
+ args && args.each_pair do |key, value|
80
+ k = key.to_sym
81
+ if element.respond_to?(:selectors) && (selector = cube.selectors[k])
82
+ if value.kind_of? Hash
83
+ element = apply_selector element, k, value
84
+ elsif value.kind_of? Array
85
+ element = apply_to_array(element, k, selector, value)
86
+ elsif !value.nil?
87
+ element = element.when( selector.eq(value) )
88
+ end
89
+ elsif element.respond_to? k
90
+ element = element.send(k, *value)
91
+ else
92
+ raise ArgumentError, "Field #{k} is not implemented for #{element}"
93
+ end
94
+
95
+ end
96
+ element
97
+ end
98
+
99
+ def apply_selector element, k, hash
100
+ hash.each_pair do |operator, arg|
101
+ selector = cube.selectors[k]
102
+ raise ArgumentError, "#{selector} does not handle method '#{operator}' for #{element} '#{k}'" unless selector.respond_to?(operator)
103
+ element = element.when( selector.send(operator, arg) ) unless arg.nil?
104
+ end
105
+ element
106
+ end
107
+
108
+ private
109
+
110
+ def apply_to_array(element, k, selector, value)
111
+
112
+ if !value.detect { |e| !e.kind_of? Hash }
113
+ value.each { |v|
114
+ element = apply_selector element, k, v
115
+ }
116
+ elsif !value.detect { |e| !e.kind_of? String }
117
+ element = element.when(selector.in(value))
118
+ else
119
+ raise ArgumentError, "Field #{k} has unexpected array value for #{element}"
120
+ end
121
+ element
122
+ end
123
+
124
+ end
125
+
126
+ attr_reader :root, :cube
127
+ def initialize cube, context_node
128
+ @cube = cube
129
+ @root = Element.new cube, context_node
130
+ end
131
+
132
+ def build_query
133
+ root.append_query cube
134
+ end
135
+
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,101 @@
1
+ module Activecube::Graphql
2
+
3
+ class ResponseBuilder
4
+
5
+
6
+ class Response
7
+
8
+ def initialize row
9
+ @row = row
10
+ end
11
+
12
+ end
13
+
14
+
15
+ attr_reader :response, :response_class
16
+ def initialize tree, response
17
+ @response = response
18
+ @key_map = Hash[response.columns.map.with_index{|key,index| [key, index]}]
19
+ @response_class = build_response_class tree.root
20
+ end
21
+
22
+ def map &block
23
+ raise ArgumentError, "Block expected on map of root response" unless block_given?
24
+ response.rows.map do |row|
25
+ block.call response_class.new row
26
+ end
27
+ end
28
+
29
+ def build_response_class from_element
30
+
31
+ response_class = Class.new Response
32
+
33
+ from_element.children.group_by(&:definition).each{|definition, elements|
34
+
35
+ if elements.count==1
36
+ element = elements.first
37
+ if element.children.empty?
38
+ simple_value response_class, definition, element
39
+ else
40
+ sub_element response_class, definition, element
41
+ end
42
+ else
43
+ match_elements response_class, definition, elements
44
+ end
45
+
46
+ }
47
+
48
+ response_class
49
+
50
+ end
51
+
52
+ private
53
+
54
+ def match_elements response_class, definition, elements
55
+
56
+ index = Hash[elements.collect { |element|
57
+ value = if element.children.empty?
58
+ @key_map[element.key]
59
+ else
60
+ build_response_class element
61
+ end
62
+ [element.name, value]
63
+ }]
64
+
65
+ response_class.class_eval do
66
+ define_method definition.underscore do |ast_node:, **rest_of_options|
67
+ key = ast_node.alias || ast_node.name
68
+ if (value = index[key]).kind_of? Class
69
+ value.new @row
70
+ elsif value.kind_of? Integer
71
+ @row[value]
72
+ else
73
+ raise ArgumentError, "Unexpected request to #{definition} by key #{key}"
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ def sub_element response_class, definition, element
81
+ subclass = build_response_class element
82
+ response_class.class_eval do
83
+ define_method definition.underscore do |**rest_of_options|
84
+ subclass.new @row
85
+ end
86
+ end
87
+ end
88
+
89
+ def simple_value response_class, definition, element
90
+ index = @key_map[element.key]
91
+ response_class.class_eval do
92
+ define_method definition.underscore do |**rest_of_options|
93
+ @row[index]
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ module Activecube
2
+ module Graphql
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activecube-graphql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksey Studnev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activecube
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: graphql
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |-
84
+ This GEM adapts the GraphQL interface to Activecube multi-dimensional queries. Now you can use GraphQL
85
+ to query cubes
86
+ email:
87
+ - astudnev@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".idea/activecube-graphql.iml"
94
+ - ".idea/misc.xml"
95
+ - ".idea/modules.xml"
96
+ - ".idea/workspace.xml"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - CODE_OF_CONDUCT.md
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - activecube-graphql.gemspec
106
+ - bin/console
107
+ - bin/setup
108
+ - lib/activecube/graphql.rb
109
+ - lib/activecube/graphql/cube_field.rb
110
+ - lib/activecube/graphql/parse_tree.rb
111
+ - lib/activecube/graphql/response_builder.rb
112
+ - lib/activecube/graphql/version.rb
113
+ homepage: https://github.com/bitquery/activecube-graphql
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.4
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Multi-Dimensional Queries using GraphQL
136
+ test_files: []