graph_query 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +33 -0
- data/LICENSE.txt +21 -0
- data/README.md +96 -0
- data/docs/01-getting-started.md +82 -0
- data/docs/02-declaring-edges.md +134 -0
- data/docs/03-walking.md +121 -0
- data/docs/04-results.md +106 -0
- data/docs/05-patterns.md +80 -0
- data/docs/06-cypher.md +94 -0
- data/docs/07-writes.md +81 -0
- data/docs/08-graphql.md +118 -0
- data/docs/09-internals.md +108 -0
- data/docs/10-reference.md +114 -0
- data/lib/graph_query/age.rb +31 -0
- data/lib/graph_query/associations.rb +73 -0
- data/lib/graph_query/compiler.rb +224 -0
- data/lib/graph_query/cypher.rb +378 -0
- data/lib/graph_query/edge.rb +117 -0
- data/lib/graph_query/graphql.rb +329 -0
- data/lib/graph_query/match.rb +119 -0
- data/lib/graph_query/model.rb +97 -0
- data/lib/graph_query/mutation.rb +82 -0
- data/lib/graph_query/pattern.rb +104 -0
- data/lib/graph_query/query.rb +168 -0
- data/lib/graph_query/registry.rb +52 -0
- data/lib/graph_query/syntax.rb +85 -0
- data/lib/graph_query/transaction.rb +140 -0
- data/lib/graph_query/version.rb +5 -0
- data/lib/graph_query.rb +123 -0
- data/rbi/graph_query.rbi +356 -0
- data/sig/graph_query.rbs +231 -0
- metadata +106 -0
data/lib/graph_query.rb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_record'
|
|
4
|
+
require 'active_support/concern'
|
|
5
|
+
require 'pg'
|
|
6
|
+
require 'graph_query/version'
|
|
7
|
+
|
|
8
|
+
# Graph traversals for Active Record on PostgreSQL. Models declare edges (from associations, join models or SQL);
|
|
9
|
+
# queries walk them with recursive CTEs and come back as relations. See README.md.
|
|
10
|
+
#
|
|
11
|
+
# class Locale < ApplicationRecord
|
|
12
|
+
# graph_edge :contains, association: :children
|
|
13
|
+
# graph_edge :has_venue, association: :venues
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Locale.where(slug: 'washington').traverse(:contains, depth: 0..).traverse(:has_venue) # every venue in the state
|
|
17
|
+
# GraphQuery.match('(s:Locale)-[:contains*0..]->()-[:has_venue]->(v:Venue)', s: Locale.where(slug: 'washington'))
|
|
18
|
+
module GraphQuery
|
|
19
|
+
class Error < StandardError; end
|
|
20
|
+
# The step names edges that don't leave the nodes it starts on.
|
|
21
|
+
class NoEdges < Error; end
|
|
22
|
+
# The walk reaches several models and the caller asked for one relation.
|
|
23
|
+
class AmbiguousResult < Error; end
|
|
24
|
+
# A pattern or Cypher query that can't be read.
|
|
25
|
+
class PatternError < Error; end
|
|
26
|
+
# Cypher that reads fine but asks for something graph_query doesn't do (comparing two variables, say).
|
|
27
|
+
class UnsupportedCypher < Error; end
|
|
28
|
+
|
|
29
|
+
autoload :Age, 'graph_query/age'
|
|
30
|
+
autoload :Associations, 'graph_query/associations'
|
|
31
|
+
autoload :Compiler, 'graph_query/compiler'
|
|
32
|
+
autoload :Cypher, 'graph_query/cypher'
|
|
33
|
+
autoload :Edge, 'graph_query/edge'
|
|
34
|
+
autoload :Match, 'graph_query/match'
|
|
35
|
+
autoload :Model, 'graph_query/model'
|
|
36
|
+
autoload :Mutation, 'graph_query/mutation'
|
|
37
|
+
autoload :Pattern, 'graph_query/pattern'
|
|
38
|
+
autoload :Query, 'graph_query/query'
|
|
39
|
+
autoload :Registry, 'graph_query/registry'
|
|
40
|
+
autoload :Syntax, 'graph_query/syntax'
|
|
41
|
+
autoload :Transaction, 'graph_query/transaction'
|
|
42
|
+
|
|
43
|
+
# Where to start a walk: SQL selecting (node_type, node_id), and the models it can hold.
|
|
44
|
+
StartSet = Data.define(:sql, :types)
|
|
45
|
+
|
|
46
|
+
class << self
|
|
47
|
+
# Unbounded steps that need a hop count (depth: 2.., with_depth, paths) stop after this many hops.
|
|
48
|
+
attr_writer :max_depth
|
|
49
|
+
|
|
50
|
+
def max_depth = @max_depth || 32
|
|
51
|
+
|
|
52
|
+
def registry = @registry ||= Registry.new
|
|
53
|
+
|
|
54
|
+
# A Query starting from a relation (or a record, or a model's every row).
|
|
55
|
+
def from(scope) = Query.new(start_set(relation_for(scope)))
|
|
56
|
+
|
|
57
|
+
# A Query starting from nodes of several models at once: [[type, id], ...].
|
|
58
|
+
def from_pairs(pairs)
|
|
59
|
+
raise ArgumentError, 'from_pairs needs at least one node' if pairs.empty?
|
|
60
|
+
|
|
61
|
+
sets = pairs.group_by { type_name(it.first) }.map do |type, rows|
|
|
62
|
+
model = type.constantize
|
|
63
|
+
start_set(model.where(model.primary_key => rows.map(&:last)))
|
|
64
|
+
end
|
|
65
|
+
Query.new(StartSet.new(sql: sets.map { "(#{it.sql})" }.join(' UNION ALL '), types: sets.flat_map(&:types)))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# A Query starting from the nodes another query reaches.
|
|
69
|
+
def from_query(query)
|
|
70
|
+
Query.new(StartSet.new(sql: "SELECT node_type, node_id FROM (#{query.to_sql}) q",
|
|
71
|
+
types: query.types))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Runs a Cypher-style linear pattern; see GraphQuery::Pattern for the syntax. Bindings name relations (or
|
|
75
|
+
# Hashes of edge conditions) for the pattern's variables; params: gives values for $names in property maps. The
|
|
76
|
+
# first node must be bound or labelled.
|
|
77
|
+
def match(pattern, params: {}, **bindings) = Match.new(Pattern.parse(pattern, params), bindings)
|
|
78
|
+
|
|
79
|
+
# Runs the block in a database transaction (a savepoint inside an open one) with a Transaction for writing
|
|
80
|
+
# edges; any error rolls every write back. Returns the Transaction, whose changes list what was written.
|
|
81
|
+
def transaction
|
|
82
|
+
graph = Transaction.new
|
|
83
|
+
ActiveRecord::Base.transaction(requires_new: true) { yield graph }
|
|
84
|
+
graph
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Runs a Cypher query (MATCH ... WHERE ... RETURN ...); see GraphQuery::Cypher. Params give values for $names.
|
|
88
|
+
def cypher(query, **params) = Cypher.new(query, params).result
|
|
89
|
+
|
|
90
|
+
def start_set(relation)
|
|
91
|
+
model = relation.klass
|
|
92
|
+
ids = relation.unscope(:select).select(relation.arel_table[model.primary_key]).to_sql
|
|
93
|
+
conn = model.connection
|
|
94
|
+
type = conn.quote(model.polymorphic_name)
|
|
95
|
+
id = conn.quote_column_name(model.primary_key)
|
|
96
|
+
StartSet.new(sql: "SELECT #{type}::text AS node_type, start_rows.#{id} AS node_id FROM (#{ids}) start_rows",
|
|
97
|
+
types: [model.polymorphic_name])
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def relation_for(scope)
|
|
101
|
+
case scope
|
|
102
|
+
when ActiveRecord::Relation then scope
|
|
103
|
+
when ActiveRecord::Base then scope.class.where(scope.class.primary_key => scope.id)
|
|
104
|
+
when Class then scope.all
|
|
105
|
+
else raise ArgumentError, "can't walk from #{scope.inspect}"
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The node type name of a model, relation, record or name.
|
|
110
|
+
def type_name(thing)
|
|
111
|
+
case thing
|
|
112
|
+
when ActiveRecord::Relation then thing.klass.polymorphic_name
|
|
113
|
+
when ActiveRecord::Base then thing.class.polymorphic_name
|
|
114
|
+
when Class then thing.polymorphic_name
|
|
115
|
+
else thing.to_s.camelize
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def model_for(thing) = thing.is_a?(Class) ? thing : type_name(thing).constantize
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
ActiveSupport.on_load(:active_record) { include GraphQuery::Model }
|
data/rbi/graph_query.rbi
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
# typed: strong
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Sorbet signatures for graph_query's public API. Tapioca picks up a gem's rbi/ folder on `tapioca gem`.
|
|
5
|
+
module GraphQuery
|
|
6
|
+
VERSION = T.let(T.unsafe(nil), String)
|
|
7
|
+
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
class NoEdges < Error; end
|
|
10
|
+
class AmbiguousResult < Error; end
|
|
11
|
+
class PatternError < Error; end
|
|
12
|
+
class UnsupportedCypher < Error; end
|
|
13
|
+
|
|
14
|
+
Scope = T.type_alias { T.any(ActiveRecord::Relation, ActiveRecord::Base, T.class_of(ActiveRecord::Base)) }
|
|
15
|
+
ModelRef = T.type_alias { T.any(T.class_of(ActiveRecord::Base), String, Symbol) }
|
|
16
|
+
EdgeCondition = T.type_alias { T.any(T::Hash[T.untyped, T.untyped], T.proc.returns(T.untyped)) }
|
|
17
|
+
Names = T.type_alias { T.nilable(T.any(Symbol, T::Array[Symbol])) }
|
|
18
|
+
NodePair = T.type_alias { [String, T.untyped] }
|
|
19
|
+
ComputedEdge = T.type_alias { T.proc.returns(T.any(ActiveRecord::Relation, String)) }
|
|
20
|
+
|
|
21
|
+
class StartSet
|
|
22
|
+
sig { returns(String) }
|
|
23
|
+
def sql; end
|
|
24
|
+
|
|
25
|
+
sig { returns(T::Array[String]) }
|
|
26
|
+
def types; end
|
|
27
|
+
|
|
28
|
+
sig { params(sql: String, types: T::Array[String]).void }
|
|
29
|
+
def initialize(sql:, types:); end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
sig { returns(Integer) }
|
|
34
|
+
def max_depth; end
|
|
35
|
+
|
|
36
|
+
sig { params(value: T.nilable(Integer)).returns(T.nilable(Integer)) }
|
|
37
|
+
def max_depth=(value); end
|
|
38
|
+
|
|
39
|
+
sig { returns(Registry) }
|
|
40
|
+
def registry; end
|
|
41
|
+
|
|
42
|
+
sig { params(scope: Scope).returns(Query) }
|
|
43
|
+
def from(scope); end
|
|
44
|
+
|
|
45
|
+
sig { params(pairs: T::Array[[ModelRef, T.untyped]]).returns(Query) }
|
|
46
|
+
def from_pairs(pairs); end
|
|
47
|
+
|
|
48
|
+
sig { params(query: Query).returns(Query) }
|
|
49
|
+
def from_query(query); end
|
|
50
|
+
|
|
51
|
+
sig { params(pattern: String, params: T::Hash[Symbol, T.untyped], bindings: T.untyped).returns(Match) }
|
|
52
|
+
def match(pattern, params: {}, **bindings); end
|
|
53
|
+
|
|
54
|
+
sig { params(query: String, params: T.untyped).returns(T.any(ActiveRecord::Relation, T::Array[T.untyped], Integer)) }
|
|
55
|
+
def cypher(query, **params); end
|
|
56
|
+
|
|
57
|
+
sig { params(block: T.proc.params(graph: Transaction).void).returns(Transaction) }
|
|
58
|
+
def transaction(&block); end
|
|
59
|
+
|
|
60
|
+
sig { params(relation: ActiveRecord::Relation).returns(StartSet) }
|
|
61
|
+
def start_set(relation); end
|
|
62
|
+
|
|
63
|
+
sig { params(scope: Scope).returns(ActiveRecord::Relation) }
|
|
64
|
+
def relation_for(scope); end
|
|
65
|
+
|
|
66
|
+
sig { params(thing: T.any(Scope, String, Symbol)).returns(String) }
|
|
67
|
+
def type_name(thing); end
|
|
68
|
+
|
|
69
|
+
sig { params(thing: ModelRef).returns(T.class_of(ActiveRecord::Base)) }
|
|
70
|
+
def model_for(thing); end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Added to every Active Record model.
|
|
74
|
+
module Model
|
|
75
|
+
extend ActiveSupport::Concern
|
|
76
|
+
|
|
77
|
+
mixes_in_class_methods(ClassMethods)
|
|
78
|
+
|
|
79
|
+
module ClassMethods
|
|
80
|
+
sig do
|
|
81
|
+
params(
|
|
82
|
+
name: Symbol,
|
|
83
|
+
association: T.nilable(Symbol),
|
|
84
|
+
to: T.nilable(ModelRef),
|
|
85
|
+
through: T.nilable(ModelRef),
|
|
86
|
+
source: T.nilable(T.any(String, Symbol)),
|
|
87
|
+
target: T.nilable(T.any(String, Symbol)),
|
|
88
|
+
sql: T.nilable(String),
|
|
89
|
+
where: T.nilable(EdgeCondition),
|
|
90
|
+
computed: T.nilable(ComputedEdge),
|
|
91
|
+
block: T.nilable(T.proc.returns(T.any(ActiveRecord::Relation, String)))
|
|
92
|
+
).void
|
|
93
|
+
end
|
|
94
|
+
def graph_edge(name, association: nil, to: nil, through: nil, source: nil, target: nil, sql: nil, where: nil,
|
|
95
|
+
computed: nil, &block); end
|
|
96
|
+
|
|
97
|
+
sig do
|
|
98
|
+
params(
|
|
99
|
+
name: T.nilable(Symbol),
|
|
100
|
+
arguments: T::Hash[Symbol, T.untyped],
|
|
101
|
+
returns: T.untyped,
|
|
102
|
+
description: T.nilable(String)
|
|
103
|
+
).returns(T.nilable(Symbol))
|
|
104
|
+
end
|
|
105
|
+
def graph_mutation(name = nil, arguments: {}, returns: nil, description: nil); end
|
|
106
|
+
|
|
107
|
+
sig { returns(T::Hash[Symbol, Mutation]) }
|
|
108
|
+
def graph_mutations; end
|
|
109
|
+
|
|
110
|
+
sig { params(names: Names, options: T.untyped).returns(Query) }
|
|
111
|
+
def traverse(names = nil, **options); end
|
|
112
|
+
|
|
113
|
+
sig { returns(Query) }
|
|
114
|
+
def graph; end
|
|
115
|
+
|
|
116
|
+
sig { params(pattern: String, params: T::Hash[Symbol, T.untyped], bindings: T.untyped).returns(Match) }
|
|
117
|
+
def graph_match(pattern, params: {}, **bindings); end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
sig { params(names: Names, options: T.untyped).returns(Query) }
|
|
121
|
+
def traverse(names = nil, **options); end
|
|
122
|
+
|
|
123
|
+
sig { returns(Query) }
|
|
124
|
+
def graph; end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
class Edge
|
|
128
|
+
sig { returns(Symbol) }
|
|
129
|
+
def name; end
|
|
130
|
+
|
|
131
|
+
sig { returns(String) }
|
|
132
|
+
def from; end
|
|
133
|
+
|
|
134
|
+
sig { returns(String) }
|
|
135
|
+
def to; end
|
|
136
|
+
|
|
137
|
+
sig { returns(String) }
|
|
138
|
+
def declared_by; end
|
|
139
|
+
|
|
140
|
+
sig { returns(T.nilable(String)) }
|
|
141
|
+
def table_model; end
|
|
142
|
+
|
|
143
|
+
sig { returns(T.nilable(String)) }
|
|
144
|
+
def sql; end
|
|
145
|
+
|
|
146
|
+
sig { returns(T.nilable(T.class_of(ActiveRecord::Base))) }
|
|
147
|
+
def model; end
|
|
148
|
+
|
|
149
|
+
sig { params(edge_where: T.nilable(EdgeCondition)).returns(T::Boolean) }
|
|
150
|
+
def filterable_by?(edge_where); end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
class Registry
|
|
154
|
+
sig { returns(T::Array[Edge]) }
|
|
155
|
+
def all; end
|
|
156
|
+
|
|
157
|
+
sig { params(declared_by: String).void }
|
|
158
|
+
def clear(declared_by); end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
class Query
|
|
162
|
+
include Enumerable
|
|
163
|
+
extend T::Generic
|
|
164
|
+
|
|
165
|
+
Elem = type_member { { fixed: ActiveRecord::Base } }
|
|
166
|
+
|
|
167
|
+
sig { returns(StartSet) }
|
|
168
|
+
def start; end
|
|
169
|
+
|
|
170
|
+
sig do
|
|
171
|
+
params(
|
|
172
|
+
names: Names,
|
|
173
|
+
depth: T.any(Integer, T::Range[T.nilable(Integer)]),
|
|
174
|
+
direction: Symbol,
|
|
175
|
+
to: T.nilable(T.any(ModelRef, T::Array[ModelRef])),
|
|
176
|
+
where: T.untyped,
|
|
177
|
+
through: T.untyped,
|
|
178
|
+
edge_where: T.nilable(EdgeCondition)
|
|
179
|
+
).returns(Query)
|
|
180
|
+
end
|
|
181
|
+
def traverse(names = nil, depth: 1, direction: :out, to: nil, where: nil, through: nil, edge_where: nil); end
|
|
182
|
+
|
|
183
|
+
sig { params(names: Names, options: T.untyped).returns(Query) }
|
|
184
|
+
def out(names = nil, **options); end
|
|
185
|
+
|
|
186
|
+
sig { params(names: Names, options: T.untyped).returns(Query) }
|
|
187
|
+
def inbound(names = nil, **options); end
|
|
188
|
+
|
|
189
|
+
sig { params(names: Names, options: T.untyped).returns(Query) }
|
|
190
|
+
def both(names = nil, **options); end
|
|
191
|
+
|
|
192
|
+
sig { returns(T::Array[String]) }
|
|
193
|
+
def types; end
|
|
194
|
+
|
|
195
|
+
sig { params(origin: T::Boolean, depth: T::Boolean, path: T::Boolean).returns(String) }
|
|
196
|
+
def to_sql(origin: false, depth: false, path: false); end
|
|
197
|
+
|
|
198
|
+
sig { params(model: T.nilable(ModelRef)).returns(ActiveRecord::Relation) }
|
|
199
|
+
def to_relation(model = nil); end
|
|
200
|
+
|
|
201
|
+
sig { params(model: T.nilable(ModelRef)).returns(ActiveRecord::Relation) }
|
|
202
|
+
def nodes(model = nil); end
|
|
203
|
+
|
|
204
|
+
sig { params(model: T.nilable(ModelRef)).returns(ActiveRecord::Relation) }
|
|
205
|
+
def with_depth(model = nil); end
|
|
206
|
+
|
|
207
|
+
sig { returns(T::Array[NodePair]) }
|
|
208
|
+
def pairs; end
|
|
209
|
+
|
|
210
|
+
sig { returns(T::Array[ActiveRecord::Base]) }
|
|
211
|
+
def records; end
|
|
212
|
+
|
|
213
|
+
sig { params(model: T.nilable(ModelRef)).returns(T::Hash[T.untyped, T::Array[ActiveRecord::Base]]) }
|
|
214
|
+
def by_origin(model = nil); end
|
|
215
|
+
|
|
216
|
+
sig { params(model: T.nilable(ModelRef)).returns(T::Array[[T.untyped, T.untyped]]) }
|
|
217
|
+
def origin_pairs(model = nil); end
|
|
218
|
+
|
|
219
|
+
sig { returns(T::Array[T::Array[NodePair]]) }
|
|
220
|
+
def paths; end
|
|
221
|
+
|
|
222
|
+
sig { params(scope: ActiveRecord::Relation, name: T.any(Symbol, String)).returns(ActiveRecord::Relation) }
|
|
223
|
+
def polymorphic(scope, name); end
|
|
224
|
+
|
|
225
|
+
sig { override.params(block: T.proc.params(record: ActiveRecord::Base).void).void }
|
|
226
|
+
def each(&block); end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
class Pattern
|
|
230
|
+
sig { params(source: String, params: T::Hash[Symbol, T.untyped]).returns(Pattern) }
|
|
231
|
+
def self.parse(source, params = {}); end
|
|
232
|
+
|
|
233
|
+
sig { returns(T::Hash[Symbol, [Symbol, Integer]]) }
|
|
234
|
+
def variables; end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
class Match
|
|
238
|
+
sig { returns(Pattern) }
|
|
239
|
+
def pattern; end
|
|
240
|
+
|
|
241
|
+
sig { params(upto: Integer).returns(Query) }
|
|
242
|
+
def query(upto = T.unsafe(nil)); end
|
|
243
|
+
|
|
244
|
+
sig { params(var: T.nilable(Symbol), model: T.nilable(ModelRef)).returns(ActiveRecord::Relation) }
|
|
245
|
+
def nodes(var = nil, model: nil); end
|
|
246
|
+
|
|
247
|
+
sig { returns(T::Array[T::Array[NodePair]]) }
|
|
248
|
+
def paths; end
|
|
249
|
+
|
|
250
|
+
sig { params(model: T.nilable(ModelRef)).returns(ActiveRecord::Relation) }
|
|
251
|
+
def to_relation(model = nil); end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
class Cypher
|
|
255
|
+
sig { params(source: String, params: T::Hash[T.any(Symbol, String), T.untyped]).void }
|
|
256
|
+
def initialize(source, params = {}); end
|
|
257
|
+
|
|
258
|
+
sig { returns(T.any(ActiveRecord::Relation, T::Array[T.untyped], Integer)) }
|
|
259
|
+
def result; end
|
|
260
|
+
|
|
261
|
+
sig { params(var: Symbol).returns(ActiveRecord::Relation) }
|
|
262
|
+
def nodes(var); end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
class Transaction
|
|
266
|
+
class Change < Data
|
|
267
|
+
sig { returns(Symbol) }
|
|
268
|
+
def action; end
|
|
269
|
+
|
|
270
|
+
sig { returns(T.nilable(Edge)) }
|
|
271
|
+
def edge; end
|
|
272
|
+
|
|
273
|
+
sig { returns(T.nilable(ActiveRecord::Base)) }
|
|
274
|
+
def from; end
|
|
275
|
+
|
|
276
|
+
sig { returns(T.nilable(ActiveRecord::Base)) }
|
|
277
|
+
def to; end
|
|
278
|
+
|
|
279
|
+
sig { returns(T.nilable(ActiveRecord::Base)) }
|
|
280
|
+
def record; end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
sig { returns(T::Array[Change]) }
|
|
284
|
+
def changes; end
|
|
285
|
+
|
|
286
|
+
sig do
|
|
287
|
+
params(from: ActiveRecord::Base, name: Symbol, to: ActiveRecord::Base, via: T.nilable(ModelRef),
|
|
288
|
+
attributes: T::Hash[Symbol, T.untyped]).returns(T.nilable(ActiveRecord::Base))
|
|
289
|
+
end
|
|
290
|
+
def link(from, name, to, via: nil, attributes: {}); end
|
|
291
|
+
|
|
292
|
+
sig do
|
|
293
|
+
params(from: ActiveRecord::Base, name: Symbol, to: ActiveRecord::Base, via: T.nilable(ModelRef))
|
|
294
|
+
.returns(T.nilable(ActiveRecord::Base))
|
|
295
|
+
end
|
|
296
|
+
def unlink(from, name, to, via: nil); end
|
|
297
|
+
|
|
298
|
+
sig do
|
|
299
|
+
params(new_from: ActiveRecord::Base, name: Symbol, to: ActiveRecord::Base, from: T.nilable(ActiveRecord::Base),
|
|
300
|
+
via: T.nilable(ModelRef), attributes: T::Hash[Symbol, T.untyped]).returns(T.nilable(ActiveRecord::Base))
|
|
301
|
+
end
|
|
302
|
+
def relink(new_from, name, to, from:, via: nil, attributes: {}); end
|
|
303
|
+
|
|
304
|
+
sig { params(from: ActiveRecord::Base, name: Symbol, to: ActiveRecord::Base, via: T.nilable(ModelRef)).returns(T::Boolean) }
|
|
305
|
+
def linked?(from, name, to, via: nil); end
|
|
306
|
+
|
|
307
|
+
sig { params(model: T.class_of(ActiveRecord::Base), attributes: T.untyped).returns(ActiveRecord::Base) }
|
|
308
|
+
def create(model, **attributes); end
|
|
309
|
+
|
|
310
|
+
sig { params(record: ActiveRecord::Base, attributes: T.untyped).returns(ActiveRecord::Base) }
|
|
311
|
+
def update(record, **attributes); end
|
|
312
|
+
|
|
313
|
+
sig { params(record: ActiveRecord::Base).returns(ActiveRecord::Base) }
|
|
314
|
+
def destroy(record); end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
class Mutation
|
|
318
|
+
sig { returns(T.class_of(ActiveRecord::Base)) }
|
|
319
|
+
def model; end
|
|
320
|
+
|
|
321
|
+
sig { returns(Symbol) }
|
|
322
|
+
def name; end
|
|
323
|
+
|
|
324
|
+
sig { returns(T::Hash[Symbol, T.untyped]) }
|
|
325
|
+
def arguments; end
|
|
326
|
+
|
|
327
|
+
sig { returns(T.untyped) }
|
|
328
|
+
def returns; end
|
|
329
|
+
|
|
330
|
+
sig { returns(T.nilable(String)) }
|
|
331
|
+
def description; end
|
|
332
|
+
|
|
333
|
+
sig { returns(T::Array[Symbol]) }
|
|
334
|
+
def required; end
|
|
335
|
+
|
|
336
|
+
sig { returns(T::Array[Symbol]) }
|
|
337
|
+
def keywords; end
|
|
338
|
+
|
|
339
|
+
sig { returns(T::Boolean) }
|
|
340
|
+
def takes_graph?; end
|
|
341
|
+
|
|
342
|
+
sig { params(record: ActiveRecord::Base, args: T.untyped).returns(T.untyped) }
|
|
343
|
+
def call(record, **args); end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
module Age
|
|
347
|
+
sig { params(connection: T.untyped).returns(T::Boolean) }
|
|
348
|
+
def self.available?(connection = T.unsafe(nil)); end
|
|
349
|
+
|
|
350
|
+
sig do
|
|
351
|
+
params(graph: String, query: String, columns: T::Array[T.any(String, Symbol)], connection: T.untyped)
|
|
352
|
+
.returns(T.untyped)
|
|
353
|
+
end
|
|
354
|
+
def self.cypher(graph, query, columns: T.unsafe(nil), connection: T.unsafe(nil)); end
|
|
355
|
+
end
|
|
356
|
+
end
|
data/sig/graph_query.rbs
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Signatures for graph_query's public API. Active Record types come from rbs_rails / gem_rbs_collection.
|
|
2
|
+
module GraphQuery
|
|
3
|
+
VERSION: String
|
|
4
|
+
|
|
5
|
+
class Error < StandardError
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class NoEdges < Error
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class AmbiguousResult < Error
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class PatternError < Error
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class UnsupportedCypher < Error
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
type scope = ActiveRecord::Relation | ActiveRecord::Base | singleton(ActiveRecord::Base)
|
|
21
|
+
type model_ref = singleton(ActiveRecord::Base) | String | Symbol
|
|
22
|
+
type edge_condition = Hash[untyped, untyped] | ^() -> untyped
|
|
23
|
+
type depth = Integer | Range[Integer?]
|
|
24
|
+
type direction = :out | :in | :both
|
|
25
|
+
type names = Symbol | Array[Symbol] | nil
|
|
26
|
+
type node_pair = [String, untyped]
|
|
27
|
+
type computed_edge = ^() -> (ActiveRecord::Relation | String)
|
|
28
|
+
|
|
29
|
+
class StartSet
|
|
30
|
+
attr_reader sql: String
|
|
31
|
+
attr_reader types: Array[String]
|
|
32
|
+
|
|
33
|
+
def initialize: (sql: String, types: Array[String]) -> void
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
attr_writer self.max_depth: Integer?
|
|
37
|
+
|
|
38
|
+
def self.max_depth: () -> Integer
|
|
39
|
+
def self.registry: () -> Registry
|
|
40
|
+
def self.from: (scope) -> Query
|
|
41
|
+
def self.from_pairs: (Array[[model_ref, untyped]]) -> Query
|
|
42
|
+
def self.from_query: (Query) -> Query
|
|
43
|
+
def self.match: (String pattern, ?params: Hash[Symbol, untyped], **untyped bindings) -> Match
|
|
44
|
+
def self.cypher: (String query, **untyped params) -> (ActiveRecord::Relation | Array[untyped] | Integer)
|
|
45
|
+
def self.transaction: () { (Transaction) -> void } -> Transaction
|
|
46
|
+
def self.start_set: (ActiveRecord::Relation) -> StartSet
|
|
47
|
+
def self.relation_for: (scope) -> ActiveRecord::Relation
|
|
48
|
+
def self.type_name: (scope | String | Symbol) -> String
|
|
49
|
+
def self.model_for: (model_ref) -> singleton(ActiveRecord::Base)
|
|
50
|
+
|
|
51
|
+
# Added to every Active Record model.
|
|
52
|
+
module Model : ActiveRecord::Base
|
|
53
|
+
module ClassMethods
|
|
54
|
+
def graph_edge: (Symbol name, ?association: Symbol?, ?to: model_ref?, ?through: model_ref?,
|
|
55
|
+
?source: Symbol | String | nil, ?target: Symbol | String | nil, ?sql: String?,
|
|
56
|
+
?where: edge_condition?, ?computed: computed_edge?) ?{ () -> (ActiveRecord::Relation | String) }
|
|
57
|
+
-> void
|
|
58
|
+
def graph_mutation: (?Symbol? name, ?arguments: Hash[Symbol, untyped], ?returns: untyped,
|
|
59
|
+
?description: String?) -> Symbol?
|
|
60
|
+
def graph_mutations: () -> Hash[Symbol, Mutation]
|
|
61
|
+
def traverse: (?names, **untyped) -> Query
|
|
62
|
+
def graph: () -> Query
|
|
63
|
+
def graph_match: (String pattern, ?params: Hash[Symbol, untyped], **untyped bindings) -> Match
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def traverse: (?names, **untyped) -> Query
|
|
67
|
+
def graph: () -> Query
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class Edge
|
|
71
|
+
attr_reader name: Symbol
|
|
72
|
+
attr_reader from: String
|
|
73
|
+
attr_reader to: String
|
|
74
|
+
attr_reader table_model: String?
|
|
75
|
+
attr_reader source_column: String?
|
|
76
|
+
attr_reader target_column: String?
|
|
77
|
+
attr_reader conditions: Array[untyped]
|
|
78
|
+
attr_reader sql: String | computed_edge | nil
|
|
79
|
+
attr_reader declared_by: String
|
|
80
|
+
|
|
81
|
+
def initialize: (name: Symbol | String, from: String, to: String, declared_by: String, ?table_model: String?,
|
|
82
|
+
?source_column: String | Symbol | nil, ?target_column: String | Symbol | nil,
|
|
83
|
+
?conditions: Array[untyped], ?sql: String | computed_edge | nil) -> void
|
|
84
|
+
def key: () -> Array[untyped]
|
|
85
|
+
def rows_sql: (direction, ?edge_where: edge_condition?, ?bound: String?) -> String
|
|
86
|
+
def model: () -> singleton(ActiveRecord::Base)?
|
|
87
|
+
def filterable_by?: (edge_condition?) -> bool
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class Registry
|
|
91
|
+
def add: (?Edge? edge, ?key: Array[untyped]?, ?declared_by: String?) ?{ () -> Edge } -> void
|
|
92
|
+
def clear: (String declared_by) -> void
|
|
93
|
+
def all: () -> Array[Edge]
|
|
94
|
+
def walkable: (Array[String] types, Array[Symbol]? names, direction) -> Array[[Edge, :out | :in]]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
class Query
|
|
98
|
+
include Enumerable[ActiveRecord::Base]
|
|
99
|
+
|
|
100
|
+
class Step
|
|
101
|
+
attr_reader names: Array[Symbol]?
|
|
102
|
+
attr_reader direction: direction
|
|
103
|
+
attr_reader min: Integer
|
|
104
|
+
attr_reader max: Integer?
|
|
105
|
+
attr_reader labels: Array[String]?
|
|
106
|
+
attr_reader where: Array[untyped]?
|
|
107
|
+
attr_reader through: Array[untyped]?
|
|
108
|
+
attr_reader edge_where: edge_condition?
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
attr_reader start: StartSet
|
|
112
|
+
attr_reader steps: Array[Step]
|
|
113
|
+
|
|
114
|
+
def initialize: (StartSet start, ?Array[Step] steps) -> void
|
|
115
|
+
def traverse: (?names, ?depth: depth, ?direction: direction, ?to: model_ref | Array[model_ref] | nil,
|
|
116
|
+
?where: scope | Hash[untyped, untyped] | Array[scope] | nil,
|
|
117
|
+
?through: scope | Array[scope] | nil, ?edge_where: edge_condition?) -> Query
|
|
118
|
+
def out: (?names, **untyped) -> Query
|
|
119
|
+
def inbound: (?names, **untyped) -> Query
|
|
120
|
+
def both: (?names, **untyped) -> Query
|
|
121
|
+
def types: () -> Array[String]
|
|
122
|
+
def to_sql: (?origin: bool, ?depth: bool, ?path: bool) -> String
|
|
123
|
+
def to_relation: (?model_ref? model) -> ActiveRecord::Relation
|
|
124
|
+
alias nodes to_relation
|
|
125
|
+
def with_depth: (?model_ref? model) -> ActiveRecord::Relation
|
|
126
|
+
def pairs: () -> Array[node_pair]
|
|
127
|
+
def records: () -> Array[ActiveRecord::Base]
|
|
128
|
+
def by_origin: (?model_ref? model) -> Hash[untyped, Array[ActiveRecord::Base]]
|
|
129
|
+
def origin_pairs: (?model_ref? model) -> Array[[untyped, untyped]]
|
|
130
|
+
def paths: () -> Array[Array[node_pair]]
|
|
131
|
+
def polymorphic: (ActiveRecord::Relation scope, Symbol | String name) -> ActiveRecord::Relation
|
|
132
|
+
def each: () { (ActiveRecord::Base) -> void } -> void
|
|
133
|
+
def compiler: () -> Compiler
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
class Compiler
|
|
137
|
+
attr_reader types: Array[Array[String]]
|
|
138
|
+
|
|
139
|
+
def initialize: (Query query, ?origin: bool, ?depth: bool, ?path: bool) -> void
|
|
140
|
+
def sql: () -> String
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
class Pattern
|
|
144
|
+
class Node
|
|
145
|
+
attr_reader var: Symbol?
|
|
146
|
+
attr_reader labels: Array[String]?
|
|
147
|
+
attr_reader props: Hash[Symbol, untyped]?
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
class Rel
|
|
151
|
+
attr_reader var: Symbol?
|
|
152
|
+
attr_reader names: Array[Symbol]?
|
|
153
|
+
attr_reader direction: direction
|
|
154
|
+
attr_reader min: Integer
|
|
155
|
+
attr_reader max: Integer?
|
|
156
|
+
attr_reader props: Hash[Symbol, untyped]?
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
attr_reader nodes: Array[Node]
|
|
160
|
+
attr_reader rels: Array[Rel]
|
|
161
|
+
attr_reader source: String
|
|
162
|
+
|
|
163
|
+
def self.parse: (String source, ?Hash[Symbol, untyped] params) -> Pattern
|
|
164
|
+
def initialize: (String source, ?params: Hash[Symbol, untyped], ?scanner: StringScanner?) -> void
|
|
165
|
+
def variables: () -> Hash[Symbol, [:node | :rel, Integer]]
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
class Match
|
|
169
|
+
attr_reader pattern: Pattern
|
|
170
|
+
attr_reader bindings: Hash[Symbol, untyped]
|
|
171
|
+
|
|
172
|
+
def initialize: (Pattern pattern, Hash[Symbol | String, untyped] bindings) -> void
|
|
173
|
+
def query: (?Integer upto) -> Query
|
|
174
|
+
def nodes: (?Symbol? var, ?model: model_ref?) -> ActiveRecord::Relation
|
|
175
|
+
def paths: () -> Array[Array[node_pair]]
|
|
176
|
+
def to_relation: (?model_ref? model) -> ActiveRecord::Relation
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
class Cypher
|
|
180
|
+
attr_reader patterns: Array[Pattern]
|
|
181
|
+
attr_reader items: Array[untyped]
|
|
182
|
+
attr_reader distinct: bool
|
|
183
|
+
attr_reader orders: Array[untyped]
|
|
184
|
+
attr_reader skip: Integer?
|
|
185
|
+
attr_reader limit: Integer?
|
|
186
|
+
|
|
187
|
+
def initialize: (String source, ?Hash[Symbol | String, untyped] params) -> void
|
|
188
|
+
def result: () -> (ActiveRecord::Relation | Array[untyped] | Integer)
|
|
189
|
+
def nodes: (Symbol var) -> ActiveRecord::Relation
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
class Transaction
|
|
193
|
+
class Change
|
|
194
|
+
attr_reader action: :link | :unlink | :create | :update | :destroy
|
|
195
|
+
attr_reader edge: Edge?
|
|
196
|
+
attr_reader from: ActiveRecord::Base?
|
|
197
|
+
attr_reader to: ActiveRecord::Base?
|
|
198
|
+
attr_reader record: ActiveRecord::Base?
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
attr_reader changes: Array[Change]
|
|
202
|
+
|
|
203
|
+
def link: (ActiveRecord::Base from, Symbol name, ActiveRecord::Base to, ?via: model_ref?,
|
|
204
|
+
?attributes: Hash[Symbol, untyped]) -> ActiveRecord::Base?
|
|
205
|
+
def unlink: (ActiveRecord::Base from, Symbol name, ActiveRecord::Base to, ?via: model_ref?) -> ActiveRecord::Base?
|
|
206
|
+
def relink: (ActiveRecord::Base new_from, Symbol name, ActiveRecord::Base to, from: ActiveRecord::Base?,
|
|
207
|
+
?via: model_ref?, ?attributes: Hash[Symbol, untyped]) -> ActiveRecord::Base?
|
|
208
|
+
def linked?: (ActiveRecord::Base from, Symbol name, ActiveRecord::Base to, ?via: model_ref?) -> bool
|
|
209
|
+
def create: (singleton(ActiveRecord::Base) model, **untyped attributes) -> ActiveRecord::Base
|
|
210
|
+
def update: (ActiveRecord::Base record, **untyped attributes) -> ActiveRecord::Base
|
|
211
|
+
def destroy: (ActiveRecord::Base record) -> ActiveRecord::Base
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
class Mutation
|
|
215
|
+
attr_reader model: singleton(ActiveRecord::Base)
|
|
216
|
+
attr_reader name: Symbol
|
|
217
|
+
attr_reader arguments: Hash[Symbol, untyped]
|
|
218
|
+
attr_reader returns: untyped
|
|
219
|
+
attr_reader description: String?
|
|
220
|
+
|
|
221
|
+
def required: () -> Array[Symbol]
|
|
222
|
+
def keywords: () -> Array[Symbol]
|
|
223
|
+
def takes_graph?: () -> bool
|
|
224
|
+
def call: (ActiveRecord::Base record, **untyped args) -> untyped
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
module Age
|
|
228
|
+
def self.available?: (?untyped connection) -> bool
|
|
229
|
+
def self.cypher: (String graph, String query, ?columns: Array[String | Symbol], ?connection: untyped) -> untyped
|
|
230
|
+
end
|
|
231
|
+
end
|