Created
November 10, 2023 16:17
-
-
Save mpapis/38abc6b0218405a1684d2f623a052ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', '~> 7.1.0' | |
gem 'graphql', '~> 2.1' | |
gem 'rspec', '~> 3.12' | |
end | |
require 'action_controller' | |
class AuthArgument < GraphQL::Schema::Argument | |
def authorized?(_object, arg_value, _context) = arg_value != 'Unauthorized' | |
end | |
class HeloResolver < GraphQL::Schema::Resolver | |
argument_class AuthArgument | |
argument :name, String, required: true | |
type String, null: false | |
def resolve(name:) = "Hello #{name}!" | |
end | |
class QueryType < GraphQL::Schema::Object | |
field :hello, resolver: HeloResolver | |
end | |
class GraphqlAppSchema < GraphQL::Schema | |
query QueryType | |
rescue_from(StandardError) { |e| raise GraphQL::ExecutionError, e.message } | |
end | |
class GraphqlController < ActionController::Base | |
def execute = render(json: GraphqlAppSchema.execute(params[:query], variables: params[:variables])) | |
end | |
class GraphqlApp < Rails::Application | |
config.load_defaults 7.1 | |
config.eager_load = true | |
config.hosts << 'www.example.com' | |
config.logger = Logger.new(STDERR) | |
config.secret_key_base = 'x' * 30 | |
routes.append { root to: 'graphql#execute', via: :all } | |
end | |
GraphqlApp.initialize! | |
RSpec.describe GraphqlController, :aggregate_failures do | |
include ActionDispatch::Integration::Runner | |
def app = GraphqlApp | |
it 'welcomes' do | |
post '/', params: { query: 'query($name: String!) { hello(name: $name) }', variables: { name: 'GraphQL' } } | |
expect(response).to be_successful | |
expect(response.body).to eq('{"data":{"hello":"Hello GraphQL!"}}') | |
end | |
it 'is unauthorized' do | |
post '/', params: { query: 'query($name: String!) { hello(name: $name) }', variables: { name: 'Unauthorized' } } | |
expect(response).to be_successful | |
expect(response.body).to eq('{"data":null,"errors":[{"message":"Unauthorized","locations":[{"line":1,"column":8}],"path":["hello"]}]}') | |
end | |
end | |
RSpec::Core::Runner.autorun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fetching gem metadata from https://rubygems.org/........... | |
Resolving dependencies... | |
... | |
Using graphql 2.1.6 | |
... | |
Using rails 7.1.1 | |
I, [2023-11-10T17:11:39.972894 #2370] INFO -- : Started POST "/" for 127.0.0.1 at 2023-11-10 17:11:39 +0100 | |
.I, [2023-11-10T17:11:40.031957 #2370] INFO -- : Started POST "/" for 127.0.0.1 at 2023-11-10 17:11:40 +0100 | |
F | |
Failures: | |
1) GraphqlController fails | |
Failure/Error: expect(response.body).to eq('{"data":null,"errors":[{"message":"Unauthorized","locations":[{"line":1,"column":8}],"path":["hello"]}]}') | |
expected: "{\"data\":null,\"errors\":[{\"message\":\"Unauthorized\",\"locations\":[{\"line\":1,\"column\":8}],\"path\":[\"hello\"]}]}" | |
got: "{\"data\":null,\"errors\":[{\"message\":\"Cannot return null for non-nullable field Query.hello\"}]}" | |
(compared using ==) | |
# confusing_supresed_auth_error.rb:51:in `block (2 levels) in <main>' | |
Finished in 0.08227 seconds (files took 0.3399 seconds to load) | |
2 examples, 1 failure | |
Failed examples: | |
rspec confusing_supresed_auth_error.rb:48 # GraphqlController fails | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment