graphql-query-resolver icon indicating copy to clipboard operation
graphql-query-resolver copied to clipboard

Add support for inline and named fragments

Open khamusa opened this issue 8 years ago • 8 comments

Add support for inline fragments and named fragments, supporting queries such as:

fragment ChefFragment on chef {
  name
  recipes {
    title
    ingredients {
      name, quantity
      vendor {
        name
      }
    }
  }
}

query {
  restaurant(id: 1) {
    ... on restaurant {
      name
      owner {
        ... ChefFragment
      }
    }
  }
}

I had to pass a reference to the hash that contains the fragment definitions through the whole method chain, since it is only available from the context variable. I'd suggest we refactor a module and use an instance of a class to hold current state in order to avoid passing so many parameters on method calls. something like:

module GraphQL
  module QueryResolver
    def self.run(*args, &block)
      Resolver.new(*args).call(&block)
    end

    class Resolver
      attr_reader :model_class, :context, :return_type
      def initialize(model_class, context, return_type)
        # save values
      end

      def call
        to_load = yield
        # and so on...
      end     
  end
end

If you like the idea I can perform the refactoring as well

khamusa avatar Sep 08 '17 23:09 khamusa

That actually sounds really great, @khamusa. Let's try the refactoring idea :)

nettofarah avatar Sep 08 '17 23:09 nettofarah

@nettofarah let me know what you think!

khamusa avatar Sep 10 '17 22:09 khamusa

@nettofarah thanks for the comments! Take a look at the last ~two~ three commits.

Regarding the dig_deeper (now to_proceed), I'm not 100% satisfied either. I can think of alternatives for refactoring but they involve switching to a more OO style which I'd rather not do since this code runs for every field and creating more objects could potentially impact performance.

khamusa avatar Sep 11 '17 16:09 khamusa

By the way, I don't know how you prefer to manage versions, but I also added these latest changes to the changelog, so that we don't forget what's to be added later. If you prefer I can remove it as well.

khamusa avatar Sep 11 '17 16:09 khamusa

@nettofarah please hold this, i'm investigating an issue with relay pagination

khamusa avatar Sep 11 '17 20:09 khamusa

@khamusa sounds good! Take your time

nettofarah avatar Sep 15 '17 01:09 nettofarah

Hey, @khamusa. Did you learn anything new here?

nettofarah avatar Nov 23 '17 02:11 nettofarah

hey @nettofarah I'm sorry I haven't been able to work on this anymore. The issue I found was actually this one: https://github.com/nettofarah/graphql-query-resolver/issues/9

I found out that the strategy we're using here was pretty much useless for relay connections.

I do think this PR is unrelated though..

khamusa avatar Nov 23 '17 13:11 khamusa