graphql_scaffold_fan 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8b89f177d02c073eedb6222ef8caf94defdbde92c973490bb497175ab9ab9c3
4
- data.tar.gz: 4075e692e0ecb7e3416fc2b64c630e6f203b392e39cb1ade162092056d07422a
3
+ metadata.gz: 5ba50341e941f5a2367cb3a550f3701fc59eacbbb550184a9e697229b07e8ae4
4
+ data.tar.gz: 687c142252f59cb524e2e3f01b8bc3aac8ed8cca769659e4cf5171b2c5710de1
5
5
  SHA512:
6
- metadata.gz: e032c47e83be49a405a772d6d4db361ce260fb172995067d245d039f413efb4a88479f9492cd63f94d45fb9acd6d24cccff5e0bc36bbadf2b3e663798c6b2f3d
7
- data.tar.gz: 1d9b2f0c1e049f07d3e8c3882a8d27e10be2a292e5462c1080f771d2c3a99f8bc990773fbc95b9bbcf9d1edd953ea689ce3ddd7a5bbc10ca7b7dd676f6d2b3f1
6
+ metadata.gz: 63d9ae28ce65ecae2f72ccddde40639eb6185624b12ce6a585d48ff8f70d858d73fa923b6ef3b25b9529c849b17f3d425d87e585a4cb145c79faed8267ab4b39
7
+ data.tar.gz: d482fe72bcffba00321c07c0e708864182812655197a415319f8eeca4d695c5d4cf2d6f33edd79ce02a4975c84d3bfe79db938b32bc494be7d469fe07e84ceef
data/.DS_Store CHANGED
Binary file
data/Gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in graphql_scaffold_fan.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem 'rake', '~> 13.0'
data/README.md CHANGED
@@ -32,4 +32,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
32
 
33
33
  ## Contributing
34
34
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql_scaffold_fan.
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/FanZhangYYDSPixelforce/graphql_scaffold.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
3
+ require 'bundler/gem_tasks'
4
4
  task default: %i[]
data/bin/console CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "graphql_scaffold_fan"
4
+ require 'bundler/setup'
5
+ require 'graphql_scaffold_fan'
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
@@ -11,5 +11,5 @@ require "graphql_scaffold_fan"
11
11
  # require "pry"
12
12
  # Pry.start
13
13
 
14
- require "irb"
14
+ require 'irb'
15
15
  IRB.start(__FILE__)
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/graphql_scaffold_fan/version"
3
+ require_relative 'lib/graphql_scaffold_fan/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "graphql_scaffold_fan"
6
+ spec.name = 'graphql_scaffold_fan'
7
7
  spec.version = GraphqlScaffoldFan::VERSION
8
- spec.authors = ["Fan Zhang"]
9
- spec.email = ["fan.zhang@pixelforce.com.au"]
8
+ spec.authors = ['Fan Zhang']
9
+ spec.email = ['fan.zhang@pixelforce.com.au']
10
10
 
11
- spec.summary = "Helping creating graphql files"
12
- spec.description = "Helping creating graphql files"
11
+ spec.summary = 'Helping creating graphql files'
12
+ spec.description = 'Helping creating graphql files'
13
13
  spec.homepage = ''
14
14
 
15
15
  # Specify which files should be added to the gem when it is released.
@@ -17,13 +17,13 @@ Gem::Specification.new do |spec|
17
17
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
18
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
19
19
  end
20
- spec.bindir = "exe"
20
+ spec.bindir = 'exe'
21
21
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
22
+ spec.require_paths = ['lib']
23
23
 
24
24
  # Uncomment to register a new dependency of your gem
25
25
  # spec.add_dependency "example-gem", "~> 1.0"
26
26
 
27
27
  # For more information and examples about making a new gem, checkout our
28
28
  # guide at: https://bundler.io/guides/creating_gem.html
