lib/graphiti_graphql/runner.rb in graphiti_graphql-0.1.1 vs lib/graphiti_graphql/runner.rb in graphiti_graphql-0.1.2

- old
+ new

@@ -2,26 +2,32 @@ class Runner def execute(query_string, variables, schema) query = GraphQL::Query.new(schema, query_string, variables: variables) definition = query.document.definitions.first selection = definition.selections.first - resource_class = find_entrypoint_resource_class(selection.name) + is_graphiti = schemas.generated.query_field?(selection.name) - # TODO: instead, keep track of fields we add + # Wrap *everything* in context, in case of federated request Util.with_gql_context do - if resource_class + if is_graphiti + resource_class = schemas.generated + .resource_for_query_field(selection.name) run_query(schema, resource_class, selection, query) else - Graphiti.graphql_schema.schema.execute query_string, + schemas.graphql.execute query_string, variables: variables, - context: GraphitiGraphQL.get_context + context: GraphitiGraphQL.config.get_context end end end private + def schemas + GraphitiGraphQL.schemas + end + def run_query(schema, resource_class, selection, query) if (errors = collect_errors(schema, query)).any? {"errors" => errors.map(&:to_h)} else params = process_selection(selection, {}, query.variables.to_h) @@ -47,39 +53,23 @@ query.analysis_errors = schema.analysis_engine .analyze_query(query, query.analyzers || []) query.validation_errors + query.analysis_errors + query.context.errors end - # We can't just constantize the name from the schema - # Because classes can be reopened and modified in tests (or elsewhere, in theory) - def find_entrypoint_resource_class(entrypoint) - Graphiti.resources.find(&matches_entrypoint?(entrypoint)) - end - def find_entrypoint_schema_resource(entrypoint) - graphiti_schema.resources.find(&matches_entrypoint?(entrypoint)) + schemas.generated.schema_resource_for_query_field(entrypoint) end - def matches_entrypoint?(entrypoint) - lambda do |resource| - resource.graphql_entrypoint.to_s.underscore == entrypoint.pluralize.underscore - end - end - def introspection_query?(query) query.document.definitions.first.selections.first.name == "__schema" end def find_resource_by_selection_name(name) - graphiti_schema.resources + schemas.graphiti.resources .find { |r| r.type == name.pluralize.underscore } end - def graphiti_schema - Graphiti.graphql_schema.graphiti_schema - end - def schema_resource_for_selection(selection, parent_resource) if parent_resource parent_resource.related_resource(selection.name.underscore.to_sym) else find_entrypoint_schema_resource(selection.name) @@ -144,11 +134,11 @@ process_selection(sideload_selection, params, variables_hash, resource, chained_name) end end fragments.each do |fragment| - resource_name = Graphiti.graphql_schema.type_registry[fragment.type.name][:resource] - klass = graphiti_schema.resources.find { |r| r.name == resource_name } + resource_name = schemas.generated.type_registry[fragment.type.name][:resource] + klass = schemas.graphiti.resources.find { |r| r.name == resource_name } _, _, fragment_sideload_selections = gather_fields fragment.selections, klass, params, nil, # no chaining supported here polymorphic_parent_data: [fields, extra_fields, sideload_selections]