Skip to content

Commit

Permalink
Merge pull request #1811 from cjoudrey/v1.8.8-prep
Browse files Browse the repository at this point in the history
1.8.8
  • Loading branch information
cjoudrey authored Aug 27, 2018
2 parents a6ed30f + d4a7314 commit fd8c87a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

### Bug fixes

## 1.8.8 (27 Aug 2018)

### Bug fixes

- When using `RelayClassicMutation`, `client_mutation_id` will no longer be passed to `authorized?` method #1771
- Fix issue in schema upgrader script which would cause `.to_non_null_type` calls in type definition to be ignored #1783
- Ensure enum values respond to `graphql_name` #1792
- Fix infinite resolution bug that could occur when an exception not inheriting from `StandardError` is thrown #1804

### New features

- Add `#path` method to schema members #1766
- Add `as:` argument to allow overriding the name of the argument when using `loads:` #1773
- Add support for list of IDs when using `loads:` in an argument definition #1797

## 1.8.7 (9 Aug 2018)

### Breaking changes
Expand Down
64 changes: 64 additions & 0 deletions guides/mutations/mutation_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,67 @@ class Types::Mutation < Types::BaseObject
field :create_comment, mutation: Mutations::CreateComment
end
```

## Auto-loading arguments

In most cases, a GraphQL mutation will act against a given global relay ID. Loading objects from these global relay IDs can require a lot of boilerplate code in the mutation's resolver.

An alternative approach is to use the `loads:` argument when defining the argument:

```ruby
class Mutations::AddStar < Mutations::BaseMutation
argument :post_id, ID, required: true, loads: Types::Post

field :post, Types::Post, null: true

def resolve(post:)
post.star

{
post: post,
}
end
end
```

By specifying that the `post_id` argument loads a `Types::Post` object type, a `Post` object will be loaded via {% internal_link "`Schema#object_from_id`", "/schema/definition.html#object-identification-hooks" %} with the provided `post_id`.

All arguments that end in `_id` and use the `loads:` method will have their `_id` suffix removed. For example, the mutation resolver above receives a `post` argument which contains the loaded object, instead of a `post_id` argument.

The `loads:` option also works with list of IDs, for example:

```ruby
class Mutations::AddStars < Mutations::BaseMutation
argument :post_ids, [ID], required: true, loads: Types::Post

field :posts, [Types::Post], null: true

def resolve(posts:)
posts.map(&:star)

{
posts: posts,
}
end
end
```

All arguments that end in `_ids` and use the `loads:` method will have their `_ids` suffix removed and an `s` appended to their name. For example, the mutation resolver above receives a `posts` argument which contains all the loaded objects, instead of a `post_ids` argument.

In some cases, you may want to control the resulting argument name. This can be done using the `as:` argument, for example:

```ruby
class Mutations::AddStar < Mutations::BaseMutation
argument :post_id, ID, required: true, loads: Types::Post, as: :something

field :post, Types::Post, null: true

def resolve(something:)
something.star

{
post: something
}
end
end
```
2 changes: 1 addition & 1 deletion lib/graphql/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module GraphQL
VERSION = "1.8.7"
VERSION = "1.8.8"
end

0 comments on commit fd8c87a

Please sign in to comment.