29
- end
29
+ end
@@ -0,0 +1,46 @@
1
+ require 'rails/generators/base'
2
+ module GraphqlScaffold
3
+ module Generators
4
+ class InitGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path('../templates', __dir__)
6
+
7
+ def create_ListMetadataType_file
8
+ dir_path = 'app/graphql/types/admin'
9
+ file_path = dir_path + '/list_metadata_type.rb'
10
+ FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
11
+
12
+ if File.exist?(file_path)
13
+ p 'File list_metadata_type.rb already exists'
14
+ else
15
+ template 'list_metadata_type.haml', file_path
16
+ end
17
+ end
18
+
19
+ def create_admin_schema_file
20
+ @top_folder_name = File.basename(Dir.getwd)
21
+ dir_path = 'app/graphql'
22
+ file_path = dir_path + "#{@top_folder_name.underscore}_admin_schema.rb"
23
+ FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
24
+
25
+ if File.exist?(file_path)
26
+ p "File #{@top_folder_name}_admin_schema.rb already exists"
27
+ else
28
+ template 'admin_schema.haml', file_path
29
+ end
30
+ end
31
+
32
+ def create_admin_controller_file
33
+ @top_folder_name = File.basename(Dir.getwd)
34
+ dir_path = 'app/controllers'
35
+ file_path = dir_path + '/admin_graphql_controller.rb'
36
+ FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
37
+
38
+ if File.exist?(file_path)
39
+ p 'File admin_graphql_controller.rb already exists'
40
+ else
41
+ template 'admin_controller.haml', file_path
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -4,17 +4,44 @@ module GraphqlScaffold
4
4
  class MutationGenerator < Rails::Generators::NamedBase
5
5
  source_root File.expand_path('../templates', __dir__)
6
6
 
7
+ def create_base_admin_mutation_file
8
+ mutations_dir_path = 'app/graphql/mutations'
9
+ generator_path = mutation_type_dir_path + '/base_admin_mutation.rb'
10
+
11
+ FileUtils.mkdir_p(mutation_type_dir_path) unless File.exist?(mutation_type_dir_path)
12
+
13
+ if File.exist?(generator_path)
14
+ p 'File base_admin_mutation.rb already exists'
15
+ else
16
+ template 'base_admin_mutation.haml', generator_path
17
+ end
18
+ end
19
+
7
20
  def create_admin_mutation_folders
8
21
  admin_mutation_dir_path = 'app/graphql/mutations/admin'
9
22
  generator_dir_path = admin_mutation_dir_path + ("/#{@module_name.underscore}" if @module_name.present?).to_s
10
23
  @generator_path = generator_dir_path + "/#{file_name}s"
11
24
 
25
+ admin_mutation_test_dir_path = 'spec/graphql/mutations/admin'
26
+ generator_test_dir_path = admin_mutation_test_dir_path + (if @module_name.present?
27
+ "/#{@module_name.underscore}"
28
+ end).to_s
29
+ @generator_test_path = generator_test_dir_path + "/#{file_name}s"
30
+
12
31
  FileUtils.mkdir_p(admin_mutation_dir_path) unless File.exist?(admin_mutation_dir_path)
32
+ FileUtils.mkdir_p(admin_mutation_test_dir_path) unless File.exist?(admin_mutation_test_dir_path)
33
+
13
34
  if File.exist?(@generator_path)
14
35
  p 'Folder already exist'
15
36
  elsif Dir.mkdir(@generator_path)
16
37
  p 'Folder created successfully'
17
38
  end
39
+
40
+ if File.exist?(@generator_test_path)
41
+ p 'Mutation Test Folder already exist'
42
+ elsif Dir.mkdir(@generator_test_path)
43
+ p 'Mutation Test Folder created successfully'
44
+ end
18
45
  end
19
46
 
20
47
  def create_admin_mutation_create_file
@@ -26,6 +53,15 @@ module GraphqlScaffold
26
53
  template 'mutation_create.haml', create_file_path
27
54
  end
28
55
  end
56
+
57
+ if File.exist?(@generator_test_path)
58
+ create_test_file_path = @generator_test_path + "/create_#{file_name}_spec.rb"
59
+ if File.exist?(create_test_file_path)
60
+ p 'Mutation_Create_Test File already exist'
61
+ else
62
+ template 'mutation_create_test.haml', create_test_file_path
63
+ end
64
+ end
29
65
  end
30
66
 
31
67
  def create_admin_mutation_update_file
@@ -37,6 +73,15 @@ module GraphqlScaffold
37
73
  template 'mutation_update.haml', update_file_path
38
74
  end
39
75
  end
76
+
77
+ if File.exist?(@generator_test_path)
78
+ update_test_file_path = @generator_test_path + "/update_#{file_name}_spec.rb"
79
+ if File.exist?(update_test_file_path)
80
+ p 'Mutation_Update_Test File already exist'
81
+ else
82
+ template 'mutation_update_test.haml', update_test_file_path
83
+ end
84
+ end
40
85
  end
