Module: Graphql::Generators::Relay

Included in:
InstallGenerator, RelayGenerator
Defined in:
lib/generators/graphql/relay.rb

Instance Method Summary collapse

Instance Method Details

#install_relayObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/graphql/relay.rb', line 5

def install_relay
  # Add Node, `node(id:)`, and `nodes(ids:)`
  template("node_type.erb", "#{options[:directory]}/types/node_type.rb")
  in_root do
    fields = "    # Add `node(id: ID!) and `nodes(ids: [ID!]!)`\n    include GraphQL::Types::Relay::HasNodeField\n    include GraphQL::Types::Relay::HasNodesField\n\n"
    inject_into_file "#{options[:directory]}/types/query_type.rb", fields, after: /class .*QueryType\s*<\s*[^\s]+?\n/m, force: false
  end

  # Add connections and edges
  template("base_connection.erb", "#{options[:directory]}/types/base_connection.rb")
  template("base_edge.erb", "#{options[:directory]}/types/base_edge.rb")
  connectionable_type_files = {
    "#{options[:directory]}/types/base_object.rb" => /class .*BaseObject\s*<\s*[^\s]+?\n/m,
    "#{options[:directory]}/types/base_union.rb" =>  /class .*BaseUnion\s*<\s*[^\s]+?\n/m,
    "#{options[:directory]}/types/base_interface.rb" => /include GraphQL::Schema::Interface\n/m,
  }
  in_root do
    connectionable_type_files.each do |type_class_file, sentinel|
      inject_into_file type_class_file, "    connection_type_class(Types::BaseConnection)\n", after: sentinel, force: false
      inject_into_file type_class_file, "    edge_type_class(Types::BaseEdge)\n", after: sentinel, force: false
    end
  end

  # Add object ID hooks & connection plugin
  schema_code = "\n  # Relay-style Object Identification:\n\n  # Return a string UUID for `object`\n  def self.id_from_object(object, type_definition, query_ctx)\n    # For example, use Rails' GlobalID library (https://github.com/rails/globalid):\n    object_id = object.to_global_id.to_s\n    # Remove this redundant prefix to make IDs shorter:\n    object_id = object_id.sub(\"gid://\\\#{GlobalID.app}/\", \"\")\n    encoded_id = Base64.urlsafe_encode64(object_id)\n    # Remove the \"=\" padding\n    encoded_id = encoded_id.sub(/=+/, \"\")\n    # Add a type hint\n    type_hint = type_definition.graphql_name.first\n    \"\\\#{type_hint}_\\\#{encoded_id}\"\n  end\n\n  # Given a string UUID, find the object\n  def self.object_from_id(encoded_id_with_hint, query_ctx)\n    # For example, use Rails' GlobalID library (https://github.com/rails/globalid):\n    # Split off the type hint\n    _type_hint, encoded_id = encoded_id_with_hint.split(\"_\", 2)\n    # Decode the ID\n    id = Base64.urlsafe_decode64(encoded_id)\n    # Rebuild it for Rails then find the object:\n    full_global_id = \"gid://\\\#{GlobalID.app}/\\\#{id}\"\n    GlobalID::Locator.locate(full_global_id)\n  end\n"
  inject_into_file schema_file_path, schema_code, before: /^end\n/m, force: false
end