README.md in graphql-metrics-1.1.5 vs README.md in graphql-metrics-2.0.0
- old
+ new
@@ -51,11 +51,11 @@
implementing the methods below, as needed.
Here's an example of a simple extractor that logs out all GraphQL query details.
```ruby
-class LoggingExtractor < GraphQLMetrics::Extractor
+class LoggingExtractor < GraphQLMetrics::Instrumentation
def query_extracted(metrics, _metadata)
Rails.logger.debug({
query_string: metrics[:query_string], # "query Project { project(name: "GraphQL") { tagline } }"
operation_type: metrics[:operation_type], # "query"
operation_name: metrics[:operation_name], # "Project"
@@ -101,10 +101,11 @@
unwrapped_type_name: metrics[:unwrapped_type_name], # "PostInput"
type: metrics[:type], # "PostInput!"
default_value_type: metrics[:default_value_type], # "IMPLICIT_NULL"
provided_value: metrics[:provided_value], # false
default_used: metrics[:default_used], # false
+ used_in_operation: metrics[:used_in_operation], # true
})
end
# Define this if you want to do something with the query just before query logging.
def before_query_extracted(query, query_context)
@@ -130,9 +131,35 @@
# Use or clear state after metrics extraction, i.e. Flush metrics to Datadog, Kafka etc.
# i.e. kafka.producer.produce('graphql_metrics', @collected_metrics); kafka.producer.deliver_messages
end
end
```
+
+You can also define ad hoc query Extractors that can work with instances of GraphQL::Query, for example:
+
+```ruby
+class TypeUsageExtractor < GraphQLMetrics::Extractor
+ attr_reader :types_used
+
+ def initialize
+ @types_used = Set.new
+ end
+
+ def field_extracted(metrics, _metadata)
+ @types_used << metrics[:type_name]
+ end
+end
+
+# ...
+
+extractor = TypeUsageExtractor.new
+extractor.extract!(query)
+puts extractor.types_used
+# => ["Comment", "Post", "QueryRoot"]
+```
+
+Note that resolver-timing related data like `duration` in `query_extracted` and `resolver_times` in `field_extracted`
+won't be available when using an ad hoc Extractor, since the query isn't actually being run; it's only analyzed.
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.