mentorenwahl/docker/api/src/api/schema/helpers.cr
Dominic Grimm a9fc6a43b1
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Renamed backend to api
2022-01-20 21:17:24 +01:00

66 lines
1.3 KiB
Crystal

require "graphql"
module API
module Schema
module Helpers
module ObjectMacros
macro field(type)
property {{ type.var }} {% if type.value %} = {{ type.value }}{% end %}
@[GraphQL::Field]
def {{ type.var }} : {{ type.type }}
@{{ type.var }}
end
end
end
module ObjectDbInit
macro db_init(type)
def initialize(obj : {{ type }})
initialize(obj.id.not_nil!)
end
end
end
module ObjectFinders
macro finders(type)
def find : {{ type }}?
{{ type }}.find(@id)
end
def find! : {{ type }}
obj = find
raise "#{{{ type }}} not found" unless obj
obj
end
end
end
module DbObject
macro db_object(type)
include ::API::Schema::Helpers::ObjectDbInit
include ::API::Schema::Helpers::ObjectFinders
db_init {{ type }}
finders {{ type }}
property id
def initialize(@id : Int32)
end
def initialize(obj : {{ type }})
@id = obj.id.not_nil!.to_i
end
@[GraphQL::Field]
def id : Int32
@id
end
end
end
end
end
end