Last active
May 18, 2024 19:34
-
-
Save hwrdprkns/9593a15e33757dd4061439a001e57a84 to your computer and use it in GitHub Desktop.
GraphQL Nested Query Test
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
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile do | |
gem "graphql" | |
gem "byebug" | |
end | |
class Schema < GraphQL::Schema | |
class GraphQL::Schema::Validator::MyCustomValidator < GraphQL::Schema::Validator | |
def initialize(test: nil, **kwargs) | |
puts "Im not included so how did I get instantiated here? #{kwargs}" | |
super(**kwargs) | |
end | |
GraphQL::Schema::Validator.install(:custom, GraphQL::Schema::Validator::MyCustomValidator) | |
end | |
class NestedQuery < GraphQL::Schema::Object | |
field :test, String do | |
argument :input, String, required: true, validates: { custom: { test: nil } } | |
end | |
def test(input:) | |
return "Some Return!" | |
end | |
end | |
class QueryRoot < GraphQL::Schema::Object | |
field :nested_query, NestedQuery, null: false | |
def nested_query | |
{} | |
end | |
end | |
rescue_from StandardError do |exception, _obj, _arg, ctx, _field| | |
puts "Error: #{exception.message}" | |
end | |
query(QueryRoot) | |
end | |
query_string = <<~QUERY | |
query TestNestedQuery($input: String!, $shouldInclude: Boolean!) { | |
nestedQuery @include(if: $shouldInclude) { test(input: $input) } | |
} | |
QUERY | |
result = Schema.execute(query_string, variables: { shouldInclude: false, input: "" }) | |
pp result.to_h | |
pp result.to_h | |
# Im not included so how did I get instantiated here? | |
# {:validated=>#<GraphQL::Schema::Argument NestedQuery.test.input: String!>} | |
# {"data"=>{}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment