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
61
62
63
|
# File 'lib/generators/graphql/relay.rb', line 5
def install_relay
template("node_type.erb", "#{options[:directory]}/types/node_type.rb")
in_root do
fields = " field :node, Types::NodeType, null: true, description: \"Fetches an object given its ID.\" do\nargument :id, ID, required: true, description: \"ID of the object.\"\n end\n\n def node(id:)\ncontext.schema.object_from_id(id, context)\n end\n\n field :nodes, [Types::NodeType, null: true], null: true, description: \"Fetches a list of objects given a list of IDs.\" do\nargument :ids, [ID], required: true, description: \"IDs of the objects.\"\n end\n\n def nodes(ids:)\nids.map { |id| context.schema.object_from_id(id, context) }\n end\n\n RUBY\n inject_into_file \"\#{options[:directory]}/types/query_type.rb\", fields, after: /class .*QueryType\\s*<\\s*[^\\s]+?\\n/m, force: false\n end\n\n # Add connections and edges\n template(\"base_connection.erb\", \"\#{options[:directory]}/types/base_connection.rb\")\n template(\"base_edge.erb\", \"\#{options[:directory]}/types/base_edge.rb\")\n connectionable_type_files = {\n \"\#{options[:directory]}/types/base_object.rb\" => /class .*BaseObject\\s*<\\s*[^\\s]+?\\n/m,\n \"\#{options[:directory]}/types/base_union.rb\" => /class .*BaseUnion\\s*<\\s*[^\\s]+?\\n/m,\n \"\#{options[:directory]}/types/base_interface.rb\" => /include GraphQL::Schema::Interface\\n/m,\n }\n in_root do\n connectionable_type_files.each do |type_class_file, sentinel|\n inject_into_file type_class_file, \" connection_type_class(Types::BaseConnection)\\n\", after: sentinel, force: false\n inject_into_file type_class_file, \" edge_type_class(Types::BaseEdge)\\n\", after: sentinel, force: false\n end\n end\n\n # Add object ID hooks & connection plugin\n schema_code = <<-RUBY\n\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.to_gid_param\n end\n\n # Given a string UUID, find the object\n def self.object_from_id(global_id, query_ctx)\n # For example, use Rails' GlobalID library (https://github.com/rails/globalid):\n GlobalID.find(global_id)\n end\n"
inject_into_file schema_file_path, schema_code, before: /^end\n/m, force: false
end
|