Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Unable to retrieve arguments from the record in Policy ? #59

Open
aponsin opened this issue Aug 14, 2018 · 2 comments
Open

Unable to retrieve arguments from the record in Policy ? #59

aponsin opened this issue Aug 14, 2018 · 2 comments

Comments

@aponsin
Copy link

aponsin commented Aug 14, 2018

The mutation:

module MyApp::GraphQL::Types::Mutations
  module Groups
    extend ActiveSupport::Concern

    included do
      field :create_sub_group, MyApp::GraphQL::Types::Objects::Group, null: false, authorize!: true, description: "Add a sub group to a group" do
        argument :parent_group_id, String, "Parent group id", required: true
        argument :name, String, "Group name", required: true
        argument :description, String, "Group description (optional)", required: false
      end
    end

    def create_sub_group(parent_group_id:, name:, description:)
      group = MyApp::Models::Group.create(parent_group_id: parent_group_id,
        name: name, description: description, owner: context[:current_user])

      if group.persisted?
        group
      else
        GraphQL::ExecutionError.new(group.errors.full_messages.join(". "), extensions: { code: 'unprocessable_entity' })
      end
    end
  end
end

The policy:

module MyApp
  module Policies
    class MutationTypePolicy < MyApp::Policies::ApplicationPolicy

      def create_sub_group?
        !user.nil? && record.arguments[:parent_group_id] == 'xxx'
      end

    end
  end
end

The part that does not work is obviously: record.arguments[:parent_group_id] == 'xxx' because I totally made it up. But it's to illustrate what I am trying to do ?
I can't seem to figure out a way to achieve that.

This is all I got when I am in that method:

[7] pry(#<MyApp::Policies::MutationTypePolicy>)> self
=> #< MyApp::Policies::MutationTypePolicy:0x00007fc2f2cce0e0
 @record=#< MyApp::GraphQL::Types::MutationType:0x00007fc2f2cdbab0 @context=#<Query::Context ...>, @object=nil>,
 @user=#< MyApp::Models::User _id: "acb09ad2-1cd6-4850-9fb1-4f14153e8006", created_at: Tue, 14 Aug 2018 22:15:20 UTC +00:00, email: "[email protected]", updated_at: Tue, 14 Aug 2018 22:37:16 UTC +00:00>>
@aponsin
Copy link
Author

aponsin commented Aug 15, 2018

What I have done for the moment to get around this if define a method to fetch the query arguments into the root object used as the record.

module MyApp::GraphQL::Types
  class MutationType < Objects::Base

    attr_reader :arguments
  
    def fetch_arguments(mutation_type)
      argument_keys = mutation_type.argument_values.keys.map(&:underscore).map(&:to_sym)
      argument_values = mutation_type.argument_values.values.map(&:value)
      @arguments = argument_keys.zip(argument_values).to_h
      self
    end

That way I can write my field definition like this:

field :create_sub_group, MyApp::GraphQL::Types::Objects::Group, null: false,
        authorize!: true, record: ->(root, args, ctx) { root.fetch_arguments(args) },  do
  ....

And I can finally access the arguments like so in my policy:

def create_sub_group?
  !user.nil? && user.id == MyApp::Models::Group.where(id: record.arguments[:parent_group_id]).first.try(:owner_id)
end

@aponsin
Copy link
Author

aponsin commented Aug 15, 2018

but it feels really hacky, isn't there any built-in way to get those arguments ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant