lighthouse-graphql 0.7.0 → 0.7.2

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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -1
  3. data/docs/generators.md +1 -1
  4. data/docs/getting-started.md +45 -12
  5. data/docs/relay.md +52 -3
  6. data/lib/generators/lighthouse/install/graphql_ruby_install.rb +14 -0
  7. data/lib/generators/lighthouse/install/install_generator.rb +15 -1
  8. data/lib/generators/lighthouse/install/templates/schema.graphql.tt +30 -0
  9. data/lib/lighthouse/ast_builder.rb +4 -1
  10. data/lib/lighthouse/cache_control.rb +14 -2
  11. data/lib/lighthouse/complexity.rb +11 -1
  12. data/lib/lighthouse/directive_resolvers/aggregate_resolver.rb +17 -1
  13. data/lib/lighthouse/directive_resolvers/all_resolver.rb +13 -1
  14. data/lib/lighthouse/directive_resolvers/association_resolvers.rb +65 -9
  15. data/lib/lighthouse/directive_resolvers/auth_resolver.rb +10 -1
  16. data/lib/lighthouse/directive_resolvers/base.rb +18 -0
  17. data/lib/lighthouse/directive_resolvers/broadcast_resolver.rb +12 -1
  18. data/lib/lighthouse/directive_resolvers/cache_resolvers.rb +28 -3
  19. data/lib/lighthouse/directive_resolvers/can_resolvers.rb +62 -9
  20. data/lib/lighthouse/directive_resolvers/count_resolver.rb +16 -1
  21. data/lib/lighthouse/directive_resolvers/event_resolver.rb +13 -1
  22. data/lib/lighthouse/directive_resolvers/field_resolver.rb +6 -1
  23. data/lib/lighthouse/directive_resolvers/find_resolver.rb +13 -1
  24. data/lib/lighthouse/directive_resolvers/first_resolver.rb +13 -1
  25. data/lib/lighthouse/directive_resolvers/guard_resolver.rb +10 -1
  26. data/lib/lighthouse/directive_resolvers/method_resolver.rb +13 -1
  27. data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +74 -8
  28. data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +17 -2
  29. data/lib/lighthouse/directive_resolvers/rename_resolver.rb +14 -1
  30. data/lib/lighthouse/directive_resolvers/subscription_resolver.rb +13 -1
  31. data/lib/lighthouse/directive_resolvers/validator_resolver.rb +13 -1
  32. data/lib/lighthouse/directives/arguments/base.rb +18 -0
  33. data/lib/lighthouse/directives/arguments/filters.rb +16 -7
  34. data/lib/lighthouse/directives/definitions.rb +43 -6
  35. data/lib/lighthouse/document_ast.rb +6 -0
  36. data/lib/lighthouse/enum_directive.rb +6 -1
  37. data/lib/lighthouse/feature_directive.rb +13 -1
  38. data/lib/lighthouse/graphql/version.rb +1 -1
  39. data/lib/lighthouse/graphql_ruby_patches.rb +26 -2
  40. data/lib/lighthouse/ide_helper.rb +7 -6
  41. data/lib/lighthouse/scalar_coercion.rb +4 -1
  42. data/lib/lighthouse/tasks/lighthouse.rake +8 -0
  43. data/lib/lighthouse/type_resolution.rb +8 -2
  44. data/sig/lighthouse/graphql.rbs +1 -0
  45. metadata +1 -1
@@ -8,10 +8,38 @@
8
8
  module Lighthouse::GraphQL::Directives::Definitions
9
9
  # Directives that only mark the SDL for others to read.
10
10
  PASSIVE = {
11
- 'nest' => 'directive @nest on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION',
12
- 'spread' => 'directive @spread on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION',
13
- 'inject' => 'directive @inject(context: String!, name: String!) repeatable on FIELD_DEFINITION',
14
- 'cacheKey' => 'directive @cacheKey on FIELD_DEFINITION'
11
+ 'nest' => <<~GRAPHQL.chomp,
12
+ """
13
+ On an argument of an input object type, apply the filters of the input's fields
14
+ (@eq, @where, ...; at any depth, for those that carry @nest too) as if they were the field's own arguments.
15
+ """
16
+ directive @nest on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
17
+ GRAPHQL
18
+ 'spread' => <<~GRAPHQL.chomp,
19
+ """
20
+ On an argument of an input object type, merge the input's fields into the arguments
21
+ the field's resolver or mutation receives, as if they were the field's own.
22
+ """
23
+ directive @spread on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
24
+ GRAPHQL
25
+ 'inject' => <<~GRAPHQL.chomp,
26
+ """
27
+ Set an argument from the request's context, on the server, so clients cannot set it.
28
+ """
29
+ directive @inject(
30
+ "A path in the context, with dots: \\"current_user.id\\"."
31
+ context: String!
32
+ "The name of the argument to set, with dots for a nested one: \\"input.author_id\\"."
33
+ name: String!
34
+ ) repeatable on FIELD_DEFINITION
35
+ GRAPHQL
36
+ 'cacheKey' => <<~GRAPHQL.chomp
37
+ """
38
+ Mark the field whose value identifies its object in the keys of @cache and @clearCache.
39
+ Without one, the first field of type ID is used.
40
+ """
41
+ directive @cacheKey on FIELD_DEFINITION
42
+ GRAPHQL
15
43
  }.freeze
16
44
 
17
45
  module_function
@@ -38,8 +66,17 @@ module Lighthouse::GraphQL::Directives::Definitions
38
66
  # @hasManyThrough and @hasOneThrough: @hasMany's and @hasOne's definitions, renamed.
39
67
  def through_aliases
40
68
  resolvers = Lighthouse::GraphQL::DirectiveResolvers
41
- { 'hasManyThrough' => resolvers::HasManyDirectiveResolver, 'hasOneThrough' => resolvers::HasOneDirectiveResolver }
42
- .to_h { |name, klass| [name, klass.definition.sub("@#{klass.directive_name}(", "@#{name}(")] }
69
+ {
70
+ 'hasManyThrough' => [resolvers::HasManyDirectiveResolver, <<~TEXT.chomp],
71
+ Resolve a one-to-many association of the parent that goes through another (`has_many :through`)
72
+ as a list, batch-loaded so that listing it for many parents is not an N+1.
73
+ With `type`, the list is paginated.
74
+ TEXT
75
+ 'hasOneThrough' => [resolvers::HasOneDirectiveResolver, <<~TEXT.chomp]
76
+ Resolve a one-to-one association of the parent that goes through another (`has_one :through`),
77
+ batch-loaded so that loading it for many parents is not an N+1.
78
+ TEXT
79
+ }.to_h { |name, (klass, description)| [name, klass.definition(name, description)] }
43
80
  end
44
81
 
45
82
  # { type name => its SDL }, for the types the directives above refer to.
@@ -67,6 +67,12 @@ class Lighthouse::GraphQL::DocumentAST
67
67
  self
68
68
  end
69
69
 
70
+ # Drops the type of that name, if any.
71
+ def remove_type(name)
72
+ @types.delete(name)
73
+ self
74
+ end
75
+
70
76
  # Adds the type parsed from `sdl`, unless the document has a type of that name.
71
77
  def define_type(name, sdl = nil)
72
78
  return @types[name] if @types.key?(name)
@@ -24,7 +24,12 @@ module Lighthouse::GraphQL::EnumDirective
24
24
  ) on ENUM_VALUE
25
25
  GRAPHQL
26
26
 
27
- TYPE_DEFINITIONS = { 'Mixed' => 'scalar Mixed' }.freeze
27
+ TYPE_DEFINITIONS = {
28
+ 'Mixed' => <<~GRAPHQL.chomp
29
+ "Loose type that allows any value. Be careful when passing in large JSON objects, as it may result in poor performance."
30
+ scalar Mixed
31
+ GRAPHQL
32
+ }.freeze
28
33
 
29
34
  module_function
30
35
 
@@ -22,7 +22,19 @@ module Lighthouse::GraphQL::FeatureDirective
22
22
  # Arguments with a default are nullable, not `Type! = default` as in PHP: graphql-ruby
23
23
  # 2.5 does not fill in a directive argument's default, so a non-null one would be
24
24
  # refused wherever it is left out. Left out or null, it takes its default.
25
- DEFINITION = 'directive @feature(name: String!, when: FeatureState = ACTIVE) on FIELD_DEFINITION | OBJECT'
25
+ DEFINITION = <<~GRAPHQL.chomp
26
+ """
27
+ Include a field or an object type in the schema only while a feature flag is in a given state,
28
+ for the actor of the request (Flipper by default, see Config.feature_checker).
29
+ Otherwise, it is hidden from introspection and a query selecting it is invalid.
30
+ """
31
+ directive @feature(
32
+ "The name of the feature flag."
33
+ name: String!
34
+ "The state the feature must be in for the element to be visible."
35
+ when: FeatureState = ACTIVE
36
+ ) on FIELD_DEFINITION | OBJECT
37
+ GRAPHQL
26
38
 
