在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):ajsharp/graphql-rails-generators开源软件地址(OpenSource Url):https://github.com/ajsharp/graphql-rails-generators开源编程语言(OpenSource Language):Ruby 100.0%开源软件介绍(OpenSource Introduction):graphql-rails-generatorsA few generators to make it easy to integrate your Rails models with graphql-ruby. I created this because I was wasting too many keystrokes copying my model schema by hand to create graphql types. This project contains generators that look at your ActiveRecord model schema and generates graphql types for you.
Installation
RequirementsThis library only supports ActiveRecord, though it would be fairly trivial to add support for other ORMs. Usagegql:model_typeGenerate a model type from a model.
Options
Example# app/graphql/post_type.rb
module Types
class PostType < Types::BaseObject
field :id, GraphQL::Types::ID, null: true
field :title, String, null: true
field :body, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
end
end gql:input MODEL_CLASSGenerate an input type from a model.
Options
Example# app/graphql/types/post_input.rb
module Types
module Input
class PostInput < Types::BaseInputObject
argument :title, String, required: false
argument :body, String, required: false
end
end
end gql:mutations MODEL_CLASSGenerate create, update and delete mutations for a model.
Example# app/graphql/types/post_input.rb
module Types
module Input
class PostInput < Types::BaseInputObject
argument :title, String, required: false
argument :body, String, required: false
end
end
end gql:mutation MUTATION_PREFIX MODEL_NAMEGenerate a mutation class from a model. A quick note about the mutation generator... The mutation generator generates something akin to an "upsert" mutation. It takes two arguments: an optional
Example# app/graphql/mutations/update_post.rb
module Mutations
class UpdatePost < Mutations::BaseMutation
field :post, Types::PostType, null: true
argument :attributes, Types::Input::PostInput, required: true
argument :id, GraphQL::Types::ID, required: false
def resolve(attributes:, id: nil)
model = find_or_build_model(id)
model.attributes = attributes.to_h
if model.save
{post: model}
else
{errors: model.errors.full_messages}
end
end
def find_or_build_model(id)
if id
Post.find(id)
else
Post.new
end
end
end
end gql:search_object MODEL_NAMEGenerate a search object from a model using SearchObjectGraphQL If you have not yet created a base search resolver:
*Adds Example# app/graphql/resolvers/base_search_resolver.rb
module Resolvers
class BaseSearchResolver < GraphQL::Schema::Resolver
require 'search_object'
require 'search_object/plugin/graphql'
include SearchObject.module(:graphql)
end
end Then generate a search object for your model:
Example# app/graphql/resolvers/post_search.rb
module Resolvers
class PostSearch < Resolvers::BaseSearchResolver
type [Types::PostType], null: false
description "Lists posts"
scope { Post.all }
option(:id, type: Int) { |scope, value| scope.where id: value }
option(:title, type: String) { |scope, value| scope.where title: value }
option(:body, type: Int) { |scope, value| scope.where rating: value }
option(:created_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where created_at: value }
option(:updated_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where updated_at: value }
def resolve
[]
end
end
end This will also insert a search field into the beginning of query_type.rb #app/graphql/types/query_type.rb
module Types
class QueryType < Types::BaseObject
field :posts, resolver: Resolvers::PostSearch
... |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论