|
1 | 1 | 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. |
4 | 5 | #
|
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. |
6 | 11 | #
|
| 12 | + # @example An enum of programming languages |
7 | 13 | # LanguageEnum = GraphQL::EnumType.define do
|
8 | 14 | # name "Languages"
|
9 | 15 | # description "Programming languages for Web projects"
|
10 | 16 | # value("PYTHON", "A dynamic, function-oriented language")
|
11 | 17 | # value("RUBY", "A very dynamic language aimed at programmer happiness")
|
12 | 18 | # value("JAVASCRIPT", "Accidental lingua franca of the web")
|
13 | 19 | # 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 | + # |
14 | 58 | class EnumType < GraphQL::BaseType
|
15 | 59 | accepts_definitions value: GraphQL::Define::AssignEnumValue
|
16 | 60 |
|
|
0 commit comments