41
86
 
42
87
  def create_admin_mutation_delete_file
@@ -48,6 +93,15 @@ module GraphqlScaffold
48
93
  template 'mutation_delete.haml', delete_file_path
49
94
  end
50
95
  end
96
+
97
+ if File.exist?(@generator_test_path)
98
+ delete_test_file_path = @generator_test_path + "/delete_#{file_name}_spec.rb"
99
+ if File.exist?(delete_test_file_path)
100
+ p 'Mutation_Delete_Test File already exist'
101
+ else
102
+ template 'mutation_delete_test.haml', delete_test_file_path
103
+ end
104
+ end
51
105
  end
52
106
 
53
107
  def create_mutation_type_file
@@ -77,4 +131,4 @@ module GraphqlScaffold
77
131
  end
78
132
  end
79
133
  end
80
- end
134
+ end
@@ -9,7 +9,7 @@ module GraphqlScaffold
9
9
 
10
10
  src_file = "app/graphql/types/#{file_name}_type.rb"
11
11
  dst_dir = 'app/graphql/types/admin'
12
- if File.exist? ("app/graphql/types/#{file_name}_type.rb")
12
+ if File.exist?("app/graphql/types/#{file_name}_type.rb")
13
13
  FileUtils.mv(src_file, dst_dir)
14
14
  file = File.open("#{dst_dir}/#{file_name}_type.rb")
15
15
  file_data = file.read
@@ -0,0 +1,64 @@
1
+ class AdminGraphqlController < ApplicationController
2
+ include GraphqlDevise::Concerns::SetUserByToken
3
+
4
+ attr_reader :current_resource
5
+
6
+ # If accessing from outside this domain, nullify the session
7
+ # This allows for outside API access while preventing CSRF attacks,
8
+ # but you'll have to authenticate your user separately
9
+ protect_from_forgery with: :null_session
10
+ after_action :track_action
11
+
12
+ def execute
13
+ variables = prepare_variables(params[:variables])
14
+ query = params[:query]
15
+ operation_name = params[:operationName]
16
+
17
+ context = gql_devise_context(AdminUser)
18
+
19
+ @current_resource = context[:current_resource]
20
+
21
+ result = <%= @top_folder_name.camelize %>AdminSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
22
+ render json: result
23
+ rescue StandardError => e
24
+ raise e unless Rails.env.development?
25
+
26
+ handle_error_in_development(e)
27
+ end
28
+
29
+ private
30
+
31
+ # Handle variables in form data, JSON body, or a blank value
32
+ def prepare_variables(variables_param)
33
+ case variables_param
34
+ when String
35
+ if variables_param.present?
36
+ JSON.parse(variables_param) || {}
37
+ else
38
+ {}
39
+ end
40
+ when Hash
41
+ variables_param
42
+ when ActionController::Parameters
43
+ variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
44
+ when nil
45
+ {}
46
+ else
47
+ raise ArgumentError, "Unexpected parameter: #{variables_param}"
48
+ end
49
+ end
50
+
51
+ def handle_error_in_development(err)
52
+ logger.error err.message
53
+ logger.error err.backtrace.join("\n")
54
+
55
+ render json: { errors: [{ message: err.message, backtrace: err.backtrace }], data: {} }, status: :internal_server_error
56
+ end
57
+
58
+ def track_action
59
+ variables = prepare_variables(params[:variables])
60
+ query = params[:query]
61
+ operation_name = params[:operationName]
62
+ ahoy.track 'Admin Portal', query: query, variables: variables, operation_name: operation_name
63
+ end
64
+ end
@@ -1,7 +1,7 @@
1
1
  module Types
2
2
  module Inputs
3
3
  module Admin
4
- class <%= class_name %> < Types::BaseInputObject
4
+ class <%= class_name %>Filter < Types::BaseInputObject
5
5
  argument :ids, [ID], required: false
6
6
 
7
7
  def prepare
