-
Notifications
You must be signed in to change notification settings - Fork 114
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
[Question] Using libraries such as join-monster, which rely on mutating schema instance #198
Comments
We can add a field for this kind of middleware tools.
|
Great, that would do the trick! |
After
Let me know if that worked for you. |
Thanks! I tried that, but it didn't seem to work. As a test, I tried: new GraphQLModule({
// ...
middleware({ schema }) {
schema['__DIRTY__'] = true;
return { schema };
}
}) I see that the However, when inspecting |
Hello! Thanks for putting the test together. I've investigated further and discovered that the problem seems to occur when adding middleware to a module that is being imported by another module. In my case, I have an entry export const AppModule = new GraphQLModule<AppModuleConfig, {}, AppModuleContext>({
imports: () => [
AuthModule,
],
}); and I am attempting to add the middleware to export const AuthModule = new GraphQLModule<{}, AuthModuleRequest, AuthModuleContext>({
typeDefs, // imported above
resolvers: {
Query: {
me: (_root, _args, _context, info) => {
console.log('resolver', info.schema);
}
}
},
middleware: ({ schema }) => {
schema['__DIRTY__'] = true;
return { schema };
},
}); If |
Could you try the following versions?
|
Great, that seems to have fixed it. Many thanks! 🎉 |
Great!!! So, we can close this issue then :) |
I just tested this today and it does not seem to work. I tested versions 0.4.2 and 0.5.0-alpha.e4344183. The modified schema only seems to get through when the middleware is added to the root module (so changing the root module or moving the middleware to the root module). I'm using the exact sample supplied above by @ghost so I'm not certain where to go from here @ardatan? |
#335 We are planning to change our merge logic, so you won't lose your modifications on child modules if you use middlewares.
|
I just tested this and it does not seem to work. The schema only gets modified when applying the middleware on the root module. |
@BeeHiveJava How do you test it? |
Root module: export const AppModule = new GraphQLModule({
imports: [
UserModule
]
}); Child module: export const UserModule = new GraphQLModule({
typeDefs: definitions,
resolvers: resolvers,
middleware: ({schema}: any) => {
schema["__DIRTY__"] = true;
return {schema};
}
}); This does not seem to modify the final schema for me. But again, when I place the middleware in the root module it does work: export const AppModule = new GraphQLModule({
middleware: ({schema}: any) => {
schema["__DIRTY__"] = true;
return {schema};
},
imports: [
UserModule
]
}); |
@BeeHiveJava |
I guess that the biggest use case would be libraries such as join-monster. I think we can get these libraries to work by modifying the generated schema with custom properties afterwards, but it would be real nice if we could add custom properties to the schema from a module. In addition I think that this is also solvable by implementing some custom directives and a middleware that parses them on the root module? That might even be a more elegant approach than monkey-patching the schema. It seems like this is going to be supported natively: graphql/graphql-js#1527 |
So, the point is to get that field inside the resolver right?
I just added a test for that case; |
You're right, I missed that point! I'll do some testing and I'll let you know if I get my use case for GraphQL-shield and join-monster to work. |
I tested and debugged some and your provided sample seems to work, the new properties are present on the schema @ardatan. I couldn't, however, get join-monster to work, but it seems like the issue I'm having now is irrelevant. Thanks for all the support thus far, fantastic library . 👍 |
@BeeHiveJava Let's keep this open until your issue is solved. |
We are using different approach for merging schemas, so I think your issue is solved in the latest version. Feel free to open this issue again if your problem persists. |
Hi again.
I've recently discovered a few GraphQL libraries that work by decorating / mutating the schema (
GraphQLSchema
) instance, and depend on those changes being persisted and not overwritten, such that those decorations can be accessed in resolvers with theresolverInfo
argument (fourth argument). For example, join-monster requires adding custom properties toGraphQLObjectType
constructors in order to be configured. When using Apollo, this can be achieved using an adapter, such as join-monster-graphql-tools-adapter, which works by decorating / mutating the schema based on a custom object you pass to it.As far as I can tell, I cannot rely on mutating the schema instance in such a way when using GraphQL Modules, as the schema are built using a factory method, which is called at strategic points in the control flow.
This has lead me to abandon the idea of being able to use such libraries with GraphQL Modules, viewing them as being idiomatically incompatible with one another. Is this an accurate conclusion, or have I missed something? Perhaps the solution could be as simple as formalising an interface for mutating / transforming a schema at a strategic point before the resolvers get called?
Cheers.
The text was updated successfully, but these errors were encountered: