Graphql-ruby: Question: Sub folders inside of folders in graphql/

Created on 26 Jan 2018  路  8Comments  路  Source: rmosolgo/graphql-ruby

How can I create subfolder inside of resolvers / or inside types / or in mutations / ?

I want create sub folder in resolvers/ like:

resolvers/create
resolvers/update
resolvers/delete
resolvers/query

But the file mutation_type.rb dont load the resolvers that are inside the sub folders in resolvers/

Most helpful comment

That's correct, only one type with a given name. By default, GraphQL-Ruby infers a GraphQL name from the Ruby class name. But you can provide one explicitly with graphql_name:

class Types::Admin::User 
  graphql_name "AdminUser"
end 

and it will use the provided value instead of the inferred one (eg, AdminUser instead of User in the above case).

Also, you can implement default_graphql_name in your base class to provide custom logic for inferring the GraphQL name, eg

class Types::BaseObject 
  def self.default_graphql_name 
    @default_graphql_name ||= self.name.split("::")[1..-1].join 
  end 
end 

(Just an arbitrary example of deriving a GraphQL name from a class name.) You can see the default implementation here: https://github.com/rmosolgo/graphql-ruby/blob/b0e97e759c850cfbc03d75dfbb115f1bc443a985/lib/graphql/schema/member/base_dsl_methods.rb#L86-L95

Hope that helps!

All 8 comments

Hi! It sounds like you're using Rails, is that right?

Rails has some strong conventions about how filepaths should match the names of Ruby constants which are defined by those files. Maybe the Rails guide will offer some insight! http://guides.rubyonrails.org/autoloading_and_reloading_constants.html

Any solutions for this on Rails?

Thanks!

@tpkonrad, can you give an example of what you're trying to do and what your expectation is? Using sub-directories typically involves namespacing using modules.

@TomasBarry, actually, It seems to be working now. Maybe i forgot to restart Rails the last time I tried. Thank you anyway!
graphql1
graphql2

Unfortunately, It seems that we cannot reuse GraphQL type names (even if they are in different modules) :(
Any clues on this?
Thanks
graphql3
graphql4

@tpkonrad, I have actually just come across this duplicate name error with a colleague. @rmosolgo may be able to confirm that it is not possible to have two types with the same name even if they are in distinct namespaces.

I suspect this is an actual GraphQL limitation rather than a graphql-ruby limitation?

That's correct, only one type with a given name. By default, GraphQL-Ruby infers a GraphQL name from the Ruby class name. But you can provide one explicitly with graphql_name:

class Types::Admin::User 
  graphql_name "AdminUser"
end 

and it will use the provided value instead of the inferred one (eg, AdminUser instead of User in the above case).

Also, you can implement default_graphql_name in your base class to provide custom logic for inferring the GraphQL name, eg

class Types::BaseObject 
  def self.default_graphql_name 
    @default_graphql_name ||= self.name.split("::")[1..-1].join 
  end 
end 

(Just an arbitrary example of deriving a GraphQL name from a class name.) You can see the default implementation here: https://github.com/rmosolgo/graphql-ruby/blob/b0e97e759c850cfbc03d75dfbb115f1bc443a985/lib/graphql/schema/member/base_dsl_methods.rb#L86-L95

Hope that helps!

Many thanks @rmosolgo, this solved my problem! :)

Was this page helpful?
0 / 5 - 0 ratings