Last active
January 5, 2018 17:40
-
-
Save phaedryx/892da0452b3529221ef23588726ffa00 to your computer and use it in GitHub Desktop.
create function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class CreateFunction < GraphQL::Function | |
attr_reader :type | |
def initialize(model) | |
@model = model | |
@type = ModelType.new(model) # creates a graphql type from a model | |
end | |
def arguments | |
fields = @type.all_fields.select do |field| | |
# some logic for which fields we want to expose | |
end | |
args = fields.map { |field| [field.name, field.type] } | |
args.map { |a| GraphQL::Argument.from_dsl(*a) }.mash { |a| [a.name, a] } | |
end | |
def call(_obj, args, ctx) | |
# parse and check the args | |
# check ctx for some authorization stuff | |
@model.create(args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment