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
Modular types and incremental imports #340
Comments
Hmm, right now you would have to do: // PostType.js
import Comment from './CommentType';
import { merge } from 'lodash';
export default {
typeDefs: [`
type Post {
id: Int
comments: [Comment]
}
`, ...Comment.typeDefs],
resolvers: merge({
Post: {
comments: (post) => post.getCommentsFromDB()
}
}, Comment.resolvers)
}; import { makeExecutableSchema } from 'graphql-tools';
import { merge, uniq } from 'lodash';
import PostType from './PostType.js';
const typeDefs = [`
type Query {
getPost(id: Int!): Post
}
`, ...PostType.typeDefs];
const resolvers = merge({
Query: {
getPost: (root, { id }) => getPostFromDB(id),
}
}, PostType.resolvers);
export default makeExecutableSchema({
typeDefs: uniq(typeDefs), // Need the uniq to avoid having the same string twice
resolvers
}); I can see where you're coming from that it could be easier. I think something which makes it more natural to pass around a schema/resolver pair and merge them together would be a good fit inside this package, and would probably be just a few lines of code! Would love to talk about a design for that. |
One point here is that I think I just have a bit of a different mental model - in my mind I feel like all of the types to be kind of part of a whole that are all merged together, rather than small modules with interdependencies. But we should support both ways of doing stuff, it's just a matter of having a nice unit to pass around/import. |
Right. I agree with your point about the different mental models. I think of types as an interconnected graph. And to define a schema, all we need to do is point to the
So yeah, each type in my mind is a standalone node in the graph. |
Sweet diagram! But yes I'm definitely into having a nicer API for that model. |
@stubailo I'm trying to come up with a pattern for exporting types as a unit. One thing that I couldn't figure out is how to do the equivalent of |
Any progress on this? Seems like still a great idea! |
I think we should address this as part of a way to make things more modular/incremental, and I'm planning to use #185 as the main issue for that topic. Closing this but that doesn't mean it's not important! |
Follow up on: https://twitter.com/mdebbar/status/869249679862546432
I'll try to illustrate the example from Twitter with more details.
Let's say we have a simple schema for only posts and comments. The QueryType has one field
getPost
. And thePostType
has a fieldcomments
.When using
graphql-js
, the top-levelschema
file doesn't have to import everything. It only imports the types necessary for the top-level query fields, and everything else will be included through incremental imports:I like the approach of
graphql-tools
which looks cleaner and less verbose, but I couldn't find a way to achieve a structure similar to the above.cc @stubailo
The text was updated successfully, but these errors were encountered: