Merge pull request #6601 from tchak/feat-graphql-logs

feat(graphql): add graphql_operation to rails logs
This commit is contained in:
Paul Chavard 2021-11-02 12:15:09 +01:00 committed by GitHub
commit e404529d37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,6 +18,47 @@ class API::V2::GraphqlController < API::V2::BaseController
private private
def append_info_to_payload(payload)
super
payload.merge!({
graphql_operation: operation_log(params[:query], params[:operationName], params[:variables])
})
end
def operation_log(query, operation_name, variables)
return "NoQuery" if query.nil?
operation = GraphQL.parse(query).children.find do |node|
if node.is_a?(GraphQL::Language::Nodes::OperationDefinition)
node.name == operation_name
end
end
return "InvalidQuery" if operation.nil?
return "IntrospectionQuery" if operation.name == "IntrospectionQuery"
message = operation.operation_type
if operation.name
message += ": #{operation.name} { "
end
message += operation.selections.map(&:name).join(', ')
message += " }"
if variables.present?
message += " "
message += variables.to_unsafe_h.flat_map do |(name, value)|
if name == "input"
value.map do |(name, value)|
"#{name}: \"#{value.to_s.truncate(10)}\""
end
else
"#{name}: \"#{value.to_s.truncate(10)}\""
end
end.join(', ')
end
message
end
def process_action(*args) def process_action(*args)
super super
rescue ActionDispatch::Http::Parameters::ParseError => exception rescue ActionDispatch::Http::Parameters::ParseError => exception