Skip to content

Instantly share code, notes, and snippets.

@mlwilkerson
Created March 6, 2019 16:12
Show Gist options
  • Save mlwilkerson/590bd83b2b14993ae8a66fa56fe75494 to your computer and use it in GitHub Desktop.
Save mlwilkerson/590bd83b2b14993ae8a66fa56fe75494 to your computer and use it in GitHub Desktop.
Demonstrate field resolution of an array of objects of authorized types where one or more objects fail authorization
require 'graphql'
class Bar < GraphQL::Schema::Object
field :color, String, null: false
def self.authorized?(object, _context)
object[:color] === 'red'
end
end
class Query < GraphQL::Schema::Object
field :bars, [Bar], null: false
field :bills, [Bar], null: false
def bars
[{color: 'red'}, {color: 'blue'}]
end
def bills
[{color: 'green'}]
end
end
class MySchema < GraphQL::Schema
query(Query)
end
result = MySchema.execute("query{ bars { color } bills { color } }").to_h
expected = {"data"=>{"bars"=>[{"color"=>"red"}], "bills"=>[]}}
if result == expected
puts "ok"
else
puts "EXPECTED: #{ expected }"
puts "GOT: #{ result }"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment