Merge selection sets

This commit is contained in:
2020-06-12 07:58:08 +02:00
parent c37b9c88b1
commit e8c54810f8
3 changed files with 73 additions and 32 deletions

View File

@ -22,21 +22,54 @@ schema = Schema {query = queryType, mutation = Nothing}
queryType :: Out.ObjectType Identity
queryType = Out.ObjectType "Query" Nothing []
$ HashMap.singleton "count"
$ Out.Resolver countField
$ HashMap.singleton "philosopher"
$ Out.Resolver philosopherField
$ pure
$ Int 8
$ Object mempty
where
countField = Out.Field Nothing (Out.NonNullScalarType int) HashMap.empty
philosopherField =
Out.Field Nothing (Out.NonNullObjectType philosopherType) HashMap.empty
philosopherType :: Out.ObjectType Identity
philosopherType = Out.ObjectType "Philosopher" Nothing []
$ HashMap.fromList resolvers
where
resolvers =
[ ("firstName", firstNameResolver)
, ("lastName", lastNameResolver)
]
firstNameResolver = Out.Resolver firstNameField $ pure $ String "Friedrich"
lastNameResolver = Out.Resolver lastNameField $ pure $ String "Nietzsche"
firstNameField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
lastNameField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty
spec :: Spec
spec =
describe "execute" $
describe "execute" $ do
it "skips unknown fields" $
let expected = Aeson.object
["data" .= Aeson.object ["count" .= (8 :: Int)]]
[ "data" .= Aeson.object
[ "philosopher" .= Aeson.object
[ "firstName" .= ("Friedrich" :: String)
]
]
]
execute' = execute schema (mempty :: HashMap Name Aeson.Value)
actual = runIdentity
$ either parseError execute'
$ parse document "" "{ count number }"
$ parse document "" "{ philosopher { firstName surname } }"
in actual `shouldBe` expected
it "merges selections" $
let expected = Aeson.object
[ "data" .= Aeson.object
[ "philosopher" .= Aeson.object
[ "firstName" .= ("Friedrich" :: String)
, "lastName" .= ("Nietzsche" :: String)
]
]
]
execute' = execute schema (mempty :: HashMap Name Aeson.Value)
actual = runIdentity
$ either parseError execute'
$ parse document "" "{ philosopher { firstName } philosopher { lastName } }"
in actual `shouldBe` expected