Skip to content
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

info.cacheControl.cacheHint can only be set in globalMddleware, not resolver-based middleware. #199

Closed
laukaichung opened this issue Nov 18, 2018 · 5 comments
Labels
Invalid 👎 This doesn't seem right

Comments

@laukaichung
Copy link

laukaichung commented Nov 18, 2018

Describe the bug
This CacheControl middleware from #75 no longer works when I use it in individual resolvers:

import {MiddlewareFn} from "type-graphql";

export function CacheControl(maxAge:number = 60): MiddlewareFn {
    return ({info:{cacheControl}}, next) => {
        cacheControl.setCacheHint({maxAge})
        return next();

    };
}

The only working way is to call info.setCacheHint in global middlewares, which is not very ideal :

@Service()
export class ErrorLoggerMiddleware implements MiddlewareInterface<GqlContext> {
    async use({ context, info }: ResolverData<GqlContext>, next: NextFn) {
        cacheControl.setCacheHint({maxAge:60})
        ........
    }
}


let schema = await buildSchema({
        resolvers,
        validate: true,
        dateScalarMode: "isoDate",
        globalMiddlewares: [ErrorLoggerMiddleware],
});


I guess the info object from global middlewares is overriding the one from the resolver-based middlewares?

Enviorment (please complete the following information):

  • OS: Arch Linux
  • Node: 10.7.0
  • Package version : 0.15.0
  • TypeScript version : 3.1.6
@MichalLytek
Copy link
Owner

I guess the info object from global middlewares is overriding the one from the resolver-based middlewares?

There's no overwriting info object in resolvers unless you do it by yourself.
Global middlewares are called before the resolver's ones so your resolver's hints will take precedence.

This CacheControl middleware from #75 no longer works when I use it in individual resolvers:

Cannot reproduce with [email protected] and [email protected] - the example works without any problems with resolver-based middleware.

Please create a repository with a minimal reproducible code example.

@MichalLytek MichalLytek added the Need More Info 🤷‍♂️ Further information is requested label Nov 18, 2018
@laukaichung
Copy link
Author

laukaichung commented Nov 18, 2018

@19majkel94
I figured it out.

Here's the repository:
https://github.com/stonecold123/typegraphql-test

index.ts

    // Uncomment this if you want to start the server with apollo-engine
    // const engine = new ApolloEngine({
    //     apiKey: "YOUR_ID"
    // });
    //
    // const httpServer = graphQLServer.createHttpServer(serverOptions);
    //
    // engine.listen(
    //     {
    //         port,
    //         httpServer,
    //         graphqlPaths: [endpoint],
    //
    //     },
    //     () =>
    //         console.log(
    //             `Server with Apollo Engine is running on http://localhost:3012`,
    //         ),
    // )

    // Comment this if you want to start the server with apollo-engine
    graphQLServer.start(serverOptions, ({ port, playground }) => {
        console.log(`graphQLServer`, `Server is running, GraphQL Playground available at http://localhost:${port}${playground}`,);
    });

I use a dockerized apollo-engine instead of creating an instance in node.js because only the former is compatible with pm2 cluster mode. The apollo server must have detected the absence of an apollo-engine instance in js and refuse to add cacheControl to the response header.

It used to add the header regardless of whether the apollo-engine was created in node.js.
Is this library responsible for adding the cacheControl header to the response header?

@MichalLytek
Copy link
Owner

So basically it's not a fault on TypeGraphQL side - please open a proper issue in apollo server repository.

When #77 is implemented, you would be able to declare the cache metadata using directives.
For now I can't do anything as #77 it's blocked by graphql-js.

@MichalLytek MichalLytek added Invalid 👎 This doesn't seem right and removed Need More Info 🤷‍♂️ Further information is requested labels Nov 18, 2018
@laukaichung
Copy link
Author

In my project, CacheController() decorator has to be used in all of the @FieldResolver() if the query needs to access those fields. You cannot just add the CacheController to the main query resolver and expect the Cache-Controller header to be added to the response header. I have no idea why my schema needs such special treatment.

@clayrisser
Copy link

@laukaichung do you have an example of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid 👎 This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants