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

Make suggest-queries script a graphql query #2

Merged
merged 1 commit into from
Feb 12, 2025
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
25 changes: 25 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,30 @@
module Types
class QueryType < Types::BaseObject
field :edition, resolver: Resolvers::ExpandedEditionResolver
field :suggest_query, type: String, null: true do
argument :base_path, String, required: true
end

def suggest_query(base_path:)
content_store_response = fetch_content(base_path)
query_builder = GraphqlQueryBuilder.new(content_store_response)
query_builder.build_query
end

private

def fetch_content(base_path)
url = URI("https://www.gov.uk/api/content#{base_path}".chomp("/"))
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
response = http.request(request)

if response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
else
raise "HTTP request failed with status #{response.code} #{response.message}"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "json"

class GraphQLQueryBuilder
class GraphqlQueryBuilder
# FIELDS_TO_IGNORE is a bit of a "to do" list really. We should implement these:
FIELDS_TO_IGNORE = %w[
api_path
Expand Down Expand Up @@ -76,7 +76,3 @@ def build_links_query(key, array, indent)
].join("\n")
end
end

content_store_response = JSON.parse(ARGF.read)
query_builder = GraphQLQueryBuilder.new(content_store_response)
puts query_builder.build_query
Loading