Skip to content

Instantly share code, notes, and snippets.

@phaedryx
Last active January 5, 2018 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaedryx/892da0452b3529221ef23588726ffa00 to your computer and use it in GitHub Desktop.
Save phaedryx/892da0452b3529221ef23588726ffa00 to your computer and use it in GitHub Desktop.
create function
# 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