Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for disabling introspection entrypoints #2327

Merged
merged 2 commits into from
Jun 18, 2019

Conversation

DmitryTsepelev
Copy link
Contributor

That might be helpful to turn off introspection entry points, especially in the production environments. The shortest way to do it I found (given the fact that we don't have any custom entry points):

class MySchema < GraphQL::Schema
  if Rails.env.production?
    introspection(Module.new do
      class EntryPoints < GraphQL::Introspection::EntryPoints
        def self.authorized?(*)
          false
        end
      end
    end)
  end
end

It looks a little bit verbose and does not hide entry points from the schema, just restricts the access. I've added a shorthand method #disable_introspection_entry_points which allows to skip generating entry points:

class MySchema < GraphQL::Schema
  disable_introspection_entry_points Rails.env.production?
end

@eapache
Copy link
Contributor

eapache commented Jun 13, 2019

We accomplished something similar, but we used the existing except argument for schema visibility and passed it a class that just does schema_member.graphql_name == '__schema' || schema_member.graphql_name == '__type'.

@DmitryTsepelev
Copy link
Contributor Author

@eapache ...or it's possible to define resolving method raising an error for each entry point.

I'm not sure if this use case is common enough (but your comment makes me think I'm not alone), but if it is - I guess it would be great to have an easy way to get this behavior

@eapache
Copy link
Contributor

eapache commented Jun 13, 2019

Our approach was simple enough (4 lines of code) that I'm not sure it's worth actually packaging into the gem. Does seem worth to document a recommended way though.

@DmitryTsepelev
Copy link
Contributor Author

Let's wait for Robert to take a look - I'll be happy to transform this PR to the documentation amendment

@rmosolgo
Copy link
Owner

Hi, thanks for the contribution! I agree that this is a common enough request that it would be a good addition to the gem.

I just have one request about the API of the new method. Could we please change it to:

def disable_introspection_entry_points(if: true) 
  # ... 

I think that will yield more obvious code in the schema class, for example:

if Rails.env.production? 
  disable_introspection_entry_points 
end 

# or 
disable_introspection_entry_points(if: Rails.env.production?)

Thanks for including tests also. If you don't mind that small change, looks good to me!

@rmosolgo rmosolgo added this to the 1.9.7 milestone Jun 13, 2019
@DmitryTsepelev DmitryTsepelev force-pushed the disable-introspection branch 2 times, most recently from 172bd6d to 904f460 Compare June 13, 2019 14:21
@DmitryTsepelev
Copy link
Contributor Author

Sure, I've changed the API, but may be it worth change keyword kwarg if to something else to avoid that dirty hack around binding? Maybe we should stay with

if Rails.env.production? 
  disable_introspection_entry_points 
end 

and allow to pass the optional argument?

@rmosolgo
Copy link
Owner

avoid that dirty hack

🤦‍♂ Yes, you're totally right about that, sorry I forgot! Better to have the optional argument like you said.

@DmitryTsepelev DmitryTsepelev force-pushed the disable-introspection branch from 904f460 to 9453030 Compare June 13, 2019 15:57
@DmitryTsepelev
Copy link
Contributor Author

Done! Could you please review once again?

@celsworth
Copy link

Is there any value to the optional if: argument? Could you not accomplish the same with normal Ruby flow control?

By that I mean to say, what's better about doing:

disable_introspection_entry_points(if: Rails.env.production?)

Instead of:

disable_introspection_entry_points if Rails.env.production?

@rmosolgo
Copy link
Owner

I'd be happy to drop the if argument, but at the same time, I'm happy to accommodate @DmitryTsepelev 's preference. He's doing the work 😆

@DmitryTsepelev DmitryTsepelev force-pushed the disable-introspection branch from 9453030 to ccebeb0 Compare June 17, 2019 21:34
@DmitryTsepelev
Copy link
Contributor Author

@celsworth that's a fair point 🙂 I've updated my commit @rmosolgo

@rmosolgo
Copy link
Owner

Awesome, thanks again for this! I pushed a commit to make it play a bit nicer with the .define system.

@rmosolgo rmosolgo merged commit 7d08edf into rmosolgo:master Jun 18, 2019
@DmitryTsepelev
Copy link
Contributor Author

Thank you! ✨

TomasBarry added a commit to TomasBarry/graphql-ruby that referenced this pull request Jan 4, 2020
A question was asked in issue rmosolgoGH-2546 asking whether it was possible to
disable a single introspection entry point considering that rmosolgo#2327 had
implemented then functionality to disable introspection as a whole using
`disable_introspection_entry_points`. In the issue, an option was laid
out to implement the functionality for individual
`disable_schema_introspection_entry_point` and
`disable_type_introspection_entry_point` options. This commit implements
these methods.

These new methods are used in an identical manner to
`disable_introspection_entry_points`:

 # Disabling __type introspection

```ruby
class MySchema < GraphQL::Schema
  disable_type_introspection_entry_point
end
```

With the above, attempting to execute a query with `__type`
introspection will return the following error message:

```
Field '__schema' doesn't exist on type 'Query'
```

 # Disabling __schema introspection
```ruby
class MySchema < GraphQL::Schema
  disable_schema_introspection_entry_point
end
```

With the above, attempting to execute a query with `__schema`
introspection will return the following error message:

```
Field '__type' doesn't exist on type 'Query'
```

