GraphiQL breaks with an scaffolded mutation in Rails - ruby-on-rails

Building a GraphQL API with rails, I scaffolded a create mutation for one of my records like so:
$ rails g graphql:mutation_create event
This added the mutation type like so in app/graphql/types/mutation_type.rb:
field :event_create, mutation: Mutations::EventCreate
And the mutation as follows in app/graphql/mutations/event_create.rb:
# frozen_string_literal: true
module Mutations
class EventCreate < BaseMutation
description "Creates a new event"
field :event, Types::EventType, null: false
argument :event_input, Types::EventInputType, required: true
def resolve(event_input:)
event = ::Event.new(**event_input)
raise GraphQL::ExecutionError.new "Error creating event", extensions: event.errors.to_hash unless event.save
{ event: event }
end
end
end
But I get the following error when loading up the GraphiQL, notice this just appears when loading the GraphiQL, it has nothing to do with me trying the mutation on hand (I've scaffolded the delete mutation and works like a charm):
{
"message": "Invalid or incomplete introspection result. Ensure that you are passing \"data\" property of introspection response and no \"errors\" was returned alongside: {}.",
"stack": "Error: Invalid or incomplete introspection result. Ensure that you are passing \"data\" property of introspection response and no \"errors\" was returned alongside: {}.\n at devAssert (webpack-internal:///../../../node_modules/graphql/jsutils/devAssert.mjs:7:11)\n at buildClientSchema (webpack-internal:///../../../node_modules/graphql/utilities/buildClientSchema.mjs:41:266)\n at eval (webpack-internal:///./components/GraphiQL.tsx:1133:57)"
}
Any idea what's going on?
PS. Apparently it happens with the update mutation as well when scaffolded. If I comment those mutations out from the mutation type the error disappears.

Related

improper jbuilder syntax for output during API error handling

The following is a json.jbuildermethod intended to respond to an API call.
if #error_code_98?
json.set! :chain_category do
json.set! :error, #error_98_messages
end
end
#error_code_99 = true when said errors arise.
error_98_messages is a concatenation of string that provide an indentifier with the nature of the error. They are accumulated as an array of items are handled by the API action.
When the error hits, the instance variable gets populated, but at the end of the action the return message this the following error
syntax error, unexpected tSYMBEG, expecting ':'
json.set! :chain_category do
I thought this was respecting the syntax for dynamic attribution, but that is mistaken. Where is the syntax off?

Using union types with flow sensitivity

I'm trying to use union types with is_a? for flow control, but I'm still getting sorbet errors. I've tried casting as well, and I'm still running into the same error, which is:
Method to_hash does not exist on T::Array[T.untyped] component of T.any(T::Array[T.untyped], T::Hash[Symbol, T.untyped]) https://srb.help/7003
I have the following struct:
class PostProcessingMethod < T::Struct
prop :method_name, Symbol
prop :args, T.any(Array, T::Hash[Symbol, T.untyped]), default: []
prop :changed_fields, T::Array[String], default: []
prop :all, T::Boolean, default: false
prop :force, T::Boolean, default: false
end
and I'm using it in a method that (currently) looks like this:
sig { params(post_processing_methods: T::Array[Documents::PostProcessingMethod]).void }
def call(post_processing_methods)
post_processing_methods.each do |post_processing_method|
next unless should_call_method?(post_processing_method)
if #object.respond_to?(post_processing_method.method_name)
if post_processing_method.args.is_a?(Array)
#object.send(post_processing_method.method_name, *post_processing_method.args)
elsif post_processing_method.args.is_a?(Hash)
#object.send(post_processing_method.method_name, **post_processing_method.args)
end
end
end
end
I've tried incorporating T.cast to ensure that sorbet knows it's a Hash in the elsif, but that doesn't seem to have made a difference.
My expectation is that is_a? should allow sorbet to know that post_processing_method is a Hash in the elseif. But if that's not the case, the T.cast should certainly handle this.
In the code posted, each .args call is treated like a new variable. If you capture the returning value in a local variable, the flow sensitivity will work.
See an example on sorbet.run

RSpec equality matcher failing for serializer test

I am writing a test for one of my Active Model Serializers to make sure that the JSON output is what I expect. However, I cannot figure out why RSpec is parsing my 'expected' output to leave out my array of testjobs, and I do not understand why I cannot get 'expected' and 'got' outputs to equal each other. At one point, I even copy-pasted the 'got' result to my 'expected' input and still received a failure message that the two strings were not equal. However, when I compared those two strings in REPL using ==, the output was true. How do I resolve these issues to get an effective test?
RSpec Error:
Failures:
1) TestrunSerializer creates special JSON for the API
Failure/Error: expect(serializer.to_json).to eq('{"testrun":{"id":1,"run_at":null,"started_at":null,"state":"pending","completed_at":null,"testjobs":[{"id":2,"active":false,"testchunk_id":2,"testrun_id":1,"testchunk_name":"flair","testchunk":{"id":15,"name":"flair"}}],"branch":{"id":1,"name":"dev","repository":{"id":321,"url":"fakeurl.com"}}}}')
expected: "{\"testrun\":{\"id\":1,\"run_at\":null,\"started_at\":null,\"state\":\"pending\",\"completed_at\":nu...r\"}}],\"branch\":{\"id\":1,\"name\":\"dev\",\"repository\":{\"id\":321,\"url\":\"fakeurl.com\"}}}}"
got: "{\"testrun\":{\"id\":1,\"run_at\":null,\"started_at\":null,\"state\":\"pending\",\"completed_at\":nu...s\":[],\"branch\":{\"id\":1,\"name\":\"dev\",\"repository\":{\"id\":321,\"url\":\"fakeurl.com\"}}}}"
(compared using ==)
# ./spec/serializers/testrun_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 0.79448 seconds (files took 5.63 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/serializers/testrun_spec.rb:8 # TestrunSerializer creates special JSON for the API
Here is the RSpec test:
require 'rails_helper'
describe TestrunSerializer, type: :serializer do
let(:repo) { Repository.create(id: 321, url: "fakeurl.com") }
let(:branch) { Branch.create(id: 1,name: "dev", repository_id: repo.id) }
let(:testchunk) { Testchunk.create(id: 15, name: "flair") }
it "creates special JSON for the API" do
serializer = TestrunSerializer.new Testrun.new("id":1, name: "name", "run_at": nil, state: "pending", branch_id: branch.id)
testjob = Testjob.create(id: 8, active: false, testchunk_id: testchunk.id, testrun_id: 1)
expect(serializer.to_json).to eq('{"testrun":{"id":1,"run_at":null,"started_at":null,"state":"pending","completed_at":null,"testjobs":[{"id":2,"active":false,"testchunk_id":2,"testrun_id":1,"testchunk_name":"flair","testchunk":{"id":15,"name":"flair"}}],"branch":{"id":1,"name":"dev","repository":{"id":321,"url":"fakeurl.com"}}}}')
end
end
Here is the actual serializer:
class TestrunSerializer < ActiveModel::Serializer
attributes :id, :run_at, :started_at, :state, :completed_at, :testjobs
has_many :testjobs
has_one :branch
end
Technologies used: Rails 5.1, RSpec 3.6, Ruby 2.4
It looks like your testjobs are not matching
completed_at\":nu...r\"}}],\"branch\"
vs
completed_at\":nu...s\":[],
You should set up your specs so the testjobs are returned as well.
Please note that the diff string is cut in the middle - this is one of the most annoying part of eq matcher when used with strings.
Edit: You may wont to switch to comparing arrays/hashes instead of strings to get better diffs. expect(serializer).to eq {testrun: "..."} (Drop to_json in your assertions)
The reason why your test didn't pass is trivial: inside the it block, you assigned the Testrun id (1) while creating the Testjob record, but the Testrun record does not exist.
SomeActiveRecord.new() will not create any actual record until you invoke save() on it, or you can just invoke SomeActiveRecord.create for that.
some_active_record = SomeActiveRecord.new(...)
some_active_record.save
# or
some_active_record = SomeActiveRecord.create(...)
So the final solution may look something like:
it "creates special JSON for the API" do
testrun = Testrun.create(id: 1, name: "name", run_at: nil, state: "pending", branch_id: branch.id)
serializer = TestrunSerializer.new(testrun)
testjob = Testjob.create(id: 8, active: false, testchunk_id: testchunk.id, testrun_id: testrun.id)
expect(serializer.to_json).to eq('{"testrun":{"id":1,"run_at":null,"started_at":null,"state":"pending","completed_at":null,"testjobs":[{"id":2,"active":false,"testchunk_id":2,"testrun_id":1,"testchunk_name":"flair","testchunk":{"id":15,"name":"flair"}}],"branch":{"id":1,"name":"dev","repository":{"id":321,"url":"fakeurl.com"}}}}')
end
Improvement Scope:
Please have a look at the tests for :json adapter in the active_model_serializers repo: https://github.com/rails-api/active_model_serializers/blob/v0.10.6/test/action_controller/json/include_test.rb.
You can easily convert the tests to suite with rspec.
If you want to test the json output, then you should put the tests under controller or request specs; rather than in serializers. Because rendering json is the responsibility of the adapter; serializers merely feed the adapter with all the attributes and associations defined in them.
Working solution:
I added the line
serializer.testjobs << testjob
to explicitly associate the testjob with the object, and the test now passes.

