|
| 1 | +module GraphqlDevise |
| 2 | + class SchemaPlugin |
| 3 | + DEFAULT_NOT_AUTHENTICATED = ->(type, field) { raise GraphqlDevise::UserError, "#{type}.#{field} requires authentication" } |
| 4 | + |
| 5 | + def initialize(resource, options, query, default = true, mutation = nil, unauthenticated = DEFAULT_NOT_AUTHENTICATED) |
| 6 | + @resource = resource |
| 7 | + @options = options |
| 8 | + @query = query |
| 9 | + @mutation = mutation |
| 10 | + @unauthenticated = unauthenticated |
| 11 | + @default = default |
| 12 | + |
| 13 | + load_fields |
| 14 | + end |
| 15 | + |
| 16 | + def use(schema_definition) |
| 17 | + schema_definition.instrument(:field, self) |
| 18 | + end |
| 19 | + |
| 20 | + def instrument(type, field) |
| 21 | + return field unless type.name == 'Query' || type.name == 'Mutation' |
| 22 | + |
| 23 | + auth_value = find_auth_value(type, field) |
| 24 | + authentication_required = if auth_value.nil? |
| 25 | + @default |
| 26 | + else |
| 27 | + auth_value |
| 28 | + end |
| 29 | + |
| 30 | + old_resolve_proc = field.resolve_proc |
| 31 | + new_resolve_proc = lambda do |object, arguments, context| |
| 32 | + @unauthenticated.call(type, field.name.to_sym) if authentication_required && context[:current_user].blank? |
| 33 | + |
| 34 | + old_resolve_proc.call(object, arguments, context) |
| 35 | + end |
| 36 | + |
| 37 | + field.redefine { resolve(new_resolve_proc) } |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def load_fields |
| 43 | + default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES) |
| 44 | + mapping_name = @resource.to_s.underscore.tr('/', '_').to_sym |
| 45 | + |
| 46 | + # clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS |
| 47 | + clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(@options).call! |
| 48 | + |
| 49 | + GraphqlDevise::MountMethod::OptionsValidator.new( |
| 50 | + [ |
| 51 | + GraphqlDevise::MountMethod::OptionValidators::SkipOnlyValidator.new(options: clean_options), |
| 52 | + GraphqlDevise::MountMethod::OptionValidators::ProvidedOperationsValidator.new( |
| 53 | + options: clean_options, supported_operations: default_operations |
| 54 | + ) |
| 55 | + ] |
| 56 | + ).validate! |
| 57 | + |
| 58 | + authenticatable_type = clean_options.authenticatable_type.presence || |
| 59 | + "Types::#{@resource}Type".safe_constantize || |
| 60 | + GraphqlDevise::Types::AuthenticatableType |
| 61 | + |
| 62 | + prepared_mutations = GraphqlDevise::MountMethod::OperationPreparer.new( |
| 63 | + mapping_name: mapping_name, |
| 64 | + custom: clean_options.operations, |
| 65 | + additional_operations: clean_options.additional_mutations, |
| 66 | + preparer: GraphqlDevise::MountMethod::OperationPreparers::MutationFieldSetter.new(authenticatable_type), |
| 67 | + selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( |
| 68 | + default: GraphqlDevise::DefaultOperations::MUTATIONS, only: clean_options.only, skipped: clean_options.skip |
| 69 | + ) |
| 70 | + ).call |
| 71 | + |
| 72 | + raise Error 'You need to define a mutation type' if prepared_mutations.any? && @mutation.blank? |
| 73 | + |
| 74 | + prepared_mutations.each do |action, mutation| |
| 75 | + @mutation.field(action, mutation: mutation, authenticate: false) |
| 76 | + end |
| 77 | + |
| 78 | + prepared_queries = GraphqlDevise::MountMethod::OperationPreparer.new( |
| 79 | + mapping_name: mapping_name, |
| 80 | + custom: clean_options.operations, |
| 81 | + additional_operations: clean_options.additional_queries, |
| 82 | + preparer: GraphqlDevise::MountMethod::OperationPreparers::ResolverTypeSetter.new(authenticatable_type), |
| 83 | + selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( |
| 84 | + default: GraphqlDevise::DefaultOperations::QUERIES, only: clean_options.only, skipped: clean_options.skip |
| 85 | + ) |
| 86 | + ).call |
| 87 | + |
| 88 | + prepared_queries.each do |action, resolver| |
| 89 | + @query.field(action, resolver: resolver, authenticate: false) |
| 90 | + end |
| 91 | + |
| 92 | + GraphqlDevise.add_mapping(mapping_name, @resource) |
| 93 | + end |
| 94 | + |
| 95 | + def find_auth_value(_, field) |
| 96 | + field.metadata[:authenticate] |
| 97 | + end |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +GraphQL::Field.accepts_definitions(authenticate: GraphQL::Define.assign_metadata_key(:authenticate)) |
| 102 | +GraphQL::Schema::Field.accepts_definition(:authenticate) |
0 commit comments