Skip to content

Commit

Permalink
Merge pull request #1290 from alphagov/add-graphql-client
Browse files Browse the repository at this point in the history
Add GraphQL endpoint for Publishing API
  • Loading branch information
brucebolt authored Oct 30, 2024
2 parents dbb3b20 + 8787eef commit fc240db
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased
## 97.1.0

* Add GraphQL endpoint for Publishing API [PR](https://github.com/alphagov/gds-api-adapters/pull/1290)
* Note: This endpoint is to be used for the GraphQL proof of concept prototype only.

## 97.0.0

Expand Down
9 changes: 9 additions & 0 deletions lib/gds_api/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,15 @@ def get_schema(schema_name)
get_json("#{endpoint}/v2/schemas/#{schema_name}").to_hash
end

# Make a GraphQL query
#
# @param query [String]
#
# @return [Hash] A response with the result of the GraphQL query.
def graphql_query(query)
post_json("#{endpoint}/graphql", query:).to_hash
end

private

def content_url(content_id, params = {})
Expand Down
13 changes: 13 additions & 0 deletions lib/gds_api/test_helpers/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ def stub_publishing_api_isnt_available
stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(status: 503)
end

# Stub a POST /graphql request
#
# @param query [String]
def stub_publishing_api_graphql_query(query, response_hash = {})
url = "#{PUBLISHING_API_ENDPOINT}/graphql"
response = {
status: 200,
body: response_hash.to_json,
headers: { "Content-Type" => "application/json; charset=utf-8" },
}
stub_request(:post, url).with(body: { query: }).to_return(response)
end

# Assert that a draft was saved and published, and links were updated.
# - PUT /v2/content/:content_id
# - POST /v2/content/:content_id/publish
Expand Down
2 changes: 1 addition & 1 deletion lib/gds_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GdsApi
VERSION = "97.0.0".freeze
VERSION = "97.1.0".freeze
end
42 changes: 42 additions & 0 deletions test/pacts/publishing_api/graphql_query_pact_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "test_helper"
require "gds_api/publishing_api"

describe "GdsApi::PublishingApi#graphql_query pact tests" do
include PactTest

let(:api_client) { GdsApi::PublishingApi.new(publishing_api_host) }

it "returns the response to the query" do
query = <<~QUERY
{
edition(basePath: "/my-document") {
... on Edition {
title
}
}
}
QUERY

publishing_api
.given("a published content item exists with base_path /my-document")
.upon_receiving("a GraphQL request")
.with(
method: :post,
path: "/graphql",
body: { query: },
headers: GdsApi::JsonClient.default_request_with_json_body_headers,
)
.will_respond_with(
status: 200,
body: {
"data": {
"edition": {
"title": "My document",
},
},
},
)

api_client.graphql_query(query)
end
end
26 changes: 26 additions & 0 deletions test/test_helpers/publishing_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,32 @@
end
end

describe "#stub_publishing_api_graphql_query" do
it "returns the given response" do
query = "some query"

stubbed_response = {
data: {
edition: {
title: "some title",
},
},
}

stub_publishing_api_graphql_query(
query,
stubbed_response,
)

api_response = publishing_api.graphql_query(query)

assert_equal(
stubbed_response.to_json,
api_response.to_json,
)
end
end

describe "#request_json_matching predicate" do
describe "nested required attribute" do
let(:matcher) { request_json_matching("a" => { "b" => 1 }) }
Expand Down

0 comments on commit fc240db

Please sign in to comment.