Rails access hash value

I'm playing around with Netflix's Workflowable gem. Right now I'm working on making a custom action where the user can choose choices.
I end up pulling {"id":1,"value":"High"} out with #options[:priority][:value]
What I want to do is get the id value of 1. Any idea how to pull that out? I tried #options[:priority][:value][:id] but that seems to through an error.
Here's what the action looks like/how I'm logging the value:
class Workflowable::Actions::UpdateStatusAction < Workflowable::Actions::Action
include ERB::Util
include Rails.application.routes.url_helpers
NAME="Update Status Action"
OPTIONS = {
:priority => {
:description=>"Enter priority to set result to",
:required=>true,
:type=>:choice,
:choices=>[{id: 1, value: "High"} ]
}
}
def run
Rails.logger.debug #options[:priority][:value]
end
end
Here's the error:
Error (3a7b2168-6f24-4837-9221-376b98e6e887): TypeError in ResultsController#flag
no implicit conversion of Symbol into Integer
Here's what #options[:priority] looks like:
{"description"=>"Enter priority to set result to", "required"=>true, "type"=>:choice, "choices"=>[{"id"=>1, "value"=>"High"}], "value"=>"{\"id\":1,\"value\":\"High\"}", "user_specified"=>true}
#options[:priority]["value"] looks to be a strong containing json, not a hash. This is why you get an error when using [:id] (this method doesn't accept symbols) and why ["id"] returns the string "id".
You'll need to parse it first, for example with JSON.parse, at which point you'll have a hash which you should be able to access as normal. By default the keys will be strings so you'll need
JSON.parse(value)["id"]
I'm assuming the error is something like TypeError: no implicit conversion of Symbol into Integer
It looks like #options[:priority] is a hash with keys :id and :value. So you would want to use #options[:priority][:id] (lose the :value that returns the string).

building a ruby conditional to check for nested params

I'm curious in an controller action, how I can do some simple validation of nested params?
def create
# validate incoming post request
errors = Array.new
person = params[:person]
event = params[:event]
errors << "person email should not be empty" if person[:email].blank?
errors << "person name should not be empty" if person[:name].blank?
errors << "event name should not be empty" if event[:name].blank?
This type of check is barfing. I'm trying to scan for some nested json params, so for example making a post request on
"person":
{
"email":"foo#gmail.com",
"name":"foo"
},
This will validate fine because the nested name is there. Although if I do a request without the nested value, it will barf. How could I write a conditional to check for the nested value, and only stuff in the error value if it's empty. Otherwise, if there is no nested value just continue as normal.
You could use the has_key? method available on Hash class.
errors << "person email should not be empty" if person.has_key?(:email) && person[:email].blank?

Resources