Skip to content
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10 }, your_p
# Note: more examples you can find in spec folder
```

### GraphQL API

The client can send GraphQL requests to Crowdin's default API host or to an Enterprise organization endpoint.

```ruby
query = <<~GRAPHQL
query Viewer {
viewer {
id
}
}
GRAPHQL

response = crowdin.graphql({ query: query })
```

You can also provide a custom endpoint URL for testing.

```ruby
crowdin.graphql({ query: query }, url: 'http://localhost:3000/api/graphql')
```

### Fetch all records

There is a possibility to fetch all records from paginatable methods using `fetch_all` method.
Expand Down
1 change: 1 addition & 0 deletions lib/crowdin-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Crowdin
require 'crowdin-api/core/request'
require 'crowdin-api/core/send_request'
require 'crowdin-api/core/fetch_all_extensions'
require 'crowdin-api/core/graphql_extensions'

# API modules
Crowdin::API_RESOURCES_MODULES.each do |api_resource|
Expand Down
11 changes: 3 additions & 8 deletions lib/crowdin-api/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ class Client
end

include Web::FetchAllExtensions
include Web::GraphqlExtensions

# Config instance that includes configuration options for the Client
attr_reader :config
# Instance with established connection through RestClient to the Crowdin API
attr_reader :connection
# Instance with options and headers for RestClient connection
attr_reader :options
# Logger instance
attr_reader :logger
# Client configuration, connection, request options, and logger instances
attr_reader :config, :connection, :options, :logger

def initialize(&block)
build_configuration(&block)
Expand Down
22 changes: 22 additions & 0 deletions lib/crowdin-api/core/graphql_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Crowdin
module Web
module GraphqlExtensions
def graphql(query = nil, **request_options)
url = request_options.delete(:url)
graphql_query = query || request_options

response = ::RestClient::Request.execute(
{
method: :post,
url: url || "#{config.base_url}/api/graphql",
payload: graphql_query.to_json
}.merge(options)
) { |res, _, _| res }

response.body.empty? ? response.code : JSON.parse(response.body)
end
end
end
end
47 changes: 47 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,53 @@
end
end

describe 'Crowdin Client graphql' do
let(:graphql_request) do
{
query: 'query Viewer { viewer { id } }',
operationName: 'Viewer',
variables: { projectId: 1, emptyValue: nil }
}
end
let(:graphql_response) { { 'data' => { 'viewer' => { 'id' => 1 } } } }

it 'posts to the default GraphQL endpoint', :default do
stub_request(:post, 'https://api.crowdin.com/api/graphql')
.with(body: graphql_request.to_json)
.to_return(body: graphql_response.to_json)

expect(@crowdin.graphql(graphql_request)).to eq(graphql_response)
end

it 'posts to the Enterprise GraphQL endpoint', :enterprise do
stub_request(:post, 'https://domain.api.crowdin.com/api/graphql')
.with(body: graphql_request.to_json)
.to_return(body: graphql_response.to_json)

expect(@crowdin.graphql(graphql_request)).to eq(graphql_response)
end

it 'supports a custom GraphQL endpoint URL', :default do
custom_url = 'http://localhost:3000/api/graphql'

stub_request(:post, custom_url)
.with(body: graphql_request.to_json)
.to_return(body: graphql_response.to_json)

expect(@crowdin.graphql(graphql_request, url: custom_url)).to eq(graphql_response)
end

it 'returns parsed GraphQL error responses', :default do
graphql_error_response = { 'errors' => [{ 'message' => 'Invalid query' }] }

stub_request(:post, 'https://api.crowdin.com/api/graphql')
.with(body: graphql_request.to_json)
.to_return(status: 400, body: graphql_error_response.to_json)

expect(@crowdin.graphql(graphql_request)).to eq(graphql_error_response)
end
end

describe 'connection' do
subject(:connection) { crowdin_client.connection }

Expand Down
Loading