Define structured input types for complex arguments in mutations and queries
Input objects organize multiple fields into structured arguments. They’re the input equivalent of objects, perfect for mutations and queries that accept complex data with multiple attributes.
Input objects can only contain input-compatible types:
Valid field types:
Scalars (:string, :int, :boolean, etc.)
Enums
Other Input objects
Invalid field types:
Objects (output types)
Interfaces
Unions
input 'CreatePostInput' do field :title, :string, null: false field :body, :string field :status, 'PostStatus' # ✓ Enum field :author, 'UserInput' # ✓ Another input field :tags, :string, array: true # ✓ Array of scalars # field :author, 'User' # ✗ Object typeend
input 'AddressInput' do field :street, :string, null: false field :city, :string, null: false field :postal_code, :stringendinput 'UserInput' do field :name, :string, null: false field :email, :string, null: false field :address, 'AddressInput'end
mutation { createUser(user: { name: "John Doe" email: "[email protected]" address: { street: "123 Main St" city: "Springfield" postalCode: "12345" } }) { id name }}
input 'CreatePostInput' do field :title, :string, null: false field :status, 'PostStatus', default: 'DRAFT' field :published, :boolean, default: falseend
input 'CreateUserInput' do field :name, :string, null: false field :email, :string, null: false field :role, 'Role', default: 'USER'endmutation_fields do field :create_user, 'User', null: false do argument :input, 'CreateUserInput', null: false endenddef create_user(input:) User.create!(input.params)end
input 'UpdateUserInput' do field :name, :string field :email, :string field :role, 'Role'endmutation_fields do field :update_user, 'User', null: false do argument :id, :id, null: false argument :input, 'UpdateUserInput', null: false endenddef update_user(id:, input:) user = User.find(id) user.update!(input.params) userend
input 'UserFilter' do field :role, 'Role' field :active, :boolean field :search, :stringendquery_fields do field :users, 'User', array: true do argument :filter, 'UserFilter' endenddef users(filter: nil) scope = User.all return scope unless filter scope = scope.where(role: filter.role.to_sym) if filter.args.role scope = scope.where(active: filter.active) if filter.args.active scope = scope.search(filter.search) if filter.args.search scopeend
input 'UserInput' do desc 'Input for creating or updating a user' field :name, :string, null: false, desc: 'Full name of the user' field :email, :string, null: false, desc: 'Primary email address'end