From c7cef378cde2db5c777a4271e92fe7233991c021 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 5 Oct 2019 08:49:01 +0200 Subject: [PATCH] added graphql tracing --- Gemfile | 1 - app/controllers/graphql_controller.rb | 5 +-- app/graphql/lupo_schema.rb | 2 +- .../types/funder_connection_with_meta_type.rb | 33 +++++++-------- app/graphql/types/funder_type.rb | 7 +--- .../types/person_connection_with_meta_type.rb | 40 +++++++++---------- app/graphql/types/person_type.rb | 15 +------ app/graphql/types/query_type.rb | 20 ---------- 8 files changed, 42 insertions(+), 81 deletions(-) diff --git a/Gemfile b/Gemfile index 30bcae33b..024ecbf23 100644 --- a/Gemfile +++ b/Gemfile @@ -64,7 +64,6 @@ gem 'batch-loader', '~> 1.4', '>= 1.4.1' gem 'graphql-cache', '~> 0.6.0', git: "https://github.com/stackshareio/graphql-cache" gem 'apollo-federation', '~> 0.4.0' gem 'google-protobuf', '3.10.0.rc.1' -# gem 'ddtrace', '~> 0.23.3' group :development, :test do gem 'rspec-rails', '~> 3.8', '>= 3.8.2' diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index 9a4deb97a..87fbc87be 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -4,11 +4,10 @@ def execute query = params[:query] operation_name = params[:operationName] context = { - # Query context goes here, for example: - # current_user: current_user, + tracing_enabled: ApolloFederation::Tracing.should_add_traces(headers) } result = LupoSchema.execute(query, variables: variables, context: context, operation_name: operation_name) - render json: MultiJson.dump(result) + render json: ApolloFederation::Tracing.attach_trace_to_result(result) rescue => e raise e unless Rails.env.development? handle_error_in_development e diff --git a/app/graphql/lupo_schema.rb b/app/graphql/lupo_schema.rb index f722cd09d..6c0e62ebb 100644 --- a/app/graphql/lupo_schema.rb +++ b/app/graphql/lupo_schema.rb @@ -3,7 +3,7 @@ class LupoSchema < GraphQL::Schema include ApolloFederation::Schema - # use(GraphQL::Tracing::DataDogTracing, analytics_enabled: Rails.env.production?) + use ApolloFederation::Tracing default_max_page_size 250 max_depth 10 diff --git a/app/graphql/types/funder_connection_with_meta_type.rb b/app/graphql/types/funder_connection_with_meta_type.rb index 23e261d1d..fd44bb53b 100644 --- a/app/graphql/types/funder_connection_with_meta_type.rb +++ b/app/graphql/types/funder_connection_with_meta_type.rb @@ -1,22 +1,23 @@ # frozen_string_literal: true -class FunderConnectionWithMetaType < BaseConnection - edge_type(FunderEdgeType) - field_class GraphQL::Cache::Field +# class FunderConnectionWithMetaType < BaseConnection - field :publication_connection_count, Integer, null: false, cache: true - field :dataset_connection_count, Integer, null: false, cache: true - field :software_connection_count, Integer, null: false, cache: true +# edge_type(FunderEdgeType) +# field_class GraphQL::Cache::Field + +# field :publication_connection_count, Integer, null: false, cache: true +# field :dataset_connection_count, Integer, null: false, cache: true +# field :software_connection_count, Integer, null: false, cache: true - def publication_connection_count - Event.query(nil, citation_type: "Funder-ScholarlyArticle").results.total - end +# def publication_connection_count +# Event.query(nil, citation_type: "Funder-ScholarlyArticle").results.total +# end - def dataset_connection_count - Event.query(nil, citation_type: "Dataset-Funder").results.total - end +# def dataset_connection_count +# Event.query(nil, citation_type: "Dataset-Funder").results.total +# end - def software_connection_count - Event.query(nil, citation_type: "Funder-SoftwareSourceCode").results.total - end -end +# def software_connection_count +# Event.query(nil, citation_type: "Funder-SoftwareSourceCode").results.total +# end +# end diff --git a/app/graphql/types/funder_type.rb b/app/graphql/types/funder_type.rb index b860e278f..31a3afa7f 100644 --- a/app/graphql/types/funder_type.rb +++ b/app/graphql/types/funder_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class FunderType < BaseObject - description "Information about funders" + extend_type field :datasets, FunderDatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do argument :first, Int, required: false, default_value: 25 @@ -16,11 +16,6 @@ class FunderType < BaseObject argument :first, Int, required: false, default_value: 25 end - def address - { "type" => "postalAddress", - "address_country" => object.country.to_h.fetch("name", nil) } - end - def datasets(**args) ids = Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Funder").results.to_a.map do |e| doi_from_url(e.subj_id) diff --git a/app/graphql/types/person_connection_with_meta_type.rb b/app/graphql/types/person_connection_with_meta_type.rb index c6ad02fe6..a98958747 100644 --- a/app/graphql/types/person_connection_with_meta_type.rb +++ b/app/graphql/types/person_connection_with_meta_type.rb @@ -1,27 +1,27 @@ # frozen_string_literal: true -class PersonConnectionWithMetaType < BaseConnection - edge_type(PersonEdgeType) - field_class GraphQL::Cache::Field +# class PersonConnectionWithMetaType < BaseConnection +# edge_type(PersonEdgeType) +# field_class GraphQL::Cache::Field - field :publication_connection_count, Integer, null: false, cache: true - field :dataset_connection_count, Integer, null: false, cache: true - field :software_connection_count, Integer, null: false, cache: true - field :organization_connection_count, Integer, null: false, cache: true +# field :publication_connection_count, Integer, null: false, cache: true +# field :dataset_connection_count, Integer, null: false, cache: true +# field :software_connection_count, Integer, null: false, cache: true +# field :organization_connection_count, Integer, null: false, cache: true - def publication_connection_count - Event.query(nil, citation_type: "Person-ScholarlyArticle").results.total - end +# def publication_connection_count +# Event.query(nil, citation_type: "Person-ScholarlyArticle").results.total +# end - def dataset_connection_count - Event.query(nil, citation_type: "Dataset-Person").results.total - end +# def dataset_connection_count +# Event.query(nil, citation_type: "Dataset-Person").results.total +# end - def software_connection_count - Event.query(nil, citation_type: "Person-SoftwareSourceCode").results.total - end +# def software_connection_count +# Event.query(nil, citation_type: "Person-SoftwareSourceCode").results.total +# end - def organization_connection_count - Event.query(nil, citation_type: "Organization-Person").results.total - end -end \ No newline at end of file +# def organization_connection_count +# Event.query(nil, citation_type: "Organization-Person").results.total +# end +# end \ No newline at end of file diff --git a/app/graphql/types/person_type.rb b/app/graphql/types/person_type.rb index adad2b935..34e55ae53 100644 --- a/app/graphql/types/person_type.rb +++ b/app/graphql/types/person_type.rb @@ -1,12 +1,7 @@ # frozen_string_literal: true class PersonType < BaseObject - description "A person." - - field :id, ID, null: true, description: "The ORCID ID of the person." - field :name, String, null: true, description: "The name of the person." - field :given_name, String, null: true, hash_key: "given_names", description: "Given name. In the U.S., the first name of a Person." - field :family_name, String, null: true, description: "Family name. In the U.S., the last name of an Person." + extend_type field :datasets, PersonDatasetConnectionWithMetaType, null: true, description: "Authored datasets", connection: true, max_page_size: 100 do argument :first, Int, required: false, default_value: 25 @@ -20,14 +15,6 @@ class PersonType < BaseObject argument :first, Int, required: false, default_value: 25 end - def id - object.uid ? "https://orcid.org/#{object.uid}" : object.id - end - - def name - object.name - end - def datasets(**args) ids = Event.query(nil, obj_id: https_to_http(object.uid || object.id), citation_type: "Dataset-Person").results.to_a.map do |e| doi_from_url(e.subj_id) diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 3db28f2a3..8c2205522 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -61,26 +61,6 @@ def prefix(id:) Prefix.where(prefix: id).first end - field :funders, FunderConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do - argument :query, String, required: false - argument :first, Int, required: false, default_value: 25 - end - - def funders(query: nil, first: nil) - Funder.query(query, limit: first).fetch(:data, []) - end - - field :funder, FunderType, null: false do - argument :id, ID, required: true - end - - def funder(id:) - result = Funder.find_by_id(id).fetch(:data, []).first - fail ActiveRecord::RecordNotFound if result.nil? - - result - end - field :organizations, OrganizationConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do argument :query, String, required: false argument :first, Int, required: false, default_value: 25