Module: GraphQL::DSL::SelectionSet

Included in:
Field, FragmentOperation, InlineFragment, Operation
Defined in:
lib/graphql/dsl/nodes/mixins/selection_set.rb

Overview

This mixin help to reuse selections sets

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Declare new GraphQL field

Examples:

Declare fields (i.e. use GraphQL query)

query = GraphQL::DSL.query {
  items {
    id
    title
  }
}

puts query.to_gql
# {
#   items
#   {
#      id
#      title
#   }
# }

See Also:



99
100
101
102
103
# File 'lib/graphql/dsl/nodes/mixins/selection_set.rb', line 99

def method_missing(name, *args, &block)
  arguments = args.empty? ? {} : args[0]

  __field(name, **arguments, &block)
end

Instance Method Details

#__field(name, __alias: nil, __directives: [], **arguments, &block) ⇒ void

This method returns an undefined value.

Declare new GraphQL field

This method can help to avoid name collisions i.e. __field(:object_id)

Examples:

Declare fields use __field method (i.e. use GraphQL query)

query = GraphQL::DSL.query {
  __field(:field1, id: 1) {
    __field(:subfield1, id: 1)
    __field(:subfield2, id: 2)
  }
}

Declare fields use DSL (i.e. use GraphQL query)

query = GraphQL::DSL.query {
  field1 id: 1 {
    subfield1 id: 1
    subfield2 id: 2
  }
}

Parameters:

  • name (String, Symbol)

    field name

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

    field alias

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

    list of directives

  • arguments (Hash)

    field arguments

  • block (Proc)

    declare sub-fields



36
37
38
# File 'lib/graphql/dsl/nodes/mixins/selection_set.rb', line 36

def __field(name, __alias: nil, __directives: [], **arguments, &block) # rubocop:disable Lint/UnderscorePrefixedVariableName
  @__nodes << Field.new(name, __alias, arguments, __directives, &block)
end

#__fragment(name, __directives: []) ⇒ void

This method returns an undefined value.

Insert GraphQL fragment

Examples:

Insert fragment with fragment1 name

query = GraphQL::DSL.query {
  field1 id: 1 {
    __fragment :fragment1
  }
}

Parameters:

  • name (String, Symbol)

    fragment name

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

    list of directives



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

def __fragment(name, __directives: []) # rubocop:disable Lint/UnderscorePrefixedVariableName
  @__nodes << FragmentSpread.new(name, __directives)
end

#__inline_fragment(type, __directives: [], &block) ⇒ void

This method returns an undefined value.

Insert GraphQL inline fragment

Parameters:

  • type (String, Symbol, nil)

    fragment type

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

    list of directives

  • block (Proc)

    declare DSL for sub-fields



66
67
68
# File 'lib/graphql/dsl/nodes/mixins/selection_set.rb', line 66

def __inline_fragment(type, __directives: [], &block) # rubocop:disable Lint/UnderscorePrefixedVariableName
  @__nodes << InlineFragment.new(type, __directives, &block)
end