schema" # First describe the field signature: field :post, PostType, null: true do description "Find a post by ID" argument :id, ID, required: true end # Then provide an implementation: def post(id:) Post.find(id) end end
:is_completed, String, null: false # Related Object: field :owner, Types::User, null: false # List field: field :viewers, [Types::User], null: false # Connection/for pagination-relay: field :items, Types::TodoItem.connection_type, null: false do argument :status, TodoStatus, required: false end end
such as music or spoken word" value "IMAGE", "A still image, such as a photo or graphic" value "TEXT", "Written words" value "VIDEO", "Motion picture, may have audio" end
post, errors: [], } else # Convert Rails model errors into GraphQL-ready error hashes user_errors = post.errors.map do |attribute, message| path = ["attributes", attribute.camelize] { path: path, message: message, } end { post: post, errors: user_errors, } end end
ctx, field| # Raise a graphql-friendly error with a custom message raise GraphQL::ExecutionError, "#{field.type.unwrap.graphql_name} not found" end end
query_ctx[:lazy_find_person] ||= { pending_ids: Set.new, loaded_ids: {}, } @lazy_state[:pending_ids] << person_id end def person loaded_record = @lazy_state[:loaded_ids][@person_id] if loaded_record loaded_record else pending_ids = @lazy_state[:pending_ids].to_a people = Person.where(id: pending_ids) people.each { |person| @lazy_state[:loaded_ids][person.id] = person } @lazy_state[:pending_ids].clear @lazy_state[:loaded_ids][@person_id] end end end