Skip to content

Commit

Permalink
Added example with a static query that works with allow_dynamic_queri…
Browse files Browse the repository at this point in the history
…es: false.
  • Loading branch information
dblock committed Oct 19, 2017
1 parent 1c896b7 commit f627af2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
13 changes: 10 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-10-19 14:20:03 -0400 using RuboCop version 0.47.1.
# on 2017-10-19 15:16:01 -0400 using RuboCop version 0.47.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 9
# Offense count: 11
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 136

# Offense count: 15
# Offense count: 17
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand All @@ -22,6 +22,13 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 12

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'spec/graphlient/static_client_query_spec.rb'

# Offense count: 5
Style/Documentation:
Exclude:
Expand Down
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Gem Version](https://badge.fury.io/rb/graphlient.svg)](https://badge.fury.io/rb/graphlient)
[![Build Status](https://travis-ci.org/ashkan18/graphlient.svg?branch=master)](https://travis-ci.org/ashkan18/graphlient)

A Ruby Client for consuming GraphQL-based APIs without all the messy strings.
A much friendlier Ruby client for consuming GraphQL-based APIs, without all the messy strings. Built on top of your usual [graphql-client](https://github.com/github/graphql-client).

## Installation

Expand Down Expand Up @@ -31,7 +31,7 @@ The schema is available automatically via `.schema`.
client.schema # GraphQL::Schema
```

Make queries with `query`, which gets a block for the query definition.
Make queries with `query`, which takes a block for the query definition.

```ruby
response = client.query do
Expand Down Expand Up @@ -122,7 +122,8 @@ end

### Parse and Execute Queries Separately

You can `parse` and `execute` queries separately.
You can `parse` and `execute` queries separately with optional variables. This is highly recommended as parsing a query and validating a query on every request adds performance overhead. Parsing queries early allows validation errors to be discovered before request time and avoids many potential security issues.


```ruby
# parse a query, returns a GraphQL::Client::OperationDefinition
Expand All @@ -139,6 +140,52 @@ end
client.execute query, ids: [42]
```

### Dynamic vs. Static Queries

Graphlient uses [graphql-client](https://github.com/github/graphql-client), which [recommends](https://github.com/github/graphql-client/blob/master/guides/dynamic-query-error.md) building queries as static module members along with dynamic variables during execution. This can be accomplished with graphlient the same way.

Create a new instance of `Graphlient::Client` with a URL and optional headers.

```ruby
module SWAPI
Client = Graphlient::Client.new('https://test-graphql.biz/graphql',
headers: {
'Authorization' => 'Bearer 123'
},
allow_dynamic_queries: false
)
end
```

The schema is available automatically via `.schema`.

```ruby
SWAPI::Client.schema # GraphQL::Schema
```

Define a query.

```ruby
module SWAPI
InvoiceQuery = Client.parse do
query(:$id => :Int) do
invoice(id: :$id) do
id
fee_in_cents
end
end
end
end
```

Execute the query.

```ruby
response = SWAPI::Client.execute(SWAPI::InvoiceQuery, id: 42)
```

Note that the client is created with `allow_dynamic_queries: false` in the example above and with `allow_dynamic_queries: true` in Graphclient by default. This option is marked deprecated, but we're proposing to remove it and default it to `true` in [graphql-client#128](https://github.com/github/graphql-client/issues/128).

### Generate Queries with Graphlient::Query

You can directly use `Graphlient::Query` to generate raw GraphQL queries.
Expand Down Expand Up @@ -218,3 +265,4 @@ end
## License

MIT License, see [LICENSE](LICENSE)

39 changes: 39 additions & 0 deletions spec/graphlient/static_client_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe Graphlient::Client do
describe 'parse and execute' do
module Graphlient::Client::Spec
Client = Graphlient::Client.new(
'http://graph.biz/graphql',
headers: { 'Authorization' => 'Bearer 1231' },
allow_dynamic_queries: false
) do |client|
client.http do |h|
h.connection do |c|
c.use Faraday::Adapter::Rack, Sinatra::Application
end
end
end

Query = Client.parse do
query(:$ids => :'[Int]') do
invoices(ids: :$ids) do
id
fee_in_cents
end
end
end
end

it 'defaults allow_dynamic_queries to false' do
expect(Graphlient::Client::Spec::Client.send(:client).allow_dynamic_queries).to be false
end

it '#execute' do
response = Graphlient::Client::Spec::Client.execute(Graphlient::Client::Spec::Query, ids: [42])
invoices = response.data.invoices
expect(invoices.first.id).to eq 42
expect(invoices.first.fee_in_cents).to eq 20_000
end
end
end

0 comments on commit f627af2

Please sign in to comment.