Table of Contents

I18n

GraphQL can be used for both private and public APIs. When a GraphQL API is exposed public, you will likely need docs about it. Although you can’t translate the name of fields, types, and other things, you can still provide a translated version of all the descriptions.

Even when the API is not public, the YAML structure can be a great place to describe your components and remove them from the actual code.

Affects Performance This is a heavy process, so it is recommended to enable only when delivering the documentation of your API or in development mode.

The Scopes

This gem uses a series of scopes, defined in i18n_scopes, to grab the description of something. You can change it to improve performance or even to support a different structure. Here is the default value:

config.i18n_scopes = [
  'graphql.%{namespace}.%{kind}.%{parent}.%{name}',
  'graphql.%{namespace}.%{kind}.%{name}',
  'graphql.%{namespace}.%{name}',
  'graphql.%{kind}.%{parent}.%{name}',
  'graphql.%{kind}.%{name}',
  'graphql.%{name}',
]

The Elements

These are the available values that can be interpolated into the scopes:

namespace

Ensures that the description complies with namespaces. It can only be achieved when getting the description using the introspection, from a to_gql output, or passing as the first argument of the description method.

Read more about namespaces.

kind

One of the given values, based on the type of the object of the description: scalar, object, interface, union, enum, input_object, schema, field, argument, or directive

parent

There are three possible scenarios here:

  1. The value provided by i18n_scope, if the parent has such method;
  2. The schema_type, if it is a field of a schema type;
  3. The parent’s to_sym value.

name

The symbolized version of the name of the object of the description.

Read more about names.

Collision

If, for some reason, any of the scopes return something other than a plain String, that key will be skipped.

Example

Given the schema described below, the following scopes will be checked when collecting descriptions:

# app/graphql/sample_schema.rb
module GraphQL
  class SampleSchema < GraphQL::Schema
    namespace :sample

    object 'User' do
      field(:name)
    end

    query_fields do
      field(:hello) { argument(:world) }
    end
  end
end

For Schema

namespace kind parent name
"sample" "schema" nil "schema"
[
  "graphql.sample.schema.schema",
  "graphql.sample.schema",
  "graphql.schema.schema",
  "graphql.schema",
]

For Field

namespace kind parent name
"sample" "field" "query" "hello"
[
  "graphql.sample.field.query.hello",
  "graphql.sample.field.hello",
  "graphql.sample.hello",
  "graphql.field.query.hello",
  "graphql.field.hello",
  "graphql.hello",
]

For Argument

namespace kind parent name
"sample" "argument" "hello" "world"
[
  "graphql.sample.argument.hello.world",
  "graphql.sample.argument.world",
  "graphql.sample.world",
  "graphql.argument.hello.world",
  "graphql.argument.world",
  "graphql.world",
]

For Object

namespace kind parent name
"sample" "object" nil "user"
[
  "graphql.sample.object.user",
  "graphql.sample.user",
  "graphql.object.user",
  "graphql.user",
]

For Object Field

namespace kind parent name
"sample" "field" "user" "name"
[
  "graphql.sample.field.user.name",
  "graphql.sample.field.name",
  "graphql.sample.name",
  "graphql.field.user.name",
  "graphql.field.name",
  "graphql.name",
]