Last active
February 5, 2019 16:52
-
-
Save subomi/ab66e359ee9c3554728aaa3d62b67849 to your computer and use it in GitHub Desktop.
This file contains 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
# app/utils/feature_flags.rb | |
module FeatureFlags | |
module Instrumentation | |
module_function | |
DEFAULT_ERROR_MESSAGE = 'This Feature is currently unavailable, please try again later.' | |
def before_query(query) | |
# We query for the root fields alone. | |
begin | |
# We do not try to select them uniquely because queries can differ in some form of arguments passed to them. | |
root_fields = query.selected_operation.selections.map { |s| s } | |
rescue | |
root_fields = [] | |
end | |
root_fields.each do |field| | |
check_flags(field) | |
end | |
end | |
def after_query(query) | |
# ... | |
end | |
def check_flags(field) | |
case field.name | |
when "queryFoo" | |
unless Flipper["Foo"].enabled? | |
raise GraphQL::ExecutionError.new(DEFAULT_ERROR_MESSAGE) | |
end | |
# carry on with the query action. | |
# ... | |
end | |
# This is used to get the GraphQL operation name. | |
def get_operation_name(field, name) | |
operation_index = field.arguments.index { |arg| arg.name == name } | |
operation_name = field.arguments[operation_index].value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment