No release in over a year
GraphQL Query generator from Ruby object structure
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

ObjectToGraphQL

GraphQL Query generator from Ruby object structure.

gem 'object_to_graphql'

Usage

object = {
  user: {
    name: "Alice",
    email_address: "alice@example.com",
    accounts: [
      { name: "alice1" },
    ],
  }
}
ObjectToGraphQL.generate(object)
# => {
#      user {
#        name
#        emailAddress
#        accounts {
#          name
#        }
#      }
#    }

It works with arguments

object = {
  user: {
    name: "Alice",
    email_address: "alice@example.com",
    accounts: [
      { name: "alice1" },
    ],
  }
}

arguments = [
  # [[:path, :to, :the_field, :to_be_injected_argument], {name: argument_name, value: argument_value}]
  [[:user], {name: "ID", value: "id-1"}]
  [[:user, :name], {name: "separator", value: ","}],
  [[:user, :accounts], {name: "order", value: "desc"}]
]

ObjectToGraphQL.generate(object, arguments)
# => {
#      user(ID: "id-1") {
#        name(separator: ",")
#        emailAddress
#        accounts(order: "desc") {
#          name
#        }
#      }
#    }

It works with variables

object = {
  user: {
    name: "Alice",
    email_address: "alice@example.com",
    accounts: [
      { name: "alice1" },
    ],
  }
}

arguments = [
  [[:user], {name: "id", value: "$user_id"}],
]
variables = [
  {name: "$user_id", type: "ID!"}
]

ObjectToGraphQL.generate(object, arguments, variables)
# => query($user_id: ID!) {
#      user(id: $user_id) {
#        name
#        emailAddress
#        accounts {
#          name
#        }
#      }
#    }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/meganemura/object_to_graphql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ObjectToGraphQL project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.