exAspArk / graphql-guard

Simple authorization gem for GraphQL :lock:
MIT License
471 stars 36 forks source link

Outdated Documentation #31

Closed danwetherald closed 4 years ago

danwetherald commented 4 years ago
class ShopType < Types::BaseObject
  field :title, String, null: false
  field :user, Types::Models::UserType, null: false
end

How would you include a guard on this field for the ShopType, we would like to limit the field user to only signed in users, but allow the other fields on a shop to be public without the need for authentication.

danwetherald commented 4 years ago
class ShopType < Types::BaseObject
    field :title, String, null: true

    field :user, Types::Models::UserType, null: true do
      guard ->(obj, args, ctx) { ctx[:current_user] && ctx[:current_shop] }
    end
  end

Just for anyone else who is looking to add guarded fields on BaseObjects.

exAspArk commented 4 years ago

Thank you @dan003400. Right, the existing documentation was written by using the previous graphql gem schema-definition DSL.

For those who would like to contribute and update the readme to use class-based schema definitions, here is an example from the gem specs:

https://github.com/exAspArk/graphql-guard/blob/b0e13cdce93a37a9d853849aa23e63674403f490/spec/fixtures/inline_schema.rb#L40-L64

ghost commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.