Skip to content

Commit 5097d57

Browse files
committed
feat(Schema.define) update docs and tests
1 parent 9171f25 commit 5097d57

17 files changed

+53
-41
lines changed

guides/defining_your_schema.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ Some parts of schema definition take types as an input. There are two good ways
238238
Your schema can be initialized with some options:
239239

240240
```ruby
241-
MySchema = GraphQL::Schema.new(
242-
query: QueryType, # root type for read-only queries
243-
mutation: MutationType, # root type for mutations
244-
max_depth: 7, # if present, the max depth for incoming queries
245-
)
241+
MySchema = GraphQL::Schema.define do
242+
query QueryType, # root type for read-only queries
243+
mutation MutationType, # root type for mutations
244+
max_depth 7, # if present, the max depth for incoming queries
245+
end
246246
```
247247

248248
Additionally, you can define error handling and custom middleware as described below.

guides/executing_queries.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ result = MySchema.execute(query_string, validate: false)
6464

6565
`graphql` includes a serial execution strategy, but you can also create custom strategies to support advanced behavior. See `GraphQL::SerialExecution#execute` the required behavior.
6666

67-
Then, set your schema to use your custom execution strategy with `GraphQL::Schema#mutation_execution_strategy` or `GraphQL::Schema#query_execution_strategy`
67+
Then, set your schema to use your custom execution strategy with `GraphQL::Schema#{query|mutation|subscription}_execution_strategy`
6868

6969
For example:
7070

@@ -81,7 +81,9 @@ end
8181

8282
# ... define your types ...
8383

84-
MySchema = GraphQL::Schema.new(query: MyQueryType, mutation: MyMutationType)
85-
# Use your custom strategy:
86-
MySchema.query_execution_strategy = CustomQueryExecutionStrategy
84+
MySchema = GraphQL::Schema.define do
85+
query MyQueryType
86+
mutation MyMutationType
87+
# Use your custom strategy:
88+
query_execution_strategy = CustomQueryExecutionStrategy
8789
```

guides/introduction.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ A GraphQL system exposes your application to the world according to its _schema_
1010
Once you've defined your query root and mutation root, you can make a schema:
1111

1212
```ruby
13-
ApplicationSchema = GraphQL::Schema.new(query: QueryRoot, mutation: MutationRoot)
13+
ApplicationSchema = GraphQL::Schema.define do
14+
query QueryRoot
15+
mutation MutationRoot
16+
end
1417
```
1518

1619
At that time, `graphql-ruby` will validate all types and fields.
@@ -80,7 +83,9 @@ The query root has one field, `post`, which finds a `Post` by ID. The `resolve`
8083
Lastly, create the schema:
8184

8285
```ruby
83-
Schema = GraphQL::Schema.new(query: QueryRoot)
86+
Schema = GraphQL::Schema.define do
87+
query QueryRoot
88+
end
8489
```
8590

8691
This schema could handle queries like:
@@ -136,7 +141,10 @@ end
136141
After defining your schema, you can evaluate queries with `GraphQL::Query`. For example:
137142

138143
```ruby
139-
Schema = GraphQL::Schema.new(query: QueryRoot) # QueryRoot defined above
144+
Schema = GraphQL::Schema.define do
145+
query QueryRoot # QueryRoot defined above
146+
end
147+
140148
query_string = "query getPost { post(id: 1) { id, title, comments { body } } }"
141149

142150
result_hash = Schema.execute(query_string)

guides/relay.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ end
5555
Then assign it to the schema:
5656

5757
```ruby
58-
MySchema = GraphQL::Schema.new(...)
59-
# Assign your node identification helper:
60-
MySchema.node_identification = NodeIdentification
58+
MySchema = GraphQL::Schema.define do
59+
# ...
60+
# Declare your node identification helper:
61+
node_identification NodeIdentification
62+
end
6163
```
6264

6365
### UUID fields
@@ -365,10 +367,10 @@ MutationType = GraphQL::ObjectType.define do
365367
end
366368

367369
# and pass it to the schema
368-
MySchema = GraphQL::Schema.new(
369-
query: QueryType,
370-
mutation: MutationType
371-
)
370+
MySchema = GraphQL::Schema.define do
371+
query QueryType,
372+
mutation MutationType
373+
end
372374
```
373375

374376
Like `QueryType`, `MutationType` is a root of the schema.

guides/security.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ end
7070
Then, define your `max_complexity` at the schema-level:
7171

7272
```ruby
73-
MySchema = GraphQL::Schema.new(
73+
MySchema = GraphQL::Schema.define do
7474
# ...
75-
max_complexity: 100
76-
)
75+
max_complexity 100
76+
end
7777
```
7878

7979
Or, at the query-level, which overrides the schema-level setting:
@@ -102,10 +102,10 @@ You can also reject queries based on the depth of their nesting. You can define
102102

103103
```ruby
104104
# Schema-level:
105-
MySchema = GraphQL::Schema.new(
105+
MySchema = GraphQL::Schema.define do
106106
# ...
107107
max_depth: 10
108-
)
108+
end
109109

