Module: GraphQL::DSL

Defined in:
lib/graphql_dsl.rb,
lib/graphql/dsl.rb,
lib/graphql/dsl/error.rb,
lib/graphql/dsl/version.rb,
lib/graphql/dsl/constants.rb,
lib/graphql/dsl/nodes/node.rb,
lib/graphql/dsl/nodes/field.rb,
lib/graphql/dsl/formatter/field.rb,
lib/graphql/dsl/nodes/operation.rb,
lib/graphql/dsl/formatter/values.rb,
lib/graphql/dsl/formatter/arguments.rb,
lib/graphql/dsl/formatter/formatter.rb,
lib/graphql/dsl/formatter/operation.rb,
lib/graphql/dsl/formatter/directives.rb,
lib/graphql/dsl/nodes/fragment_spread.rb,
lib/graphql/dsl/nodes/inline_fragment.rb,
lib/graphql/dsl/nodes/fragment_operation.rb,
lib/graphql/dsl/formatter/fragment_spread.rb,
lib/graphql/dsl/formatter/inline_fragment.rb,
lib/graphql/dsl/nodes/executable_document.rb,
lib/graphql/dsl/nodes/containers/directive.rb,
lib/graphql/dsl/nodes/mixins/selection_set.rb,
lib/graphql/dsl/formatter/fragment_operation.rb,
lib/graphql/dsl/formatter/executable_document.rb,
lib/graphql/dsl/formatter/variable_definitions.rb,
lib/graphql/dsl/nodes/containers/variable_definition.rb

Overview

GraphQL DSL entry-point

Defined Under Namespace

Modules: SelectionSet Classes: Directive, Error, ExecutableDocument, Field, Formatter, FragmentOperation, FragmentSpread, InlineFragment, Node, Operation, VariableDefinition

Constant Summary collapse

VERSION =

Gem version

'1.0.0'
UNDEFINED =

Help to mark default parameter as undefined

Object.new

Refinement Method Summary collapse

Class Method Summary collapse

Class Method Details

.directive(name, **arguments) ⇒ Object

Create GraphQL directive

Parameters:

  • name (String, Symbol)

    directive name

  • arguments (Hash)

    arguments

Returns:

  • Directive



205
206
207
208
209
# File 'lib/graphql/dsl.rb', line 205

refine Kernel do
  def directive(name, **arguments)
    Directive.new(name, **arguments)
  end
end

.executable_document(&block) ⇒ ExecutableDocument

Create executable GraphQL document

Examples:

Create executable document with several queries

executable_document = GraphQL::DSL.executable_document {
  query(:sheep) {
    animal(type: :sheep) {
      __fragment :animal
    }
  }

  query(:she_goats) {
    animal(type: :she_goat) {
      __fragment :animal
    }
  }

  fragment(:animal, :Animal) {
    name
    age
  }
}

puts executable_document.to_gql
# query sheep
# {
#   animal(type: "sheep")
#   {
#     ...animal
#   }
# }
#
# query she_goats
# {
#   animal(type: "she_goat")
#   {
#     ...animal
#   }
# }
#
# fragment animal on Animal
# {
#   name
#   age
# }

Parameters:

  • block (Proc)

    declare DSL for operations

Returns:



54
55
56
# File 'lib/graphql/dsl.rb', line 54

def executable_document(&block)
  ExecutableDocument.new(&block)
end

.fragment(name, type, directives = [], &block) ⇒ FragmentOperation

Create GraphQL fragment operation

Examples:

Create fragment

fragment = GraphQL::DSL.fragment(:animal, :Animal) {
  id
  name
  age
}

puts fragment.to_gql
# fragment animal on Animal
# {
#   id
#   name
#   age
# }

Parameters:

  • name (String, Symbol)

    fragment name

  • type (String, Symbol)

    fragment type or interface

  • directives (Array) (defaults to: [])

    list of directives

  • block (Proc)

    declare DSL for sub-fields

Returns:



187
188
189
# File 'lib/graphql/dsl.rb', line 187

def fragment(name, type, directives = [], &block)
  FragmentOperation.new(name, type, directives, &block)
end

.mutation(name = nil, variable_definitions = {}, directives = [], &block) ⇒ Operation

Create GraphQL mutation operation

Examples:

Create mutation

mutation = GraphQL::DSL.mutation(:create_sheep) {
  createSheep(name: 'Milly', age: 5) {
    id
    name
    age
  }
}

puts mutation.to_gql
# mutation create_sheep
# {
#   createSheep(name: "Milly", age: 5)
#   {
#     id
#     name
#     age
#   }
# }

Parameters:

  • name (String, Symbol, nil) (defaults to: nil)

    mutation name

  • variable_definitions (Hash) (defaults to: {})

    variable definitions

  • directives (Array) (defaults to: [])

    list of directives

  • block (Proc)

    declare DSL for sub-fields

Returns:



122
123
124
# File 'lib/graphql/dsl.rb', line 122

def mutation(name = nil, variable_definitions = {}, directives = [], &block)
  Operation.new(:mutation, name, variable_definitions, directives, &block)
end

.query(name = nil, variable_definitions = {}, directives = [], &block) ⇒ Operation

Create GraphQL query operation

Examples:

Create query

query = GraphQL::DSL.query(:sheep) {
  animal(type: :sheep) {
    name
    age
  }
}

puts query.to_gql
# query sheep
# {
#   animal(type: "sheep")
#   {
#     name
#     age
#   }
# }

Parameters:

  • name (String, Symbol, nil) (defaults to: nil)

    query name

  • variable_definitions (Hash) (defaults to: {})

    variable definitions

  • directives (Array) (defaults to: [])

    list of directives

  • block (Proc)

    declare DSL for sub-fields

Returns:



87
88
89
# File 'lib/graphql/dsl.rb', line 87

def query(name = nil, variable_definitions = {}, directives = [], &block)
  Operation.new(:query, name, variable_definitions, directives, &block)
end

.subscription(name = nil, variable_definitions = {}, directives = [], &block) ⇒ Operation

Create GraphQL subscription operation

Examples:

Create subscription

subscription = GraphQL::DSL.subscription(:sheep_jumps) {
  animal(type: :sheep) {
    id
    name
    age
  }
}

puts subscription.to_gql
# subscription sheep_jumps
# {
#   animal(type: "sheep")
#   {
#     id
#     name
#     age
#   }
# }

Parameters:

  • name (String, Symbol, nil) (defaults to: nil)

    subscription name

  • variable_definitions (Hash) (defaults to: {})

    variable definitions

  • directives (Array) (defaults to: [])

    list of directives

  • block (Proc)

    declare DSL for sub-fields

Returns:



157
158
159
# File 'lib/graphql/dsl.rb', line 157

def subscription(name = nil, variable_definitions = {}, directives = [], &block)
  Operation.new(:subscription, name, variable_definitions, directives, &block)
end

.variable(type, default = UNDEFINED, *directives) ⇒ Object

Create GraphQL variable

Parameters:

  • type (String, Symbol)

    variable type

  • default (Object, nil) (defaults to: UNDEFINED)

    default value

  • directives (Array<Directive, Hash, Array>)

    list of directives

Returns:

  • VariableDefinition



222
223
224
225
226
# File 'lib/graphql/dsl.rb', line 222

refine Kernel do
  def variable(type, default = UNDEFINED, *directives)
    VariableDefinition.new(type, default, directives)
  end
end