Skip to content

GraphQL::Analysis::AST::FieldUsage deprecated argument detections for mutations raises an error when the InputType value is prepared #4864

@jrichardlai

Description

@jrichardlai

Describe the bug

GraphQL::Analysis::AST::FieldUsage deprecated argument detections for mutations is raising an error when the InputType value is prepared.

Versions

graphql version: 2.2.11

Steps to reproduce

diff --git a/spec/graphql/analysis/ast/field_usage_spec.rb b/spec/graphql/analysis/ast/field_usage_spec.rb
index 6e54ac38d..75e5dd635 100644
--- a/spec/graphql/analysis/ast/field_usage_spec.rb
+++ b/spec/graphql/analysis/ast/field_usage_spec.rb
@@ -254,6 +254,17 @@ describe GraphQL::Analysis::AST::FieldUsage do
     end
   end

+  describe "mutation with deprecated arguments with prepared values" do
+    let(:query_string) {%|
+      mutation {
+        pushValue(preparedTestInput: { deprecatedDate: "2020-10-10" })
+      }
+    |}
+
+    it "keeps track of nested deprecated arguments" do
+      assert_equal ['PreparedDateInput.deprecatedDate'], result[:used_deprecated_arguments]
+    end
+  end

   describe "when an argument prepare raises a GraphQL::ExecutionError" do
     class ArgumentErrorFieldUsageSchema < GraphQL::Schema
diff --git a/spec/support/dummy/schema.rb b/spec/support/dummy/schema.rb
index 206142cdb..d88b2d2ab 100644
--- a/spec/support/dummy/schema.rb
+++ b/spec/support/dummy/schema.rb
@@ -265,6 +265,18 @@ module Dummy
     argument :old_source, String, required: false, deprecation_reason: "No longer supported"
   end

+  class PreparedDateInput < BaseInputObject
+    description "Input with prepared value"
+    argument :date, String, description: "date as a string", required: false
+    argument :deprecated_date, String, description: "date as a string", required: false, deprecation_reason: "Use date"
+
+    def prepare
+      return nil unless date || deprecated_date
+
+      Date.parse(date || deprecated_date)
+    end
+  end
+
   class DeepNonNull < BaseObject
     field :non_null_int, Integer, null: false do
       argument :returning, Integer, required: false
@@ -490,6 +502,7 @@ module Dummy
     field :push_value, [Integer], null: false, description: "Push a value onto a global array :D" do
       argument :value, Integer, as: :val
       argument :deprecated_test_input, DairyProductInput, required: false
+      argument :prepared_test_input, PreparedDateInput, required: false
     end
     def push_value(val:)
       GLOBAL_VALUES << val

Expected behavior

GraphQL::Analysis::AST::FieldUsage can detect deprecated arguments within input objects in a mutation when there is a prepared field.

Actual behavior

Error is returned when an InputType argument is prepared

Click to view exception backtrace
Error:
GraphQL::Analysis::AST::FieldUsage::mutation with deprecated arguments with prepared values#test_0001_keeps track of nested deprecated arguments:
NoMethodError: undefined method `arguments' for #<Date: 2020-10-10 ((2459133j,0s,0n),+0s,2299161j)>
    lib/graphql/analysis/ast/field_usage.rb:52:in `block in extract_deprecated_arguments'
    lib/graphql/analysis/ast/field_usage.rb:39:in `each_pair'
    lib/graphql/analysis/ast/field_usage.rb:39:in `extract_deprecated_arguments'
    lib/graphql/analysis/ast/field_usage.rb:23:in `on_leave_field'
    lib/graphql/analysis/ast/visitor.rb:85:in `block in call_on_leave_field'

Additional context

I was looking on how to fix it, however I was not able to find a way to get the unprepared arguments values from GraphQL::Execution::Interpreter::ArgumentValue. Is there a way to do so?

pry(#<GraphQL::Analysis::AST::FieldUsage>)> argument
=> #<GraphQL::Execution::Interpreter::ArgumentValue:0x000000010651f750 @default_used=false, @definition=#<GraphQL::Schema::Argument Mutation.pushValue.preparedTestInput: PreparedDateInput>, @value=#<Date: 2020-10-10 ((2459133j,0s,0n),+0s,2299161j)>>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions