Skip to content

Commit

Permalink
stop relying on :schema_generation_context? to avoid visibility rules
Browse files Browse the repository at this point in the history
There is a built-in alternative that doesn't leaks all over client code.
  • Loading branch information
sskirby committed May 29, 2024
1 parent 762a303 commit 2dd6afe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 73 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

_none_

## 4.0.0 (2024-05-28)

**Changes**

* Remove the `schema_generation_context?` attribute to the GraphQL `context` when generating the schema. Use the
already available `GraphQL::Schema::AlwaysVisible` plugin instead.
* **(Breaking)** Remove the `NulogyGraphqlApi::Schema::BaseMutation` class which introduced a new API for the
`visible?` method that took a block. This introduced a deviation from the ruby graphql gem's API only for
Mutations and so it was removed. Please ensure that any invocations of `visible?` do not take a block and use
`GraphQL::Schema::Mutation` instead.
* **(Breaking)** Change the `NulogyGraphqlApi::Tasks::SchemaGenerator#generate_schema` method to output the
stringified version of the schema instead of checking it for changes and writing it to a file.
* Expose the `#check_changes` and `#write_schema_to_file` methods on the
`NulogyGraphqlApi::Tasks::SchemaGenerator` to give the user more control over how to build their
tooling.

## 3.0.1 (2024-01-30)

* Add `include_graphql_error` RSpec matcher
Expand Down
44 changes: 1 addition & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ end

There is a Rake task to generate the `schema.graphql` file. You need to provide the `schema_file_path` and the schema class so that the task can detect breaking changes and generate the file. If you don't have a schema file because it's your first time generating it then the rake task will just create one for you in the path provided.

There is also a third argument `context`. You'll have to configure it to be able to generate the SDL of fields or types that are only visible for more privileged users.

```ruby
namespace :graphql_api do
desc "Generate the graphql schema of the api."
Expand All @@ -142,47 +140,7 @@ namespace :graphql_api do

NulogyGraphqlApi::Tasks::SchemaGenerator
.new(schema_file_path, schema)
.generate_schema
end
end
```

### Node visibility

When you customize the visibility of parts of your graph you have to make sure that all nodes are visible when the schema is being generated by the rake task. You can do so by using the `schema_generation_context?` attribute that is added to the context by the `SchemaGenerator` mentioned in the previous section.

Here is how to use it:

##### On fields
```ruby
field :entity, MyApp::EntityType, null: false do
description "Find an entity by ID"
argument :id, ID, required: true

def visible?(context)
context[:schema_generation_context?] || context[:current_user].superuser?
end
end
```


##### On mutations

In this case the `schema_generation_context?` attribute is checked by the `BaseMutation` class. All you have to do is inheriting from it and override `visible?` passing a block to the base method.

```ruby
module MyApp
class CreateEntity < NulogyGraphqlApi::Schema::BaseMutation
field :entity, MyApp::EntityType, null: false
field :errors, [NulogyGraphqlApi::Types::UserErrorType], null: false

def self.visible?(context)
super { context[:current_user].super_user? }
end

def resolve(args)
# ...
end
.write_schema_to_file
end
end
```
Expand Down
1 change: 0 additions & 1 deletion lib/nulogy_graphql_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "nulogy_graphql_api/error_handling"
require "nulogy_graphql_api/graphql_executor"
require "nulogy_graphql_api/graphql_response"
require "nulogy_graphql_api/schema/base_mutation"
require "nulogy_graphql_api/transaction_service"
require "nulogy_graphql_api/tasks/schema_changes_checker"
require "nulogy_graphql_api/tasks/schema_generator"
Expand Down
12 changes: 0 additions & 12 deletions lib/nulogy_graphql_api/schema/base_mutation.rb

This file was deleted.

29 changes: 12 additions & 17 deletions lib/nulogy_graphql_api/tasks/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,32 @@ class SchemaGenerator
def initialize(schema_output_path, schema, context: {})
@schema_output_path = schema_output_path
@schema = schema
@context = context.merge(
schema_generation_context?: true
)
@context = context
end

def generate_schema
check_changes
write_schema_to_file
# We will want to create a subclass of the schema here to make sure we don't pollute the original schema.
GraphQL::Schema::AlwaysVisible.use(@schema)
@schema.to_definition(context: @context)
end

private

def check_changes
return if old_schema.blank?

SchemaChangesChecker.new.check_changes(old_schema, @schema)
end

def old_schema
return unless File.exist?(@schema_output_path)

File.read(@schema_output_path)
SchemaChangesChecker.new.check_changes(old_schema, generate_schema)
end

def write_schema_to_file
File.write(@schema_output_path, schema_definition)
File.write(@schema_output_path, generate_schema)
puts Rainbow("\nSuccessfully updated #{@schema_output_path}").green
end

def schema_definition
GraphQL::Schema::Printer.print_schema(@schema, context: @context)
private

def old_schema
return unless File.exist?(@schema_output_path)

File.read(@schema_output_path)
end
end
end
Expand Down

0 comments on commit 2dd6afe

Please sign in to comment.