27
39
  TYPE_DEFINITIONS = {
28
40
  'FeatureState' => <<~GRAPHQL.chomp
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lighthouse
4
4
  module GraphQL
5
- VERSION = '0.7.0'
5
+ VERSION = '0.7.2'
6
6
  end
7
7
  end
@@ -6,7 +6,8 @@ module Lighthouse::GraphQL
6
6
  # of nil, and building the schema then fails to coerce it. Its own
7
7
  # build_default_value already maps NullValue to nil; do the same here.
8
8
  module NullDirectiveArguments
9
- def args_to_kwargs(arg_owner, node)
9
+ # graphql 2.6.14 added a third parameter (the type resolver): pass on whatever it takes.
10
+ def args_to_kwargs(arg_owner, node, *rest)
10
11
  node.is_a?(::GraphQL::Language::Nodes::NullValue) ? nil : super
11
12
  end
12
13
  end
@@ -29,7 +30,28 @@ module Lighthouse::GraphQL
29
30
  end
30
31
 
31
32
  def build_interface_type(definition, type_resolver, base_type)
32
- Lighthouse::GraphQL::RootTypes.base_for(definition.name) || super
33
+ root = Lighthouse::GraphQL::RootTypes.base_for(definition.name)
34
+ return super unless root
35
+
36
+ type_resolver.register(root) if type_resolver.respond_to?(:register)
37
+ root
38
+ end
39
+ end
40
+
41
+ # graphql 2.6.14 makes the class of every type before any is built, on
42
+ # base_types, and builds into it, whatever base build_object_type is given:
43
+ # seed the root types' with the app's (`Types::QueryType`, `Types::NodeType`).
44
+ module RootTypeClasses
45
+ def initialize(lookup_hash, directives, queued_directives, type_classes, &block)
46
+ super
47
+ # `keys`: the values change while it goes over them.
48
+ type_classes.keys.each do |name|
49
+ root = Lighthouse::GraphQL::RootTypes.base_for(name)
50
+ current = type_classes[name]
51
+ next if root.nil? || (current.is_a?(Class) ? current < root : current.equal?(root))
52
+
53
+ type_classes[name] = root.is_a?(Class) ? Class.new(root) { graphql_name(name) } : root
54
+ end
33
55
  end
34
56
  end
35
57
 
@@ -37,6 +59,8 @@ module Lighthouse::GraphQL
37
59
  # root types are then built on base_types[:object], as before.
38
60
  if builder && %i[build_object_type build_interface_type].all? { |name| builder.method_defined?(name) || builder.private_method_defined?(name) }
39
61
  builder.prepend(RootTypeBuilder)
62
+ resolver = builder.const_get(:TypeResolver) if builder.const_defined?(:TypeResolver)
63
+ resolver.prepend(RootTypeClasses) if resolver&.method_defined?(:interface_module)
40
64
  end
41
65
 
42
66
  # The app's schema class inherits Lighthouse's (`class AppSchema <
@@ -78,9 +78,8 @@ module Lighthouse::IdeHelper
78
78
  end
79
79
 
80
80
  def placeholder_only(sdl)
81
- declared = sdl.to_s.match?(/^\s*scalar\s+_\b/) ? Set['_'] : Set.new
82
81
  render('The schema does not build: only the `_` placeholder is declared until it does.',
83
- undeclared_placeholder?(sdl, declared) ? [PLACEHOLDER_SCALAR] : [])
82
+ placeholder_declared?(sdl) ? [] : [PLACEHOLDER_SCALAR])
84
83
  end
85
84
 
86
85
  # Config.ide_helper's refresh: the files in the Rails app's root, when the
@@ -110,7 +109,7 @@ module Lighthouse::IdeHelper
110
109
  def programmatic_types_sdl(sdl, registry: Lighthouse::DirectiveRegistry.default, directives: directives_sdl(sdl, registry: registry))
111
110
  declared = type_names(sdl) | type_names(directives)
112
111
  generated_sdl = generated_types(sdl, declared, registry).sort_by(&:name).map(&:to_query_string)
