Skip to content

Commit 47277b4

Browse files
committed
doc(Field) improve header & examples
1 parent c546ea1 commit 47277b4

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

lib/graphql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(message, line, col, query)
1818
end
1919

2020
# Turn a query string into an AST
21-
# @param [String] a GraphQL query string
21+
# @param query_string [String] a GraphQL query string
2222
# @return [GraphQL::Language::Nodes::Document]
2323
def self.parse(query_string)
2424
parse_with_racc(query_string)

lib/graphql/field.rb

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,78 @@ module GraphQL
55
#
66
# They're usually created with the `field` helper. If you create it by hand, make sure {#name} is a String.
77
#
8-
# @example creating a field
8+
# A field must have a return type, but if you want to defer the return type calculation until later,
9+
# you can pass a proc for the return type. That proc will be called when the schema is defined.
10+
#
11+
# @example Lazy type resolution
12+
# # If the field's type isn't defined yet, you can pass a proc
13+
# field :city, -> { TypeForModelName.find("City") }
14+
#
15+
# For complex field definition, you can pass a block to the `field` helper, eg `field :name do ... end`.
16+
# This block is equivalent to calling `GraphQL::Field.define { ... }`.
17+
#
18+
# @example Defining a field with a block
19+
# field :city, CityType do
20+
# # field definition continues inside the block
21+
# end
22+
#
23+
# ## Resolve
24+
#
25+
# Fields have `resolve` functions to determine their values at query-time.
26+
# The default implementation is to call a method on the object based on the field name.
27+
#
28+
# @example Create a field which calls a method with the same name.
929
# GraphQL::ObjectType.define do
1030
# field :name, types.String, "The name of this thing "
1131
# end
1232
#
13-
# @example handling a circular reference
14-
# # If the field's type isn't defined yet, you have two options:
33+
# You can specify a custom proc with the `resolve` helper.
34+
#
35+
# There are some shortcuts for common `resolve` implementations:
36+
# - Provide `property:` to call a method with a different name than the field name
37+
# - Provide `hash_key:` to resolve the field by doing a key lookup, eg `obj[:my_hash_key]`
1538
#
39+
# @example Create a field that calls a different method on the object
1640
# GraphQL::ObjectType.define do
17-
# # If you pass a Proc, it will be evaluated at schema build-time
18-
# field :city, -> { CityType }
19-
# # If you pass a String, it will be looked up in the global namespace at schema build-time
20-
# field :country, "CountryType"
41+
# # use the `property` keyword:
42+
# field :firstName, types.String, property: :first_name
2143
# end
2244
#
23-
# @example creating a field that accesses a different property on the object
45+
# @example Create a field looks up with `[hash_key]`
2446
# GraphQL::ObjectType.define do
25-
# # use the `property` option:
26-
# field :firstName, types.String, property: :first_name
47+
# # use the `hash_key` keyword:
48+
# field :firstName, types.String, hash_key: :first_name
2749
# end
2850
#
29-
# @example defining a field, then attaching it to a type
30-
# name_field = GraphQL::Field.define do
31-
# name("Name")
32-
# type(!types.String)
33-
# description("The name of this thing")
34-
# resolve -> (object, arguments, context) { object.name }
51+
# ## Arguments
52+
#
53+
# Fields can take inputs; they're called arguments. You can define them with the `argument` helper.
54+
#
55+
# @example Create a field with an argument
56+
# field :students, types[StudentType] do
57+
# argument :grade, types.Int
58+
# resolve -> (obj, args, ctx) {
59+
# Student.where(grade: args[:grade])
60+
# }
3561
# end
3662
#
37-
# NamedType = GraphQL::ObjectType.define do
38-
# # use the `field` option:
39-
# field :name, field: name_field
63+
# They can have default values which will be provided to `resolve` if the query doesn't include a value.
64+
#
65+
# @example Argument with a default value
66+
# field :events, types[EventType] do
67+
# # by default, don't include past events
68+
# argument :includePast, types.Boolean, default_value: false
69+
# resolve -> (obj, args, ctx) {
70+
# args[:includePast] # => false if no value was provided in the query
71+
# # ...
72+
# }
4073
# end
4174
#
75+
# ## Complexity
76+
#
77+
# Fields can have _complexity_ values which describe the computation cost of resolving the field.
78+
# You can provide the complexity as a constant with `complexity:` or as a proc, with the `complexity` helper.
79+
#
4280
# @example Custom complexity values
4381
# # Complexity can be a number or a proc.
4482
#
@@ -57,12 +95,26 @@ module GraphQL
5795
# complexity -> (ctx, args, child_complexity) { child_complexity * args[:limit] }
5896
# end
5997
#
98+
# @example Creating a field, then assigning it to a type
99+
# name_field = GraphQL::Field.define do
100+
# name("Name")
101+
# type(!types.String)
102+
# description("The name of this thing")
103+
# resolve -> (object, arguments, context) { object.name }
104+
# end
105+
#
106+
# NamedType = GraphQL::ObjectType.define do
107+
# # The second argument may be a GraphQL::Field
108+
# field :name, name_field
109+
# end
110+
#
60111
class Field
61112
include GraphQL::Define::InstanceDefinable
62113
accepts_definitions :name, :description, :resolve, :type, :property, :deprecation_reason, :complexity, :hash_key, argument: GraphQL::Define::AssignArgument
63114

64115
lazy_defined_attr_accessor :deprecation_reason, :description, :property, :hash_key
65116

117+
# @return [<#call(obj, args,ctx)>] A proc-like object which can be called to return the field's value
66118
attr_reader :resolve_proc
67119

68120
# @return [String] The name of this field on its {GraphQL::ObjectType} (or {GraphQL::InterfaceType})

lib/graphql/field/resolve.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Field
44
module Resolve
55
module_function
66

7-
# @param [GraphQL::Field] A field that needs a resolve proc
7+
# @param field [GraphQL::Field] A field that needs a resolve proc
88
# @return [Proc] A resolver for this field, based on its config
99
def create_proc(field)
1010
if field.property

0 commit comments

Comments
 (0)