@@ -0,0 +1,42 @@
1
+ class <%= @top_folder_name.camelize %>AdminSchema < GraphQL::Schema
2
+ use GraphQL::Tracing::NewRelicTracing, set_transaction_name: true
3
+ use GraphqlDevise::SchemaPlugin.new(
4
+ query: Types::Query::AdminUserQueryType,
5
+ mutation: Types::Mutation::AdminUserMutationType,
6
+ resource_loaders: [
7
+ GraphqlDevise::ResourceLoader.new(AdminUser, only: %i[login logout])
8
+ ]
9
+ )
10
+
11
+ mutation(Types::Mutation::AdminUserMutationType)
12
+ query(Types::Query::AdminUserQueryType)
13
+
14
+ use GraphQL::Batch
15
+
16
+ # Union and Interface Resolution
17
+ def self.resolve_type(_abstract_type, _obj, _ctx)
18
+ # TODO: Implement this function
19
+ # to return the correct object type for `obj`
20
+ raise(GraphQL::RequiredImplementationMissingError)
21
+ end
22
+
23
+ # Relay-style Object Identification:
24
+
25
+ # Return a string UUID for `object`
26
+ def self.id_from_object(object, type_definition, _query_ctx)
27
+ # Here's a simple implementation which:
28
+ # - joins the type name & object.id
29
+ # - encodes it with base64:
30
+ GraphQL::Schema::UniqueWithinType.encode(type_definition.name, object.id)
31
+ end
32
+
33
+ # Given a string UUID, find the object
34
+ def self.object_from_id(id, _query_ctx)
35
+ # For example, to decode the UUIDs generated above:
36
+ type_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)
37
+ #
38
+ # Then, based on `type_name` and `id`
39
+ # find an object in your application
40
+ # ...
41
+ end
42
+ end
@@ -0,0 +1,4 @@
1
+ module Mutations
2
+ class BaseAdminMutation < GraphQL::Schema::Mutation
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module Types
2
+ module Admin
3
+ class ListMetadataType < Types::BaseObject
4
+ field :count, Integer, null: false
5
+ end
6
+ end
7
+ end
@@ -4,8 +4,13 @@ module Mutations
4
4
 
5
5
  type Types::Admin::<%= class_name %>Type
6
6
 
7
- def resolve(id:, **args)
8
-
7
+ def resolve(**args)
8
+ <%= class_name.underscore %> = <%= class_name %>.new(args)
9
+ if <%= class_name.underscore %>.save
10
+ <%= class_name.underscore %>
11
+ else
12
+ context.add_error(GraphQL::ExecutionError.new( <%= class_name.underscore %>.errors.full_messages.join(', ')))
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -0,0 +1,32 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe Mutations::Admin::<%= class_name %>s::Create<%= class_name %>, type: :request do
4
+ let(:admin_user) { create(:admin_user) }
5
+
6
+ describe '.resolve' do
7
+ it 'should create' do
8
+ variables = <<~GQL
9
+ {
10
+ }
11
+ GQL
12
+
13
+ expect do
14
+ sign_in admin_user
15
+ post '/api/admin/graphql', headers: admin_user.create_new_auth_token, params: { query: query, variables: variables }
16
+ expect(response).to have_http_status(:success)
17
+ json = JSON.parse(response.body)
18
+ data = json['data']['create<%= class_name %>']
19
+ end.to change { <%= class_name %>.count }.by(1)
20
+ end
21
+ end
22
+
23
+ def query
24
+ <<~GQL
25
+ mutation create<%= class_name %>() {
26
+ create<%= class_name %>(){
27
+ id
28
+ }
29
+ }
30
+ GQL
31
+ end
32
+ end
@@ -4,8 +4,10 @@ module Mutations
4
4
 
5
5
  type Types::Admin::<%= class_name %>Type
6
6
 
7
- def resolve(id:, **args)
8
-
7
+ def resolve(id:)
8
+ <%= class_name.underscore %> = ::<%= class_name %>.find(id)
9
+ <%= class_name.underscore %>.destroy!
10
+ <%= class_name.underscore %>
9
11
  end
10
12
  end
11
13
  end
