stardust_rails 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +140 -0
  4. data/Rakefile +32 -0
  5. data/app/controllers/stardust/graphql_controller.rb +79 -0
  6. data/config/routes.rb +7 -0
  7. data/lib/generators/example/mutations/update_item.rb +22 -0
  8. data/lib/generators/example/queries/foo.rb +31 -0
  9. data/lib/generators/example/queries/foos.rb +29 -0
  10. data/lib/generators/example/types/item.rb +9 -0
  11. data/lib/generators/mutation.template +30 -0
  12. data/lib/generators/query.template +26 -0
  13. data/lib/generators/type.template +8 -0
  14. data/lib/stardust/configuration.rb +13 -0
  15. data/lib/stardust/engine.rb +51 -0
  16. data/lib/stardust/generators/example.rb +17 -0
  17. data/lib/stardust/generators/mutation.rb +22 -0
  18. data/lib/stardust/generators/query.rb +22 -0
  19. data/lib/stardust/generators/type.rb +22 -0
  20. data/lib/stardust/generators.rb +9 -0
  21. data/lib/stardust/graphql/collector.rb +161 -0
  22. data/lib/stardust/graphql/configuration.rb +23 -0
  23. data/lib/stardust/graphql/dsl.rb +45 -0
  24. data/lib/stardust/graphql/extensions/authorize.rb +22 -0
  25. data/lib/stardust/graphql/field.rb +38 -0
  26. data/lib/stardust/graphql/input_object.rb +24 -0
  27. data/lib/stardust/graphql/mutation.rb +61 -0
  28. data/lib/stardust/graphql/object.rb +30 -0
  29. data/lib/stardust/graphql/query.rb +62 -0
  30. data/lib/stardust/graphql/scalar.rb +23 -0
  31. data/lib/stardust/graphql/schema.rb +8 -0
  32. data/lib/stardust/graphql/types/dsl.rb +27 -0
  33. data/lib/stardust/graphql/types.rb +10 -0
  34. data/lib/stardust/graphql/union.rb +27 -0
  35. data/lib/stardust/graphql.rb +25 -0
  36. data/lib/stardust/instance.rb +25 -0
  37. data/lib/stardust/version.rb +3 -0
  38. data/lib/stardust.rb +26 -0
  39. data/lib/tasks/stardust_tasks.rake +13 -0
  40. metadata +167 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6bbe149e010f7ea906ef2931a7b4d54ce6b2617a98148e86a1bc2a448f21d296
