Skip to content

Commit 90cc12d

Browse files
committed
JSON scalar type
1 parent 91fbadb commit 90cc12d

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

guides/type_definitions/scalars.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Scalars are "leaf" values in GraphQL. There are several built-in scalars, and yo
1616
- `Boolean`, like a JSON or Ruby boolean (`true` or `false`)
1717
- `ID`, which a specialized `String` for representing unique object identifiers
1818
- `ISO8601DateTime`, an ISO 8601-encoded datetime
19+
- `JSON`, like JSON or Ruby hashes, arrays, strings, integers, floats, booleans and nils. This should be used judiciously because it subverts the GraphQL type system.
1920

2021
Fields can return built-in scalars by referencing them by name:
2122

@@ -34,6 +35,8 @@ field :is_top_ranked, Boolean, null: false
3435
field :id, ID, null: false
3536
# ISO8601DateTime field
3637
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
38+
# JSON field
39+
field :parameters, GraphQL::Types::JSON, null: false
3740
```
3841

3942
Custom scalars (see below) can also be used by name:

lib/graphql/types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
require "graphql/types/id"
55
require "graphql/types/int"
66
require "graphql/types/iso_8601_date_time"
7+
require "graphql/types/json"
78
require "graphql/types/string"
89
require "graphql/types/relay"

lib/graphql/types/json.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
module GraphQL
3+
module Types
4+
# An untyped JSON scalar that maps to Ruby hashes, arrays, strings, integers, floats, booleans and nils.
5+
# This should be used judiciously because it subverts the GraphQL type system.
6+
#
7+
# Use it for fields or arguments as follows:
8+
#
9+
# field :template_parameters, GraphQL::Types::JSON, null: false
10+
#
11+
# argument :template_parameters, GraphQL::Types::JSON, null: false
12+
#
13+
class JSON < GraphQL::Schema::Scalar
14+
description "Represents untyped JSON"
15+
16+
def self.coerce_input(value, _context)
17+
value
18+
end
19+
20+
def self.coerce_result(value, _context)
21+
value
22+
end
23+
end
24+
end
25+
end

spec/support/jazz.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,6 @@ def self.coerce_result(val, ctx)
249249
end
250250
end
251251

252-
class RawJson < GraphQL::Schema::Scalar
253-
def self.coerce_input(val, ctx)
254-
val
255-
end
256-
257-
def self.coerce_result(val, ctx)
258-
val
259-
end
260-
end
261-
262252
class Musician < BaseObject
263253
implements GloballyIdentifiableType
264254
implements NamedEntity
@@ -358,12 +348,12 @@ def now_playing; Models.data["Ensemble"].first; end
358348
field :inspect_context, [String], null: false
359349
field :hashyEnsemble, Ensemble, null: false
360350

361-
field :echo_json, RawJson, null: false do
362-
argument :input, RawJson, required: true
351+
field :echo_json, GraphQL::Types::JSON, null: false do
352+
argument :input, GraphQL::Types::JSON, required: true
363353
end
364354

365-
field :echo_first_json, RawJson, null: false do
366-
argument :input, [RawJson], required: true
355+
field :echo_first_json, GraphQL::Types::JSON, null: false do
356+
argument :input, [GraphQL::Types::JSON], required: true
367357
end
368358

369359
field :upcase_check_1, String, null: true, resolver_method: :upcase_check, extras: [:upcase]

0 commit comments

Comments
 (0)