Noh | エンジニア向け情報共有コミュニティ
Signup / Login

Name Enumerations in GraphQL Ruby as `〇〇_Type`.

ruby
rails
graphql

投稿日: 2024/06/25

更新日: 2024/07/02

This article is a translation of the following article.

https://noh.ink/articles/ybo9tz1IK8k1nWIuHe6R

I am yosi.

I didn't plan to investigate and write an article particularly about it during the work review, but I came across it again during Sunday coding, so I will write an article about it.

In GraphQL, you can define an Enum and write it as follows.
(Please replace the file path and module name as they may be different.)

app/graphql/types/enums/sort_type.rb

class Types::Enums::SortType < Types::BaseEnum value 'ASC', value: :asc value 'DESC', value: :desc end

This Enum can be used as follows. Let's assume that a suitable PostType is defined.

app/graphql/types/query_type.rb

module Types class QueryType < Types::BaseObject # 省略 field :posts, Types::Objects::PostType.connection_type, null: false do argument :sort, Types::Enums::SortType, required: false, default_value: :desc end def posts(sort:) Post.order(id: sort) end end end

It may seem like it's completed at first glance, but it does not behave as intended.

Let's output the GraphQL schema file with bundle exec rails graphql:schema:json.

schema.json

{ "kind": "ENUM", "name": "Sort", "description": null, "interfaces": null, "possibleTypes": null, "fields": null, "inputFields": null, "enumValues": [ { "name": "ASC", "description": null, "isDeprecated": false, "deprecationReason": null }, { "name": "DESC", "description": null, "isDeprecated": false, "deprecationReason": null } ] },

The name has been changed to Sort and the _Type attached to the class name has been removed.

I checked the enum list in the Github API v4, and there are many Enums that end with _type, so it doesn't seem impossible from a specification standpoint.

Set a different name for Enum from the class name

The conclusion is that it can be achieved by specifying graphql_name.

app/graphql/types/enums/sort_type.rb

class Types::Enums::SortType < Types::BaseEnum + graphql_name 'SortType' + value 'ASC', value: :asc value 'DESC', value: :desc end

When you run bundle exec rails graphql:schema:json, you can confirm that the name is correctly set to SortType in the GraphQL schema file.

schema.json

{ "kind": "ENUM", "name": "SortType", "description": null, "interfaces": null, "possibleTypes": null, "fields": null, "inputFields": null, "enumValues": [ { "name": "ASC", "description": null, "isDeprecated": false, "deprecationReason": null }, { "name": "DESC", "description": null, "isDeprecated": false, "deprecationReason": null } ] },

Summary and Digression

I read the documentation, but couldn't find any mention of this area which has me a little confused. However, as I was reading the source code, I found the following comment in /lib/graphql/schema/enum.rb:

@param graphql_name [String, Symbol] the GraphQL value for this, usually SCREAMING_CASE

I see.

By the way, it is a secret that it crossed my mind for a moment that I should just use SortTypeType.

Table of Contents