Graphql-ruby: creating nested input objects in graphql-ruby

Created on 24 Sep 2019  路  1Comment  路  Source: rmosolgo/graphql-ruby

@rmosolgo in the graphql spec, its generally encouraged to have nested keys in your input objects.

e.g.

input CreatePostInput {
  post: {
    title: String;
    description: String;
  }
}

type CreatePostPayload {
  post: {
    id: Id;
    title: String;
    description: String;
    createdAt: DateTime;
  }
}

type Mutation {
   createPost(input: CreatePostInput!): CreatePostPayload
}

how do I do the same within the graphql gem?

Most helpful comment

The easiest way is to use RelayClassicMutation, eg:

class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation

Then, any argument ...s defined on mutations will be added to the *Input type, which is generated by the library.

If you want to do it by hand, you can create an input object, then use that input object as an argument type, for example:

class Types::CreatePostInput < Types::BaseInputObject 
  argument :title, String, required: true
  argument :description, String, required: true
end 

# ... 
class Mutations::CreatePost < Mutations::BaseMutation 
  argument :input, Types::CreatePostInput, required: true
end 

Then, it will be like the schema you posted in the example.

>All comments

The easiest way is to use RelayClassicMutation, eg:

class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation

Then, any argument ...s defined on mutations will be added to the *Input type, which is generated by the library.

If you want to do it by hand, you can create an input object, then use that input object as an argument type, for example:

class Types::CreatePostInput < Types::BaseInputObject 
  argument :title, String, required: true
  argument :description, String, required: true
end 

# ... 
class Mutations::CreatePost < Mutations::BaseMutation 
  argument :input, Types::CreatePostInput, required: true
end 

Then, it will be like the schema you posted in the example.

Was this page helpful?
0 / 5 - 0 ratings