@@ -0,0 +1,37 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe Mutations::Admin::<%= class_name %>s::Delete<%= class_name %>, type: :request do
4
+ let(:admin_user) { create :admin_user }
5
+ let(:<%= class_name.underscore %>) { create :<%= class_name.underscore %> }
6
+
7
+ describe '.resolve' do
8
+ it 'should delete' do
9
+ admin_user = create(:admin_user)
10
+ sign_in admin_user
11
+
12
+ variables = <<~GQL
13
+ {
14
+ "id": "#{<%= class_name.underscore %>.id}"
15
+ }
16
+ GQL
17
+
18
+ headers = admin_user.create_new_auth_token
19
+
20
+ post '/api/admin/graphql', headers: headers, params: { query: query, variables: variables }
21
+ expect(response).to have_http_status(:success)
22
+ json = JSON.parse(response.body)
23
+
24
+ expect { <%= class_name %>.find(<%= class_name.underscore %>.id) }.to raise_error ActiveRecord::RecordNotFound
25
+ end
26
+ end
27
+
28
+ def query
29
+ <<~GQL
30
+ mutation delete<%= class_name %>($id: ID!) {
31
+ delete<%= class_name %>(id: $id){
32
+ id
33
+ }
34
+ }
35
+ GQL
36
+ end
37
+ end
@@ -5,7 +5,12 @@ module Mutations
5
5
  type Types::Admin::<%= class_name %>Type
6
6
 
7
7
  def resolve(id:, **args)
8
-
8
+ <%= class_name.underscore %> = <%= class_name %>.find(id)
9
+ if <%= class_name.underscore %>.update(args)
10
+ <%= class_name.underscore %>
11
+ else
12
+ context.add_error(GraphQL::ExecutionError.new(<%= class_name.underscore %>.errors.full_messages.join(', ')))
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe Mutations::Admin::<%= class_name %>s::Update<%= class_name %>, type: :request do
4
+ let(:admin_user) { create :admin_user }
5
+ let(:<%= class_name.underscore %>) { create :<%= class_name.underscore %> }
6
+
7
+ describe '.resolve' do
8
+ it 'should update' do
9
+ sign_in admin_user
10
+
11
+ variables = <<~GQL
12
+ {
13
+ "id": "#{<%= class_name.underscore %>.id}"
14
+ }
15
+ GQL
16
+
17
+ post '/api/admin/graphql', headers: admin_user.create_new_auth_token, params: { query: query, variables: variables }
18
+ expect(response).to have_http_status(:success)
19
+ json = JSON.parse(response.body)
20
+
21
+ data = json['data']['update<%= class_name %>']
22
+ expect(data['id']).to eq <%= class_name.underscore %>.id.to_s
23
+ end
24
+ end
25
+
26
+ def query
27
+ <<~GQL
28
+ mutation update<%= class_name %>($id: ID!) {
29
+ update<%= class_name %>(id: $id){
30
+ id
31
+ }
32
+ }
33
+ GQL
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlScaffoldFan
4
- VERSION = "0.1.2"
4
+ VERSION = '0.1.3'
5
5
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "graphql_scaffold_fan/version"
3
+ require_relative 'graphql_scaffold_fan/version'
4
4
 
5
5
  module GraphqlScaffold
6
6
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_scaffold_fan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fan Zhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-21 00:00:00.000000000 Z
11
+ date: 2022-06-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Helping creating graphql files
14
14
  email:
@@ -26,15 +26,23 @@ files:
26
26
  - bin/console
27
27
  - bin/setup
28
28
  - graphql_scaffold_fan.gemspec
29
+ - lib/generators/graphql_scaffold/init_generator.rb
29
30
  - lib/generators/graphql_scaffold/mutation_generator.rb
30
31
  - lib/generators/graphql_scaffold/type_generator.rb
32
+ - lib/generators/templates/admin_controller.haml
31
33
  - lib/generators/templates/admin_filter.haml
34
+ - lib/generators/templates/admin_schema.haml
35
+ - lib/generators/templates/base_admin_mutation.haml
32
36
  - lib/generators/templates/base_object.haml
37
+ - lib/generators/templates/list_metadata_type.haml
33
38
  - lib/generators/templates/mutation_create.haml
39
+ - lib/generators/templates/mutation_create_test.haml
34
40
  - lib/generators/templates/mutation_delete.haml
41
+ - lib/generators/templates/mutation_delete_test.haml
35
42
  - lib/generators/templates/mutation_test.haml
36
43
  - lib/generators/templates/mutation_type.haml
37
44
  - lib/generators/templates/mutation_update.haml
45
+ - lib/generators/templates/mutation_update_test.haml
38
46
  - lib/generators/templates/query_test.haml
39
47
  - lib/generators/templates/query_type.haml
40
48
  - lib/graphql_scaffold_fan.rb