<commit_before># ActiveGraphQL

[![Build Status](https://travis-ci.org/wakoopa/activegraphql.png)](https://travis-ci.org/wakoopa/activegraphql)

ActiveGraphQL connects classes to [GraphQL](http://graphql.org/) services.
The library provides a **Model** class that, when subclassed and configured, encapsulates the communication with the service.

```ruby
class MyModel < ActiveGraphQL::Model
  configure url: 'http://some-graphql-service/endpoint'
end
```

Any subclass of `ActiveGraphQL::Model` will provide the following methods:

- **all:** Retrive all objects for the entity.
- **where(conditions):** Retrieve all objects for the entity finding by conditions.
- **find_by(conditions):** Retrieve first object for the entity finding by conditions.

Any one of these methods returns an `ActiveGraphQL::Fetcher` who provides the method `fetch(*graph)` that is responsible of calling the service. The `*graph` arguments allow to specify how the response format will be.

For convention, any method is performing a call to the service with a query, that is resolved based on: model class name, conditions and graph.

**Retrieving all `MyModel` objects (just ask for retrieving `id`)**

```ruby
>> MyModel.all.fetch(:id).first.id
=> "1"
```

Resolved query:

```
{ myModels { id } }
```

**Retrieving all `MyModel` objects with `value == "a"` (ask for `id` and `name`)**

```ruby
>> m = MyModel.where(value: 'a').fetch(:id, :name).first

>> m.id
=> "3"

>> m.name
=> "Whatever"
```

Resolved query:

```
{ myModels("value": "a") { id, name } }
```

**Retrieving `MyModel` object with `id == "5"` (ask for `id`, `name` and `nestedObject { id }`)**

```ruby
>> m = MyModel.find_by(id: '5').fetch(:id, :name, nested_object: [:description])

>> m.id
=> "5"

>> m.name
=> "Whatever"

>> m.nested_object.description
=> "Some description here"
```

Resolved query:

```
{ myModel("id": "5") { id, name, nestedObject { description } } }
```

## Localisation support
Any fetcher provides the `in_locale(locale)` method that makes the call to include the `HTTP_ACCEPT_LANGUAGE` header to get the content localized in case of the service supporting it.

```ruby
>> MyModel.all.in_locale(:en).fetch(:body).first.body
=> "This is my text"

>> MyModel.all.in_locale(:es).fetch(:body).first.body
=> "Este es mi texto"

# Also accepts strings as locale
>> MyModel.all.in_locale('es_ES').fetch(:body).first.body
=> "Este es mi texto"
```
<commit_msg>Less confusing readme<commit_after># ActiveGraphQL

[![Build Status](https://travis-ci.org/wakoopa/activegraphql.png)](https://travis-ci.org/wakoopa/activegraphql)

ActiveGraphQL connects classes to [GraphQL](http://graphql.org/) services.
The library provides a **Model** class that, when subclassed and configured, encapsulates the communication with the service.

```ruby
class MyModel < ActiveGraphQL::Model
  configure url: 'http://some-graphql-service/endpoint'
end
```

Any subclass of `ActiveGraphQL::Model` will provide the following methods:

- **all:** Retrive all objects for the entity.
- **where(conditions):** Retrieve all objects for the entity finding by conditions.
- **find_by(conditions):** Retrieve first object for the entity finding by conditions.

Any one of these methods returns an `ActiveGraphQL::Fetcher` who provides the method `fetch(*graph)` that is responsible of calling the service. The `*graph` arguments allow to specify how the response format will be.

For convention, any method is performing a call to the service with a query, that is resolved based on: model class name, conditions and graph.

**Retrieving all `MyModel` objects (just ask for retrieving `id`)**

```ruby
>> MyModel.all.fetch(:id).first.id
=> "1"
```

Resolved query:

```
{ myModels { id } }
```

**Retrieving all `MyModel` objects with `value == "a"` (ask for `id` and `name`)**

```ruby
>> m = MyModel.where(value: 'a').fetch(:id, :name).first

>> m.id
=> "3"

>> m.name
=> "Whatever"
```

Resolved query:

```
{ myModels("value": "a") { id, name } }
```

**Retrieving `MyModel` object with `id == "5"` (ask for `id`, `name` and `nestedObject { id }`)**

```ruby
>> m = MyModel.find_by(id: '5').fetch(:id, :name, nested_object: [:description])

>> m.id
=> "5"

>> m.name
=> "Whatever"

>> m.nested_object.description
=> "Some description here"
```

Resolved query:

```
{ myModel("id": "5") { id, name, nestedObject { description } } }
```

## Localisation support
Any fetcher provides the `in_locale(locale)` method that makes the call to include the `HTTP_ACCEPT_LANGUAGE` header to get the content localized in case of the service supporting it.

```ruby
>> MyModel.all.in_locale(:en).fetch(:some_attribute).first.some_attribute
=> "This is my text"

>> MyModel.all.in_locale(:es).fetch(:some_attribute).first.some_attribute
=> "Este es mi texto"

# Also accepts strings as locale
>> MyModel.all.in_locale('es_ES').fetch(:some_attribute).first.some_attribute
=> "Este es mi texto"
```

{'fraction_non_alphanumeric': 0.1565922920892495, 'fraction_numerical': 0.002434077079107505, 'mean_word_length': 5.020757020757021, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '998981', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1553, 'n_words': 516}