Resolving GraphQL/ExtractType rubocop offense in mutation type rails
Learn how to resolve the GraphQL/ExtractType rubocop offense in your Rails mutation type. Discover best practices for organizing and structuring your GraphQL code and how to use GraphQL types to create more maintainable and scalable GraphQL APIs.

Before we do any refactoring, currently, my project’s mutation root looks like the below in schema:
type Mutation {
adminCreateCategory(
input: AdminCreateCategoryInput!
): Category
adminCreateKind(
input: AdminCreateKindInput!
): Kind
adminCreateVendor(
input: AdminCreateVendorInput!
): Vendor
adminLogin(
input: AdminLoginInput!
): AdminLoginPayload
}
Also, the mutation_type.rb
looks like this:
module Types
# Path: app/graphql/types/mutation_type.rb
class MutationType < Types::BaseObject
description "The mutation root of this schema"
field :admin_login, mutation: Mutations::AdminLogin, description: "Admin login"
field :admin_create_category, mutation: Mutations::AdminCreateCategory, description: "Create a new category"
field :admin_create_kind, mutation: Mutations::AdminCreateKind, description: "Create a new kind"
field :admin_create_vendor, mutation: Mutations::AdminCreateVendor, description: "Create a new vendor"
end
end
My rubocop says:
Consider moving admin_login, admin_create_category, admin_create_kind, admin_create_vendor to a new type and adding the `admin` field instead (convention:GraphQL/ExtractType)
To resolve this offense, we will move to a separate common field, admin,
in the mutation type.
Solution:
Create a new file app/graphql/types/admin_mutation_type.rb
with the following text:
module Types
# Admin mutation root
class AdminMutationType < Types::BaseObject
description "The admin mutation root of this schema"
field :login, mutation: Mutations::AdminLogin, description: "Admin login"
field :create_category, mutation: Mutations::AdminCreateCategory, description: "Create a new category"
field :create_kind, mutation: Mutations::AdminCreateKind, description: "Create a new kind"
field :create_vendor, mutation: Mutations::AdminCreateVendor, description: "Create a new vendor"
end
end
Update app/graphql/types/mutation_type.rb
with the following code:
module Types
# Path: app/graphql/types/mutation_type.rb
class MutationType < Types::BaseObject
description "The mutation root of this schema"
field :admin, Types::AdminMutationType, null: false, description: "Admin mutations"
def admin
Types::AdminMutationType
end
end
end
Now the new schema will be changed as follows:
type Mutation {
admin: AdminMutation!
}
type AdminMutation {
createCategory(
input: AdminCreateCategoryInput!
): Category
createKind(
input: AdminCreateKindInput!
): Kind
createVendor(
input: AdminCreateVendorInput!
): Vendor
login(
input: AdminLoginInput!
): AdminLoginPayload
}
Now refactor test cases according to the new changes. We can also create a separate type for create
for mutation of create in the admin.
The same procedure can also be done in query_type.rb
while refactoring.
Happy Coding!