Created
March 6, 2019 16:12
-
-
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
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 '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