Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL: Add isPuid query #625

Merged
merged 7 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading