-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Use the types from the superclass #5383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Use the types from the superclass #5383
Conversation
So if it's not needed you don't have to define the query type again.
Wow, this is an interesting idea ... basically Depending on your use case, you could do it by going to and from a String, for example: orig_schema_string = OriginalSchema.to_definition
extra_schema_types = <<<~GRAPHQL
type AdditionalType {
# ...
}
GRAPHQL
new_schema_string = orig_schema_string + extra_schema_types
NewSchema = GraphQL::Schema.from_definition(new_schema_string) That would work for schema structure, but if you need Ruby-land data (like resolve methods), it won't work. Could you tell me something about your use case? I bet we can add support for it if it isn't already possible. |
Exactly, dup but adding extra types is the perfect summary :D Fiddling around I got to something like you proposed but I indeed got to something like the structure, but not so much the ruby functionality. My usecase is a bit exotic, but let me explain 😄: We use GraphQL internally for document creation. So you use GraphQL to query the data, which is later on exposed to a template. These templates are made by content experts, they can easily edit the GraphQL queries and the template files, and never have to touch ruby. Sometimes though, these templates require data that's not available in our models. It's data that is specific to that document template. What I would want to do is allow these content experts to define an addition to the schema, which would automatically resolve to some kind of JSON storage in the back. With the huge advantage that everything is GraphQL typed etc. We would use introspection on the schema extension to build some kind of form, where the data can be entered, and then the GraphQL query can fetch the data based on that specific scheme. (so the resolver would be something like a simple JSON/hash resolver, but could also refer others). Because I don't want templates to collide with their schema's and types, I want to make separate schema's for the different templates that extend the schema, hence this request 😄. I hope this makes a bit of sense. Thanks at least for entertaining this idea! Happy to implement it if you give me some pointers what you would consider the proper way to do it. |
Let me know if you have any guidance on a PR you would be willing to accept (because clearly my proposed change makes tests fail :D) |
Yes, this definitely makes sense. Out of curiousity, would you be creating new Another question: how would Another approach that could be made to work would be just copying over As far as final result goes, I think the best API would be to add something like graphql-ruby/lib/graphql/language/parser.rb Line 200 in 0e092de
Let me know how you see these new types being integrated with your schema -- maybe providing real support for new_schema = Class.new(OriginalSchema) do
query(OriginalSchema.query)
mutation(OriginalSchema.mutation)
orphan_types(OriginalSchema.orphan_types)
end
new_schema.extend_from_definition(extra_types_string) |
I was thinking of making those on the fly, but in my current configuration I could also create the classes on startup. I need the schema in two moments:
FYI, currently (with the original change that copies the types def self.schema_with_addition(additional_schema_path)
GraphQL::Schema::BuildFromDefinition.from_document(self, GraphQL.parse_file(additional_schema_path), default_resolve: nil)
end So it looks like the With respect to extend, that does the job for my usecase. I have a extend type Params {
start_date: ISO8601Date!
signing_date: ISO8601Date!
} Executing my current code (as defined above), exposes the proper fields and in my statically defined |
So if it's not needed you don't have to define the query type again.
First of all, I have no clue if what I'm doing here makes sense or not, but let me explain what I was after:
When creating a schema from a definition, I got into the case where I wanted to add a set of types to an existing schema, but I wanted to use the GraphQL IDL to do so (see below).
Originally I thought that I could do that by passing the
OriginalSchema.types
tobase types
, but that's not how these are passed.I also wanted to see if I can pass these types through extra_types, but I don't know how I get the types in a proper form from the
graphql_string
.