@@ -1654,4 +1654,59 @@ class Link < GraphQL::Schema::Directive
16541654 GRAPHQL
16551655 assert_equal expected_schema , schema . to_definition
16561656 end
1657+
1658+ describe "JSON type" do
1659+ class JsonTypeApplication
1660+ SCHEMA_STRING = <<~EOS
1661+ scalar JsonValue
1662+
1663+ type Query {
1664+ echoJsonValue(arg: JsonValue): JsonValue
1665+ }
1666+ EOS
1667+
1668+ def initialize
1669+ @schema = GraphQL ::Schema . from_definition ( SCHEMA_STRING , default_resolve : self )
1670+ end
1671+
1672+ def execute_query ( query_string , **vars )
1673+ @schema . execute ( query_string , variables : vars )
1674+ end
1675+
1676+ def call ( parent_type , field , object , args , context )
1677+ args . fetch ( :arg )
1678+ end
1679+
1680+ def coerce_input ( type , value , ctx )
1681+ ( ctx [ :nils ] ||= [ ] ) . push ( value [ 2 ] )
1682+ ::JSON . generate ( value )
1683+ end
1684+
1685+ def coerce_result ( type , value , ctx )
1686+ ::JSON . parse ( value )
1687+ end
1688+ end
1689+
1690+ it "Sends normal ruby values to schema coercion" do
1691+ app = JsonTypeApplication . new
1692+
1693+ res_1 = app . execute_query ( <<~EOS , arg : [ 3 , "abc" , nil , 7 ] )
1694+ query WithArg($arg: JsonValue) {
1695+ echoJsonValue(arg: $arg)
1696+ }
1697+ EOS
1698+
1699+ assert_equal ( [ 3 , "abc" , nil , 7 ] , res_1 [ "data" ] [ "echoJsonValue" ] )
1700+ assert_equal [ nil , nil ] , res_1 . context [ :nils ]
1701+
1702+ res_2 = app . execute_query ( <<~EOS )
1703+ query {
1704+ echoJsonValue(arg: [3, "abc", null, 7])
1705+ }
1706+ EOS
1707+ assert_equal ( [ 3 , "abc" , nil , 7 ] , res_2 [ "data" ] [ "echoJsonValue" ] )
1708+ assert_equal [ nil , nil ] , res_2 . context [ :nils ]
1709+
1710+ end
1711+ end
16571712end
0 commit comments