Skip to content

Commit

Permalink
feat(api-key-management) Add Graphql types and queries for ApiKey mod…
Browse files Browse the repository at this point in the history
…el (#2743)

Added new graphql types and queries for ApiKey model.
  • Loading branch information
floganz authored Oct 30, 2024
1 parent 550e792 commit 660fa7a
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/graphql/resolvers/api_key_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Resolvers
class ApiKeyResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'developers:keys:manage'

argument :id, ID, required: true, description: 'Uniq ID of the API key'

description 'Query the API key'

type Types::ApiKeys::Object, null: false

def resolve(id: nil)
current_organization.api_keys.find(id)
rescue ActiveRecord::RecordNotFound
not_found_error(resource: 'api_key')
end
end
end
21 changes: 21 additions & 0 deletions app/graphql/resolvers/api_keys_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Resolvers
class ApiKeysResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'developers:keys:manage'

description 'Query the API keys of current organization'

argument :limit, Integer, required: false
argument :page, Integer, required: false

type Types::ApiKeys::SanitizedObject.collection_type, null: false

def resolve(page: nil, limit: nil)
current_organization.api_keys.page(page).limit(limit)
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/api_keys/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Types
module ApiKeys
class Object < Types::BaseObject
graphql_name 'ApiKey'

field :id, ID, null: false
field :value, String, null: false

field :created_at, GraphQL::Types::ISO8601DateTime, null: false
end
end
end
13 changes: 13 additions & 0 deletions app/graphql/types/api_keys/sanitized_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Types
module ApiKeys
class SanitizedObject < Object
graphql_name 'SanitizedApiKey'

def value
"••••••••" + object.value.last(3)
end
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class QueryType < Types::BaseObject

field :add_on, resolver: Resolvers::AddOnResolver
field :add_ons, resolver: Resolvers::AddOnsResolver
field :api_key, resolver: Resolvers::ApiKeyResolver
field :api_keys, resolver: Resolvers::ApiKeysResolver
field :billable_metric, resolver: Resolvers::BillableMetricResolver
field :billable_metrics, resolver: Resolvers::BillableMetricsResolver
field :coupon, resolver: Resolvers::CouponResolver
Expand Down
42 changes: 42 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 660fa7a

Please sign in to comment.