Skip to content

Commit c546ea1

Browse files
committed
doc(Enum, Directive) add better docs and examples
1 parent fddb5bf commit c546ea1

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

lib/graphql/argument.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module GraphQL
99
# argument :favoriteFood, types.String, "Favorite thing to eat", default_value: "pizza"
1010
# end
1111
#
12-
# @example defining an input field for an {InputObjectType}
12+
# @example defining an argument for an {InputObjectType}
1313
# GraphQL::InputObjectType.define do
14-
# input_field :newName, !types.String
14+
# argument :newName, !types.String
1515
# end
1616
#
1717
class Argument

lib/graphql/directive.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
module GraphQL
2+
# Directives are server-defined hooks for modifying execution.
3+
#
4+
# Two directives are included out-of-the-box:
5+
# - `@skip(if: ...)` Skips the tagged field if the value of `if` is true
6+
# - `@include(if: ...)` Includes the tagged field _only_ if `if` is true
7+
#
28
class Directive
39
include GraphQL::Define::InstanceDefinable
410
accepts_definitions :locations, :name, :description, :include_proc, argument: GraphQL::Define::AssignArgument

lib/graphql/enum_type.rb

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11
module GraphQL
2-
# A finite set of possible values, represented in query strings with
3-
# SCREAMING_CASE_NAMES
2+
# Represents a collection of related values.
3+
# By convention, enum names are `SCREAMING_CASE_NAMES`,
4+
# but other identifiers are supported too.
45
#
5-
# @example An enum of programming languages
6+
# You can use as return types _or_ as inputs.
7+
#
8+
# By default, enums are passed to `resolve` functions as
9+
# the strings that identify them, but you can provide a
10+
# custom Ruby value with the `value:` keyword.
611
#
12+
# @example An enum of programming languages
713
# LanguageEnum = GraphQL::EnumType.define do
814
# name "Languages"
915
# description "Programming languages for Web projects"
1016
# value("PYTHON", "A dynamic, function-oriented language")
1117
# value("RUBY", "A very dynamic language aimed at programmer happiness")
1218
# value("JAVASCRIPT", "Accidental lingua franca of the web")
1319
# end
20+
#
21+
# @example Using an enum as a return type
22+
# field :favoriteLanguage, LanguageEnum, "This person's favorite coding language"
23+
# # ...
24+
# # In a query:
25+
# Schema.execute("{ coder(id: 1) { favoriteLanguage } }")
26+
# # { "data" => { "coder" => { "favoriteLanguage" => "RUBY" } } }
27+
#
28+
# @example Defining an enum input
29+
# field :coders, types[CoderType] do
30+
# argument :knowing, types[LanguageType]
31+
# resolve -> (obj, args, ctx) {
32+
# Coder.where(language: args[:knowing])
33+
# }
34+
# end
35+
#
36+
# @example Using an enum as input
37+
# {
38+
# # find coders who know Python and Ruby
39+
# coders(knowing: [PYTHON, RUBY]) {
40+
# name
41+
# hourlyRate
42+
# }
43+
# }
44+
#
45+
# @example Enum whose values are different in Ruby-land
46+
# GraphQL::EnumType.define do
47+
# # ...
48+
# # use the `value:` keyword:
49+
# value("RUBY", "Lisp? Smalltalk?", value: :rb)
50+
# end
51+
#
52+
# # Now, resolve functions will receive `:rb` instead of `"RUBY"`
53+
# field :favoriteLanguage, LanguageEnum
54+
# resolve -> (obj, args, ctx) {
55+
# args[:favoriteLanguage] # => :rb
56+
# }
57+
#
1458
class EnumType < GraphQL::BaseType
1559
accepts_definitions value: GraphQL::Define::AssignEnumValue
1660

lib/graphql/scalar_type.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module GraphQL
77
#
88
# `GraphQL` comes with standard built-in scalars:
99
#
10-
# Constant | `.define` helper
11-
# =========|==================
12-
# `GraphQL::STRING_TYPE` | `types.String`
13-
# `GraphQL::INT_TYPE` | `types.Int`
14-
# `GraphQL::FLOAT_TYPE` | `types.Float`
15-
# `GraphQL::ID_TYPE` | `types.ID`
16-
# `GraphQL::BOOLEAN_TYPE` | `types.Boolean`
10+
# |Constant | `.define` helper|
11+
# |-------|--------|
12+
# |`GraphQL::STRING_TYPE` | `types.String`|
13+
# |`GraphQL::INT_TYPE` | `types.Int`|
14+
# |`GraphQL::FLOAT_TYPE` | `types.Float`|
15+
# |`GraphQL::ID_TYPE` | `types.ID`|
16+
# |`GraphQL::BOOLEAN_TYPE` | `types.Boolean`|
1717
#
1818
# (`types` is an instance of `GraphQL::Definition::TypeDefiner`; `.String`, `.Float`, etc are methods which return built-in scalars.)
1919
#

0 commit comments

Comments
 (0)