110110
# Query-level, which overrides the schema-level setting:
111111
MySchema.execute(query_string, max_depth: 10)

lib/graphql/schema/printer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class Schema
33
# Used to convert your {GraphQL::Schema} to a GraphQL schema string
44
#
55
# @example print your schema to standard output
6-
# Schema = GraphQL::Schema.new(query: QueryType)
7-
# puts GraphQL::Schema::Printer.print_schema(Schema)
6+
# MySchema = GraphQL::Schema.define(query: QueryType)
7+
# puts GraphQL::Schema::Printer.print_schema(MySchema)
88
#
99
module Printer
1010
extend self
@@ -20,7 +20,7 @@ def print_introspection_schema
2020
query_root = ObjectType.define do
2121
name "Query"
2222
end
23-
schema = Schema.new(query: query_root)
23+
schema = GraphQL::Schema.define(query: query_root)
2424
print_filtered_schema(schema, method(:is_introspection_type))
2525
end
2626

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ QueryType = GraphQL::ObjectType.define do
6262
end
6363

6464
# Then create your schema
65-
Schema = GraphQL::Schema.new(
66-
query: QueryType,
67-
max_depth: 8,
68-
)
65+
Schema = GraphQL::Schema.define do
66+
query QueryType,
67+
max_depth 8,
68+
end
6969
```
7070

7171
#### Execute queries

spec/graphql/analysis/query_complexity_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
end
256256
end
257257

258-
GraphQL::Schema.new(query: query_type, types: [double_complexity_type])
258+
GraphQL::Schema.define(query: query_type, orphan_types: [double_complexity_type])
259259
}
260260
let(:query_string) {%|
261261
{

spec/graphql/argument_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
end
1111

1212
err = assert_raises(GraphQL::Schema::InvalidTypeError) {
13-
schema = GraphQL::Schema.new(query: query_type)
13+
schema = GraphQL::Schema.define(query: query_type)
1414
schema.types
1515
}
1616

spec/graphql/field_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def acts_like_default_resolver(field, old_prop, new_prop)
8282
invalid_field.name = :symbol_name
8383

8484
dummy_query.fields["symbol_name"] = invalid_field
85-
dummy_schema = GraphQL::Schema.new(query: dummy_query)
85+
dummy_schema = GraphQL::Schema.define(query: dummy_query)
8686

8787
err = assert_raises(GraphQL::Schema::InvalidTypeError) { dummy_schema.types }
8888
assert_equal "QueryType is invalid: field :symbol_name name must return String, not Symbol (:symbol_name)", err.message

spec/graphql/query/context_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
resolve -> (target, args, ctx) { ctx.query.class.name }
1717
end
1818
}}
19-
let(:schema) { GraphQL::Schema.new(query: query_type, mutation: nil)}
19+
let(:schema) { GraphQL::Schema.define(query: query_type, mutation: nil)}
2020
let(:result) { schema.execute(query_string, context: {"some_key" => "some value"})}
2121

2222
describe "access to passed-in values" do

spec/graphql/query/executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
end
8686
end
8787

88-
GraphQL::Schema.new(query: DummyQueryType, mutation: MutationType)
88+
GraphQL::Schema.define(query: DummyQueryType, mutation: MutationType)
8989
}
9090
let(:variables) { nil }
9191
let(:query_string) { %|

spec/graphql/query/serial_execution/value_resolution_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
end
3131
end
3232
}
33-
let(:schema) { GraphQL::Schema.new(query: query_root) }
33+
let(:schema) { GraphQL::Schema.define(query: query_root) }
3434
let(:result) { schema.execute(
3535
query_string,
3636
)}

spec/graphql/schema/printer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
end
6161
end
6262

63-
GraphQL::Schema.new(query: query_root)
63+
GraphQL::Schema.define(query: query_root)
6464
}
6565

6666
describe ".print_introspection_schema" do

spec/graphql/schema/reduce_types_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def reduce_types(types)
9696
end
9797

9898
describe "when a field is only accessible through an interface" do
99-
it "is found through Schema.new(types:)" do
99+
it "is found through Schema.define(types:)" do
100100
assert_equal HoneyType, DummySchema.types["Honey"]
101101
end
102102
end

spec/graphql/schema/timeout_middleware_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
end
3636
end
3737

38-
schema = GraphQL::Schema.new(query: query_type)
38+
schema = GraphQL::Schema.define(query: query_type)
3939
schema.middleware << timeout_middleware
4040
schema
4141
}

spec/support/star_wars_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,5 @@ def upcased_parent_name
215215
field :introduceShip, field: IntroduceShipMutation.field
216216
end
217217

218-
StarWarsSchema = GraphQL::Schema.new(query: QueryType, mutation: MutationType)
218+
StarWarsSchema = GraphQL::Schema.define(query: QueryType, mutation: MutationType)
219219
StarWarsSchema.node_identification = NodeIdentification

0 commit comments

Comments
 (0)