1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
# frozen_string_literal: true
require "spec_helper"
require "generators/graphql/install_generator"
class GraphQLGeneratorsInstallGeneratorTest < Rails::Generators::TestCase
tests Graphql::Generators::InstallGenerator
destination File.expand_path("../../../tmp/dummy", File.dirname(__FILE__))
setup do
prepare_destination
FileUtils.cd(File.join(destination_root, '..')) do
`rails new dummy --skip-active-record --skip-test-unit --skip-spring --skip-bundle --skip-webpack-install`
end
end
def refute_file(path)
assert !File.exist?(path), "No file at #{path.inspect}"
end
test "it generates a folder structure" do
run_generator([ "--relay", "false"])
assert_file "app/graphql/types/.keep"
assert_file "app/graphql/mutations/.keep"
assert_file "app/graphql/mutations/base_mutation.rb"
["base_input_object", "base_enum", "base_scalar", "base_union"].each do |base_type|
assert_file "app/graphql/types/#{base_type}.rb"
end
expected_query_route = %|post "/graphql", to: "graphql#execute"|
expected_graphiql_route = %|
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
|
assert_file "config/routes.rb" do |contents|
assert_includes contents, expected_query_route
assert_includes contents, expected_graphiql_route
end
assert_file "app/graphql/resolvers/base_resolver.rb" do |contents|
assert_includes contents, "module Resolvers"
assert_includes contents, "class BaseResolver < GraphQL::Schema::Resolver"
end
assert_file "Gemfile" do |contents|
assert_match %r{gem ('|")graphiql-rails('|"), :?group(:| =>) :development}, contents
end
expected_schema = <<-RUBY
# frozen_string_literal: true
class DummySchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
# For batch-loading (see https://graphql-ruby.org/dataloader/overview.html)
use GraphQL::Dataloader
# GraphQL-Ruby calls this when something goes wrong while running a query:
def self.type_error(err, context)
# if err.is_a?(GraphQL::InvalidNullError)
# # report to your bug tracker here
# return nil
# end
super
end
# Union and Interface Resolution
def self.resolve_type(abstract_type, obj, ctx)
# TODO: Implement this method
# to return the correct GraphQL object type for `obj`
raise(GraphQL::RequiredImplementationMissingError)
end
# Stop validating when it encounters this many errors:
validate_max_errors(100)
end
RUBY
assert_file "app/graphql/dummy_schema.rb", expected_schema
expected_base_mutation = <<-RUBY
# frozen_string_literal: true
module Mutations
class BaseMutation < GraphQL::Schema::RelayClassicMutation
argument_class Types::BaseArgument
field_class Types::BaseField
input_object_class Types::BaseInputObject
object_class Types::BaseObject
end
end
RUBY
assert_file "app/graphql/mutations/base_mutation.rb", expected_base_mutation
expected_query_type = <<-RUBY
# frozen_string_literal: true
module Types
class QueryType < Types::BaseObject
# Add root-level fields here.
# They will be entry points for queries on your schema.
# TODO: remove me
field :test_field, String, null: false,
description: \"An example field added by the generator\"
def test_field
\"Hello World!\"
end
end
end
RUBY
assert_file "app/graphql/types/query_type.rb", expected_query_type
assert_file "app/controllers/graphql_controller.rb", EXPECTED_GRAPHQLS_CONTROLLER
expected_base_field = <<-RUBY
# frozen_string_literal: true
module Types
class BaseField < GraphQL::Schema::Field
argument_class Types::BaseArgument
end
end
RUBY
assert_file "app/graphql/types/base_field.rb", expected_base_field
expected_base_argument = <<-RUBY
# frozen_string_literal: true
module Types
class BaseArgument < GraphQL::Schema::Argument
end
end
RUBY
assert_file "app/graphql/types/base_argument.rb", expected_base_argument
expected_base_object = <<-RUBY
# frozen_string_literal: true
module Types
class BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
end
end
RUBY
assert_file "app/graphql/types/base_object.rb", expected_base_object
expected_base_interface = <<-RUBY
# frozen_string_literal: true
module Types
module BaseInterface
include GraphQL::Schema::Interface
field_class Types::BaseField
end
end
RUBY
assert_file "app/graphql/types/base_interface.rb", expected_base_interface
# Run it again and make sure the gemfile only contains graphiql-rails once
FileUtils.cd(File.join(destination_root)) do
run_generator(["--relay", "false", "--force"])
end
assert_file "Gemfile" do |contents|
assert_equal 1, contents.scan(/graphiql-rails/).length
end
# It doesn't seem like this works on Rails 4, oh well
if Rails::VERSION::STRING > "5"
FileUtils.cd(File.join(destination_root)) do
run_generator(["--relay", "false", "--force"], behavior: :revoke)
end
refute_file "app/graphql/types/base_object.rb"
refute_file "app/graphql/types/base_interface.rb"
refute_file "app/graphql/types/base_argument.rb"
refute_file "app/graphql/types/base_field.rb"
refute_file "app/graphql/types/query_type.rb"
refute_file "app/graphql/dummy_schema.rb"
assert_file "config/routes.rb" do |contents|
refute_includes contents, expected_query_route
# This doesn't work for some reason....
# refute_includes contents, expected_graphiql_route
end
assert_file "Gemfile" do |contents|
refute_match %r{gem ('|")graphiql-rails('|"), :?group(:| =>) :development}, contents
end
end
end
test "it allows for a user-specified install directory" do
run_generator(["--directory", "app/mydirectory", "--relay", "false"])
assert_file "app/mydirectory/types/.keep"
assert_file "app/mydirectory/mutations/.keep"
end
if Rails::VERSION::STRING > "3.9"
# This test doesn't work on Rails 3 because it tries to boot the app
# between the batch and relay generators, but `bundle install`
# hasn't run yet, so graphql-batch isn't present
test "it generates graphql-batch and relay boilerplate" do
run_generator(["--batch"])
assert_file "app/graphql/loaders/.keep"
assert_file "Gemfile" do |contents|
assert_match %r{gem ('|")graphql-batch('|")}, contents
end
expected_query_type = <<-RUBY
# frozen_string_literal: true
module Types
class QueryType < Types::BaseObject
field :node, Types::NodeType, null: true, description: "Fetches an object given its ID." do
argument :id, ID, required: true, description: "ID of the object."
end
def node(id:)
context.schema.object_from_id(id, context)
end
field :nodes, [Types::NodeType, null: true], null: true, description: "Fetches a list of objects given a list of IDs." do
argument :ids, [ID], required: true, description: "IDs of the objects."
end
def nodes(ids:)
ids.map { |id| context.schema.object_from_id(id, context) }
end
# Add root-level fields here.
# They will be entry points for queries on your schema.
# TODO: remove me
field :test_field, String, null: false,
description: \"An example field added by the generator\"
def test_field
\"Hello World!\"
end
end
end
RUBY
assert_file "app/graphql/types/query_type.rb", expected_query_type
assert_file "app/graphql/dummy_schema.rb", EXPECTED_RELAY_BATCH_SCHEMA
end
end
test "it doesn't install graphiql when API Only" do
run_generator(['--api'])
assert_file "Gemfile" do |contents|
refute_includes contents, "graphiql-rails"
end
assert_file "config/routes.rb" do |contents|
refute_includes contents, "GraphiQL::Rails"
end
end
test "it can skip keeps, skip graphiql and customize schema name" do
run_generator(["--skip-keeps", "--skip-graphiql", "--schema=CustomSchema"])
assert_no_file "app/graphql/types/.keep"
assert_no_file "app/graphql/mutations/.keep"
assert_file "app/graphql/types"
assert_file "app/graphql/mutations"
assert_file "Gemfile" do |contents|
refute_includes contents, "graphiql-rails"
end
assert_file "config/routes.rb" do |contents|
refute_includes contents, "GraphiQL::Rails"
end
assert_file "app/graphql/custom_schema.rb", /class CustomSchema < GraphQL::Schema/
assert_file "app/controllers/graphql_controller.rb", /CustomSchema\.execute/
end
test "it can add GraphQL Playground as an IDE through the --playground option" do
run_generator(["--playground"])
assert_file "Gemfile" do |contents|
assert_includes contents, "graphql_playground-rails"
end
expected_playground_route = %|
if Rails.env.development?
mount GraphqlPlayground::Rails::Engine, at: "/playground", graphql_path: "/graphql"
end
|
assert_file "config/routes.rb" do |contents|
assert_includes contents, expected_playground_route
end
end
EXPECTED_GRAPHQLS_CONTROLLER = <<-'RUBY'
# frozen_string_literal: true
class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
# protect_from_forgery with: :null_session
def execute
variables = prepare_variables(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = DummySchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue StandardError => e
raise e unless Rails.env.development?
handle_error_in_development(e)
end
private
# Handle variables in form data, JSON body, or a blank value
def prepare_variables(variables_param)
case variables_param
when String
if variables_param.present?
JSON.parse(variables_param) || {}
else
{}
end
when Hash
variables_param
when ActionController::Parameters
variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{variables_param}"
end
end
def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")
render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
end
end
RUBY
EXPECTED_RELAY_BATCH_SCHEMA = '# frozen_string_literal: true
class DummySchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
# GraphQL::Batch setup:
use GraphQL::Batch
# GraphQL-Ruby calls this when something goes wrong while running a query:
def self.type_error(err, context)
# if err.is_a?(GraphQL::InvalidNullError)
# # report to your bug tracker here
# return nil
# end
super
end
# Union and Interface Resolution
def self.resolve_type(abstract_type, obj, ctx)
# TODO: Implement this method
# to return the correct GraphQL object type for `obj`
raise(GraphQL::RequiredImplementationMissingError)
end
# Stop validating when it encounters this many errors:
validate_max_errors(100)
# Relay-style Object Identification:
# Return a string UUID for `object`
def self.id_from_object(object, type_definition, query_ctx)
# For example, use Rails\' GlobalID library (https://github.com/rails/globalid):
object.to_gid_param
end
# Given a string UUID, find the object
def self.object_from_id(global_id, query_ctx)
# For example, use Rails\' GlobalID library (https://github.com/rails/globalid):
GlobalID.find(global_id)
end
end
'
end
|