-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4674 from rmosolgo/query-logger
Add Query#logger
- Loading branch information
Showing
7 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
layout: guide | ||
doc_stub: false | ||
search: true | ||
section: Queries | ||
title: Logging | ||
desc: Development output from GraphQL-Ruby | ||
index: 12 | ||
--- | ||
|
||
At runtime, GraphQL-Ruby will output debug information using {{ "GraphQL::Query.logger" | api_doc }}. By default, this uses `Rails.logger`. To see output, make sure `config.log_level = :debug` is set. (This information isn't meant for production logs.) | ||
|
||
You can configure a custom logger with {{ "GraphQL::Schema.default_logger" | api_doc }}, for example: | ||
|
||
```ruby | ||
class MySchema < GraphQL::Schema | ||
# This logger will be used by queries during execution: | ||
default_logger MyCustomLogger.new | ||
end | ||
``` | ||
|
||
You can also pass `context[:logger]` to provide a logger during execution. |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
require "spec_helper" | ||
|
||
describe "Logger" do | ||
describe "Schema.default_logger" do | ||
if defined?(Rails) | ||
it "When Rails is present, returns the Rails logger" do | ||
assert_equal Rails.logger, GraphQL::Schema.default_logger | ||
end | ||
else | ||
it "Without Rails, returns a new logger" do | ||
assert_instance_of Logger, GraphQL::Schema.default_logger | ||
end | ||
end | ||
|
||
it "can be overridden" do | ||
new_logger = Logger.new($stdout) | ||
schema = Class.new(GraphQL::Schema) do | ||
default_logger(new_logger) | ||
end | ||
assert_equal new_logger, schema.default_logger | ||
end | ||
|
||
it "can be set to a null logger with nil" do | ||
schema = Class.new(GraphQL::Schema) | ||
schema.default_logger(nil) | ||
nil_logger = schema.default_logger | ||
std_out, std_err = capture_io do | ||
nil_logger.error("Blah") | ||
nil_logger.warn("Something") | ||
nil_logger.error("Hi") | ||
end | ||
assert_equal "", std_out | ||
assert_equal "", std_err | ||
end | ||
end | ||
|
||
describe "during execution" do | ||
module LoggerTest | ||
class DefaultLoggerSchema < GraphQL::Schema | ||
module Node | ||
include GraphQL::Schema::Interface | ||
field :id, ID | ||
end | ||
|
||
class Query < GraphQL::Schema::Object | ||
field :node, Node do | ||
argument :id, ID | ||
end | ||
|
||
def node(id:) | ||
|
||
end | ||
end | ||
query(Query) | ||
end | ||
|
||
class CustomLoggerSchema < DefaultLoggerSchema | ||
LOG_STRING = StringIO.new | ||
LOGGER = Logger.new(LOG_STRING) | ||
LOGGER.level = :debug | ||
default_logger(LOGGER) | ||
end | ||
end | ||
|
||
before do | ||
LoggerTest::CustomLoggerSchema::LOG_STRING.truncate(0) | ||
end | ||
|
||
it "logs about hidden interfaces with no implementations" do | ||
res = LoggerTest::CustomLoggerSchema.execute("{ node(id: \"5\") { id } }") | ||
assert_equal ["Field 'node' doesn't exist on type 'Query'"], res["errors"].map { |err| err["message"] } | ||
assert_includes LoggerTest::CustomLoggerSchema::LOG_STRING.string, "Interface `Node` hidden because it has no visible implementors" | ||
end | ||
|
||
it "doesn't print messages by default" do | ||
res = nil | ||
stdout, stderr = capture_io do | ||
res = LoggerTest::DefaultLoggerSchema.execute("{ node(id: \"5\") { id } }") | ||
end | ||
assert_equal ["Field 'node' doesn't exist on type 'Query'"], res["errors"].map { |err| err["message"] } | ||
assert_equal "", stdout | ||
assert_equal "", stderr | ||
end | ||
end | ||
end |