4
+ data.tar.gz: 119e01c47e8cee378d93ba7d93b2dea97680aa643506d74ff658230f3b3ef2af
5
+ SHA512:
6
+ metadata.gz: bb299aa1d30927a1364ef8b233f88ccd96141885375430b6ee9e9a415f25404434e09a17bd59805fb1fe0890ba7c869901f6dc9de101a0a5ebf3674b55d6ffb5
7
+ data.tar.gz: 6337f5244a51e946793b89b02920ab617ccfe7fe5e7b54167702f74c6d3d67a035246d81ce34994dacb9b89ca550089918f0181ad58e9bce2faf792e9b2576c5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Bradley Wittenbrook
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Stardust
2
+ GraphQL APIs in Rails made easy
3
+
4
+ Building on top of the fantastic [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) gem, Stardust allows you to focus on business logic
5
+
6
+ ```
7
+ - app
8
+ - graph
9
+ - mutations
10
+ - queries
11
+ - types
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Stardust uses `define_` hooks to automatically build out the graph by crawling through the `graph` directory inside your application.
17
+
18
+ Mutations and Queries are called through the `resolve` method. Any arguments are passed in as **named** arguments to the function.
19
+
20
+ ### Mutation
21
+
22
+ Mutations are used to define
23
+
24
+ ```ruby
25
+ Stardust::GraphQL.define_mutation :add_item do
26
+
27
+ description "Add a new item"
28
+ null false
29
+
30
+ argument :name, :string, required: true
31
+
32
+ field :item, :item, null: true
33
+ field :success, :boolean, null: false
34
+
35
+ def resolve(name:)
36
+ {
37
+ success: true,
38
+ item: {
39
+ id: 4,
40
+ name: name
41
+ }
42
+ }
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ ### Query
49
+
50
+ ```ruby
51
+ Stardust::GraphQL.define_query :items do
52
+
53
+ def items
54
+ [
55
+ {
56
+ id: 1,
57
+ name: "foo"
58
+ },
59
+ {
60
+ id: 2,
61
+ name: "bar"
62
+ },
63
+ {
64
+ id: 3,
65
+ name: "baz"
66
+ }
67
+ ].freeze
68
+ end
69
+
70
+ description "Get a list of items"
71
+ type [:item]
72
+ null false
73
+
74
+ def resolve
75
+ items
76
+ end
77
+
78
+ end
79
+ ```
80
+
81
+ ### Type
82
+
83
+ ```ruby
84
+ Stardust::GraphQL.define_types do
85
+
86
+ object :item do
87
+ description "An example item"
88
+ field :id, :id, null: false
89
+ field :name, :string, null: false
90
+ end
91
+
92
+ end
93
+ ```
94
+
95
+
96
+
97
+ ## Installation
98
+ Add this line to your application's Gemfile:
99
+
100
+ ```ruby
101
+ gem 'stardust_rails', require: 'stardust'
102
+ ```
103
+
104
+ And then execute:
105
+ ```bash
106
+ $ bundle
107
+ ```
108
+
109
+ Or install it yourself as:
110
+ ```bash
111
+ $ gem install stardust_rails
112
+ ```
113
+
114
+ Mount the engine:
115
+ ```ruby
116
+ # config/routes.rb
117
+ Rails.application.routes.draw do
118
+ ...
119
+ mount Stardust::Engine, at: "/"
120
+ ...
121
+ end
122
+ ```
123
+
124
+ Generate your first type, query or mutation:
125
+ ```bash
126
+ $ rails g stardust:example
127
+ $ rails g stardust:type foo
128
+ $ rails g stardust:query foos
129
+ $ rails g stardust:mutation bar
130
+ ```
131
+
132
+ View GraphiQL here:
133
+ [http://localhost:3000/graphiql](http://localhost:3000/graphiql)
134
+
135
+
136
+ ## Contributing
137
+ Contribution directions go here.
138
+
139
+ ## License
140
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Stardust'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,79 @@
1
+ module Stardust
2
+ class GraphQLController < ActionController::API
3
+
4
+ def execute
5
+ result = nil
6
+
7
+ around_execute do
8
+ result = GraphQL::Schema.execute(
9
+ query,
10
+ variables: variables,
11
+ context: context,
12
+ operation_name: operation_name
13
+ )
14
+ end
15
+ render json: result
16
+
17
+ rescue StandardError => e
18
+ raise e unless Rails.env.development?
19
+ handle_error_in_development e
20
+ end
21
+
22
+
23
+ private
24
+
25
+ def operation_name
26
+ params[:operationName]
27
+ end
28
+
29
+ def query
30
+ params[:query]
31
+ end
32
+
33
+ def variables
34
+ ensure_hash(params[:variables])
35
+ end
36
+
37
+ def context
38
+ setup_context = Stardust.configuration.graphql.setup_context
39
+ setup_context ? setup_context.(request) : {}
40
+ end
41
+
42
+ def around_execute
43
+ around_execute = Stardust.configuration.graphql.around_execute
44
+ yield around_execute
45
+ end
46
+
47
+ # Handle form data, JSON body, or a blank value
48
+ def ensure_hash(ambiguous_param)
49
+ case ambiguous_param
50
+ when String
51
+ if ambiguous_param.present?
52
+ ensure_hash(JSON.parse(ambiguous_param))
53
+ else
54
+ {}
55
+ end
56
+ when Hash, ActionController::Parameters
57
+ ambiguous_param
58
+ when nil
59
+ {}
60
+ else
61
+ raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
62
+ end
63
+ end
64
+
65
+ def handle_error_in_development(e)
66
+ logger.error e.message
67
+ logger.error e.backtrace.join("\n")
68
+
69
+ render json: {
70
+ error: {
71
+ message: e.message,
72
+ backtrace: e.backtrace
73
+ },
74
+ data: {}
75
+ }, status: 500
76
+
77
+ end
78
+ end
79
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+
3
+ post "/graphql", to: "stardust/graphql#execute"
4
+ if Stardust.instance == :development
5
+ mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ Stardust::GraphQL.define_mutation :add_item do
2
+
3
+ description "Add a new item"
4
+ null false
5
+
6
+ argument :name, :string, required: true
7
+
8
+ field :item, :item, null: true
9
+ field :success, :boolean, null: false
10
+
11
+ def resolve(name:)
12
+ {
13
+ success: true,
14
+ item: {
15
+ id: 4,
16
+ name: name
17
+ }
18
+ }
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,31 @@
1
+ Stardust::GraphQL.define_query :item do
2
+
3
+ def items
4
+ [
5
+ {
6
+ id: 1,
7
+ name: "foo"
8
+ },
9
+ {
10
+ id: 2,
11
+ name: "bar"
12
+ },
13
+ {
14
+ id: 3,
15
+ name: "baz"
16
+ }
17
+ ].freeze
18
+ end
19
+
20
+ description "Get a specific item"
21
+ type :item
22
+ null true
23
+
24
+ argument :id, :integer, required: true
25
+
26
+ def resolve(id:)
27
+ items.select{|i| i[:id] == id}.first
28
+ end
29
+
30
+
31
+ end
@@ -0,0 +1,29 @@
1
+ Stardust::GraphQL.define_query :items do
2
+
3
+ def items
4
+ [
5
+ {
6
+ id: 1,
7
+ name: "foo"
8
+ },
9
+ {
10
+ id: 2,
11
+ name: "bar"
12
+ },
13
+ {
14
+ id: 3,
15
+ name: "baz"
16
+ }
17
+ ].freeze
18
+ end
19
+
20
+ description "Get a list of items"
21
+ type [:item]
22
+ null false
23
+
24
+ def resolve
25
+ items
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,9 @@
1
+ Stardust::GraphQL.define_types do
2
+
3
+ object :item do
4
+ description "An example item"
5
+ field :id, :id, null: false
6
+ field :name, :string, null: false
7
+ end
8
+
9
+ end
@@ -0,0 +1,30 @@
1
+ Stardust::GraphQL.define_mutation :<%= type_name %> do
2
+
3
+ description "TODO: Write a detailed description"
4
+
5
+ ## Optional
6
+ ##
7
+ # argument :id, :integer, required: true,
8
+ # loads: ::MyModel, as: :model
9
+
10
+ ## Required
11
+ ## What fields does this mutation return
12
+ ##
13
+ # field :item, :item, null: true
14
+ # field :error, :error, null: true
15
+
16
+ ## Can this query return nil
17
+ # null true
18
+
19
+
20
+ def resolve
21
+ ## TODO: add your logic here
22
+ ## if has_error
23
+ ## {item: some_item}
24
+ ## else
25
+ ## { error: { message: "Some error" } }
26
+ ## end
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,26 @@
1
+ Stardust::GraphQL.define_query :<%= type_name %> do
2
+
3
+ description "TODO: Write a detailed description"
4
+
5
+ ## Required
6
+ ## What is the return type of this query
7
+ ## type :my_type
8
+
9
+ ## Can this query return nil
10
+ null true
11
+
12
+ ## Optional
13
+ ##
14
+ ## argument :id, :integer, required: true,
15
+ ## loads: ::MyModel, as: :model
16
+ ##
17
+ ## Use named params to have access to any arguments
18
+ ## def resolve(model:)
19
+
20
+
21
+ ## This is the entrypoint for the query
22
+ def resolve
23
+ ## TODO: return something
24
+ end
25
+
26
+ end
@@ -0,0 +1,8 @@
1
+ Stardust::GraphQL.define_types do
2
+
3
+ object :<%= type_name %> do
4
+ description "TODO: Write a detailed description"
5
+ field :id, :id, null: false
6
+ end
7
+
8
+ end
@@ -0,0 +1,13 @@
1
+ module Stardust
2
+ class Configuration
3
+
4
+ def graphql
5
+ @graphql ||= GraphQL::Configuration.new
6
+ end
7
+
8
+ def configure_graphql
9
+ yield graphql
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ require "graphql/rails_logger"
2
+ require 'apollo_upload_server/middleware'
3
+
4
+ module Stardust
5
+ class Engine < ::Rails::Engine
6
+
7
+ config.autoload_paths += Dir[
8
+ "#{config.root}/app/**/",
9
+ "#{config.root}/lib/**/"
10
+ ]
11
+
12
+ DIRS = [
13
+ "app/graph/types",
14
+ "app/graph/queries",
15
+ "app/graph/mutations",
16
+ ].freeze
17
+
18
+ initializer "stardust.graph.draw" do |app|
19
+
20
+ # !!IMPORTANT without this here the logger gets fired before the upload middlware
21
+ app.middleware.use ApolloUploadServer::Middleware
22
+
23
+ # Load graph definitions
24
+ DIRS.each do |dir|
25
+ Dir[Rails.root.join(dir).join("**/*.rb")].each do |file|
26
+ require file
27
+ end
28
+ end
29
+ Stardust::GraphQL::Collector.replace_types!
30
+
31
+ ActiveSupport::Reloader.after_class_unload do
32
+ Stardust::GraphQL::Collector.clear_definitions!
33
+ DIRS.each do |dir|
34
+ Dir[Rails.root.join(dir).join("**/*.rb")].each do |file|
35
+ load file
36
+ end
37
+ end
38
+ Stardust::GraphQL::Collector.replace_types!
39
+ end
40
+ end
41
+
42
+ config.to_prepare do
43
+ # Setup logger
44
+ ::GraphQL::RailsLogger.configure do |config|
45
+ config.white_list = {
46
+ 'Stardust::GraphQLController' => %w(execute)
47
+ }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators'
2
+
3
+ module Stardust
4
+ module Generators
5
+ class Example < Rails::Generators::Base
6
+
7
+ source_root File.expand_path('../../generators/example', __dir__)
8
+
9
+ def copy_initializer_file
10
+ copy_file 'mutations/update_item.rb', 'app/graph/mutations/update_item.rb'
11
+ copy_file 'queries/foo.rb', 'app/graph/queries/foo.rb'
12
+ copy_file 'queries/foos.rb', 'app/graph/queries/foos.rb'
13
+ copy_file 'types/item.rb', 'app/graph/types/item.rb'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ module Stardust
4
+ module Generators
5
+ class Mutation < Rails::Generators::Base
6
+
7
+ argument :path, type: :string
8
+
9
+ source_root File.expand_path('../../generators', __dir__)
10
+
11
+ def generate_type
12
+ template "mutation.template", "app/graph/mutations/#{path.underscore}.rb"
13
+ end
14
+
15
+ private
16
+
17
+ def type_name
18
+ path.underscore.gsub("/", "_")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ module Stardust
4
+ module Generators
5
+ class Query < Rails::Generators::Base
6
+
7
+ argument :path, type: :string
8
+
9
+ source_root File.expand_path('../../generators', __dir__)
10
+
11
+ def generate_type
12
+ template "query.template", "app/graph/queries/#{path.underscore}.rb"
13
+ end
14
+
15
+ private
16
+
17
+ def type_name
18
+ path.underscore.gsub("/", "_")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ module Stardust
4
+ module Generators
5
+ class Type < Rails::Generators::Base
6
+
7
+ argument :path, type: :string
8
+
9
+ source_root File.expand_path('../../generators', __dir__)
10
+
11
+ def generate_type
12
+ template "type.template", "app/graph/types/#{path.underscore}.rb"
13
+ end
14
+
15
+ private
16
+
17
+ def type_name
18
+ path.underscore.split("/").last
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module Stardust
2
+ module Generators
3
+ end
4
+ end
5
+
6
+ require 'stardust/generators/example'
7
+ require 'stardust/generators/type'
8
+ require 'stardust/generators/query'
9
+ require 'stardust/generators/mutation'