Note, having both `disable_type_introspection_entry_point` and
`disable_schema_introspection_entry_point` is identical to having just
`disable_introspection_entry_points`. Having
`disable_introspection_entry_points` and
`disable_type_introspection_entry_point` or
`disable_schema_introspection_entry_point` is no different to just
having `disable_introspection_entry_points`.
TomasBarry added a commit to TomasBarry/graphql-ruby that referenced this pull request Jan 4, 2020
A question was asked in issue rmosolgoGH-2546 asking whether it was possible to
disable a single introspection entry point considering that rmosolgo#2327 had
implemented then functionality to disable introspection as a whole using
`disable_introspection_entry_points`. In the issue, an option was laid
out to implement the functionality for individual
`disable_schema_introspection_entry_point` and
`disable_type_introspection_entry_point` options. This commit implements
these methods.

These new methods are used in an identical manner to
`disable_introspection_entry_points`:

 # Disabling `__type` introspection

```ruby
class MySchema < GraphQL::Schema
  disable_type_introspection_entry_point
end
```

With the above, attempting to execute a query with `__type`
introspection will return the following error message:

```
Field '__type' doesn't exist on type 'Query'
```

 # Disabling `__schema` introspection
```ruby
class MySchema < GraphQL::Schema
  disable_schema_introspection_entry_point
end
```

With the above, attempting to execute a query with `__schema`
introspection will return the following error message:

```
Field '__schema' doesn't exist on type 'Query'
```

Note, having both `disable_type_introspection_entry_point` and
`disable_schema_introspection_entry_point` is identical to having just
`disable_introspection_entry_points`. Having
`disable_introspection_entry_points` and
`disable_type_introspection_entry_point` or
`disable_schema_introspection_entry_point` is no different to just
having `disable_introspection_entry_points`.
TomasBarry added a commit to TomasBarry/graphql-ruby that referenced this pull request Jan 4, 2020
A question was asked in issue rmosolgoGH-2546 asking whether it was possible to
disable a single introspection entry point considering that rmosolgo#2327 had
implemented then functionality to disable introspection as a whole using
`disable_introspection_entry_points`. In the issue, an option was laid
out to implement the functionality for individual
`disable_schema_introspection_entry_point` and
`disable_type_introspection_entry_point` options. This commit implements
these methods.

These new methods are used in an identical manner to
`disable_introspection_entry_points`:

 # Disabling `__type` introspection

```ruby
class MySchema < GraphQL::Schema
  disable_type_introspection_entry_point
end
```

With the above, attempting to execute a query with `__type`
introspection will return the following error message:

```
Field '__type' doesn't exist on type 'Query'
```

 # Disabling `__schema` introspection
```ruby
class MySchema < GraphQL::Schema
  disable_schema_introspection_entry_point
end
```

With the above, attempting to execute a query with `__schema`
introspection will return the following error message:

```
Field '__schema' doesn't exist on type 'Query'
```

Note, having both `disable_type_introspection_entry_point` and
`disable_schema_introspection_entry_point` is identical to having just
`disable_introspection_entry_points`. Having
`disable_introspection_entry_points` and
`disable_type_introspection_entry_point` or
`disable_schema_introspection_entry_point` is no different to just
having `disable_introspection_entry_points`.
TomasBarry added a commit to TomasBarry/graphql-ruby that referenced this pull request Jan 4, 2020
A question was asked in issue rmosolgoGH-2546 asking whether it was possible to
disable a single introspection entry point considering that rmosolgo#2327 had
implemented then functionality to disable introspection as a whole using
`disable_introspection_entry_points`. In the issue, an option was laid
out to implement the functionality for individual
`disable_schema_introspection_entry_point` and
`disable_type_introspection_entry_point` options. This commit implements
these methods.

These new methods are used in an identical manner to
`disable_introspection_entry_points`:

 # Disabling `__type` introspection

```ruby
class MySchema < GraphQL::Schema
  disable_type_introspection_entry_point
end
```

With the above, attempting to execute a query with `__type`
introspection will return the following error message:

```
Field '__type' doesn't exist on type 'Query'
```

 # Disabling `__schema` introspection
```ruby
class MySchema < GraphQL::Schema
  disable_schema_introspection_entry_point
end
```

With the above, attempting to execute a query with `__schema`
introspection will return the following error message:

```
Field '__schema' doesn't exist on type 'Query'
```

Note, having both `disable_type_introspection_entry_point` and
`disable_schema_introspection_entry_point` is identical to having just
`disable_introspection_entry_points`. Having
`disable_introspection_entry_points` and
`disable_type_introspection_entry_point` or
`disable_schema_introspection_entry_point` is no different to just
having `disable_introspection_entry_points`.
TomasBarry added a commit to TomasBarry/graphql-ruby that referenced this pull request Jan 4, 2020
A question was asked in issue rmosolgoGH-2546 asking whether it was possible to
disable a single introspection entry point considering that rmosolgo#2327 had
implemented then functionality to disable introspection as a whole using
`disable_introspection_entry_points`. In the issue, an option was laid
out to implement the functionality for individual
`disable_schema_introspection_entry_point` and
`disable_type_introspection_entry_point` options. This commit implements
these methods.

These new methods are used in an identical manner to
`disable_introspection_entry_points`:

 # Disabling `__type` introspection

```ruby
class MySchema < GraphQL::Schema
  disable_type_introspection_entry_point
end
```

With the above, attempting to execute a query with `__type`
introspection will return the following error message:

```
Field '__type' doesn't exist on type 'Query'
```

 # Disabling `__schema` introspection
```ruby
class MySchema < GraphQL::Schema
  disable_schema_introspection_entry_point
end
```

With the above, attempting to execute a query with `__schema`
introspection will return the following error message:

```
Field '__schema' doesn't exist on type 'Query'
```

Note, having both `disable_type_introspection_entry_point` and
`disable_schema_introspection_entry_point` is identical to having just
`disable_introspection_entry_points`. Having
`disable_introspection_entry_points` and
`disable_type_introspection_entry_point` or
`disable_schema_introspection_entry_point` is no different to just
having `disable_introspection_entry_points`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants