Skip to content

Commit

Permalink
GraphQL: Add isPuid query (#625)
Browse files Browse the repository at this point in the history
* Add isPuid query

* Change regex to match whole term only

* Move resolve method to its own resolver file

* Add tests for isPuid query

* Fix rubocop offenses

* Update schema.graphql

* Fix comment in resolver file
  • Loading branch information
malchua authored Jun 14, 2024
1 parent 35b11ee commit 4ed99dd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
31 changes: 31 additions & 0 deletions app/graphql/resolvers/is_puid_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Resolvers
# IsPuid Resolver
class IsPuidResolver < BaseResolver
argument :id, GraphQL::Types::ID,
required: true,
description: 'ID to compare to puid format'

type Boolean, null: false

def resolve(id:)
id_sections = id.split('_')
model_prefix = id_sections[1]
return false unless id_sections.length == 3

case model_prefix
when 'SAM'
Irida::PersistentUniqueId.valid_puid?(id, Sample)
when 'ATT'
Irida::PersistentUniqueId.valid_puid?(id, Attachment)
when 'GRP'
Irida::PersistentUniqueId.valid_puid?(id, Group)
when 'PRJ'
Irida::PersistentUniqueId.valid_puid?(id, Namespaces::ProjectNamespace)
else
false
end
end
end
end
10 changes: 10 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ type Query {
last: Int
): GroupConnection!

"""
Boolean
"""
isPuid(
"""
ID to compare to puid format
"""
id: ID!
): Boolean!

"""
Find a namespace.
"""
Expand Down
3 changes: 3 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class QueryType < Types::BaseObject
field :project_sample, Types::SampleType, null: true, resolver: Resolvers::ProjectSampleResolver,
description: 'Find a sample within a project.'

field :is_puid, Boolean, null: false, resolver: Resolvers::IsPuidResolver,
description: 'Check if id is in puid format'

def current_user
context[:current_user]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/irida/persistent_unique_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def time_to_base32(time)
end

def valid_puid?(puid, object_class = nil)
re = /#{app_prefix}_#{object_class.model_prefix}_([A-Z]|[2-7]){10}/
re = /\A#{app_prefix}_#{object_class.model_prefix}_([A-Z]|[2-7]){10}\z/
re.match?(puid)
end
end
Expand Down
43 changes: 43 additions & 0 deletions test/graphql/is_puid_query_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'test_helper'

class PuidQueryTest < ActiveSupport::TestCase
PUID_QUERY_TRUE = <<~GRAPHQL
query {
isPuid(id: "INXT_SAM_23F24BP6A7")
}
GRAPHQL

PUID_QUERY_FALSE = <<~GRAPHQL
query {
isPuid(id: "INXT_SAM_23D56SE3M8")
}
GRAPHQL

def setup
@user = users(:john_doe)
end

test 'is puid query should return true' do
result = IridaSchema.execute(PUID_QUERY_TRUE, context: { current_user: @user }, variables: {})

assert_nil result['errors'], 'should work and have no errors.'

data = result['data']

assert_not_empty data, 'puid type should work'
assert data['isPuid']
end

test 'is puid query should return false' do
result = IridaSchema.execute(PUID_QUERY_FALSE, context: { current_user: @user }, variables: {})

assert_nil result['errors'], 'should work and have no errors.'

data = result['data']

assert_not_empty data, 'puid type should work'
assert_not data['isPuid']
end
end

0 comments on commit 4ed99dd

Please sign in to comment.