@@ -343,38 +343,32 @@ and an error is returned in a REST format as the request never reaches your GQL
343343
344344#### Authenticate in Your GQL Schema
345345For this you will need to add the ` GraphqlDevise::SchemaPlugin ` to your schema as described
346- [ here] ( #mounting-operations-into-your-own-schema ) and also set the authenticated resource
347- in a ` before_action ` hook.
346+ [ here] ( #mounting-operations-into-your-own-schema ) .
348347
349348``` ruby
350349# app/controllers/my_controller.rb
351350
352351class MyController < ApplicationController
353352 include GraphqlDevise ::Concerns ::SetUserByToken
354353
355- before_action -> { set_resource_by_token(:user ) }
356-
357354 def my_action
358- render json: DummySchema .execute(params[:query ], context: graphql_context)
355+ render json: DummySchema .execute(params[:query ], context: graphql_context( :user ) )
359356 end
360357end
361-
362- # @resource.to_s.underscore.tr('/', '_').to_sym
363358```
364- The ` set_resource_by_token ` method receives a symbol identifying the resource you are trying
359+ The ` graphql_context ` method receives a symbol identifying the resource you are trying
365360to authenticate. So if you mounted the ` 'User' ` resource, the symbol is ` :user ` . You can use
366361this snippet to find the symbol for more complex scenarios
367- ` resource_klass.to_s.underscore.tr('/', '_').to_sym ` .
362+ ` resource_klass.to_s.underscore.tr('/', '_').to_sym ` . ` graphql_context ` can also take an
363+ array of resources if you mounted more than one into your schema. The gem will try to
364+ authenticate a resource for each element on the array until it finds one.
368365
369- The ` graphql_context ` method is simply a helper method that returns a hash like this
370- ``` ruby
371- { current_resource: @resource , controller: self }
372- ```
373- These are the two values the gem needs to check if a user is authenticated and to perform
374- other auth operations. All ` set_resource_by_token ` does is set the ` @resource ` variable if
375- the provided authentication headers are valid. If authentication fails, resource will be ` nil `
376- and this is how ` GraphqlDevise::SchemaPlugin ` knows if a user is authenticated or not in
377- each query.
366+ Internally in your own mutations and queries a key ` current_resource ` will be available in
367+ the context if a resource was successfully authenticated or ` nil ` otherwise.
368+
369+ Keep in mind that sending multiple values to the ` graphql_context ` method means that depending
370+ on who makes the request, the context value ` current_resource ` might contain instances of the
371+ different models you might have mounted into the schema.
378372
379373Please note that by using this mechanism your GQL schema will be in control of what queries are
380374restricted to authenticated users and you can only do this at the root level fields of your GQL
0 commit comments