113
- generated_sdl.unshift(PLACEHOLDER_SCALAR) if undeclared_placeholder?(sdl, declared)
112
+ generated_sdl.unshift(PLACEHOLDER_SCALAR) if declared.exclude?('_')
114
113
 
115
114
  render(
116
115
  'Types the manipulation phase generates (Connection/Paginator types, whereConditions inputs, ...).',
@@ -128,9 +127,11 @@ module Lighthouse::IdeHelper
128
127
 
129
128
  # The manipulation phase replaces the `_` placeholder (`where: _ @whereConditions`,
130
129
  # `orderBy: _ @orderBy`), but the IDE reads the SDL as written, so it needs
131
- # the placeholder declared.
132
- def undeclared_placeholder?(sdl, declared)
133
- sdl.to_s.match?(/:\s*\[?\s*_\b/) && declared.exclude?('_')
130
+ # the placeholder declared. It is always declared, whether or not the SDL
131
+ # uses it yet, so the first `_` an author writes is not an error; only an SDL
132
+ # that declares it itself is left alone.
133
+ def placeholder_declared?(sdl)
134
+ sdl.to_s.match?(/^\s*scalar\s+_\b/)
134
135
  end
135
136
 
136
137
  def type_names(sdl)
@@ -21,7 +21,10 @@ module Lighthouse::GraphQL::ScalarCoercion
21
21
  The class that reads and writes this scalar's values: coerce_input(value, context) and
22
22
  coerce_result(value, context), as a GraphQL::Schema::Scalar.
23
23
  """
24
- directive @scalar(class: String!) on SCALAR
24
+ directive @scalar(
25
+ "The scalar class, looked up in Config.namespaces[:types]. A fully-qualified name is used as-is."
26
+ class: String!
27
+ ) on SCALAR
25
28
  GRAPHQL
26
29
 
27
30
  module_function
@@ -7,6 +7,14 @@ namespace :lighthouse do
7
7
  Lighthouse::IdeHelper.write(output_dir: output_dir).each { |path| puts "Wrote #{path}" }
8
8
  end
9
9
 
10
+ desc 'Print the final schema (SDL) to stdout, as clients see it (SCHEMA=AppSchema)'
11
+ task print_schema: :environment do
12
+ schema = Object.const_get(ENV.fetch('SCHEMA', 'AppSchema'))
13
+ puts schema.to_definition
14
+ rescue StandardError => e
15
+ abort("#{schema || 'Schema'} can not be printed: #{e.message}")
16
+ end
17
+
10
18
  desc 'Build the schema cache in tmp/cache (for config.schema_cache; e.g. during a Docker build)'
11
19
  task cache: :environment do
12
20
  puts "Wrote #{Lighthouse::GraphQL::SchemaCache.warm!}"
@@ -26,14 +26,20 @@ module Lighthouse::GraphQL::TypeResolution
26
26
  The class that picks the object type of this union's values: `resolve_type(object, context)`
27
27
  or `call(object, context)`, returning a type name or a type class.
28
28
  """
29
- directive @union(resolveType: String!) on UNION
29
+ directive @union(
30
+ "The class to call, looked up in Config.namespaces[:types]. A fully-qualified name is used as-is."
31
+ resolveType: String!
32
+ ) on UNION
30
33
  GRAPHQL
31
34
  'interface' => <<~GRAPHQL.chomp
32
35
  """
33
36
  The class that picks the object type of this interface's values: `resolve_type(object, context)`
34
37
  or `call(object, context)`, returning a type name or a type class.
35
38
  """
36
- directive @interface(resolveType: String!) on INTERFACE
39
+ directive @interface(
40
+ "The class to call, looked up in Config.namespaces[:types]. A fully-qualified name is used as-is."
41
+ resolveType: String!
42
+ ) on INTERFACE
37
43
  GRAPHQL
38
44
  }.freeze
39
45
 
@@ -50,6 +50,7 @@ module Lighthouse
50
50
  def add: (untyped definition) -> untyped
51
51
  # Adds a type, or replaces the one of the same name.
52
52
  def put_type: (untyped definition) -> self
53
+ def remove_type: (String name) -> self
53
54
  # Adds the type parsed from the SDL (or the block's), unless one of the name exists.
54
55
  def define_type: (String name, ?String? sdl) ?{ () -> String } -> untyped
55
56
  def type?: (String name) -> bool
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lighthouse-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaz Innovation