-
-
Notifications
You must be signed in to change notification settings - Fork 677
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
Actual serverless aws lambda support #162
Comments
Because standard solutions generate schema programmatically, using Unfortunately, duplicated types issue comes from the way how decorators are evaluated and how node's And building schema on every single lambda call is a horrible idea. It's very CPU time consuming job and has to be avoided, that's why you should cache the built schema between lambda function calls.
You can call
Is there any |
I don't think this is the case. My guess is that there are several threads running in the same environment, each importing the lambda function -- so the module level stuff is run multiple times, once for each 'server' node. The lambda function itself does not build the schema. Thanks for the other suggestions, I'll give them a try. |
The easiest way to disable "caching" of metadata from decorators is to clear |
Simply using the flag Edit: That flag adds a bit of overhead, so we might want to find a real solution for this anyway. |
I don't get it... |
I don't belive that the problem is on AWS lambdas. It's mainly on local development.
When using
The documentation on Clearing the metadata if we're on a local server (kinda) works: import { getMetadataStorage } from "type-graphql/metadata/getMetadataStorage";
if (process.env.IS_OFFLINE) {
getMetadataStorage().clear();
} Another solution might be to turn off cache invalidation on serverless-offline and use HMR or nodemon to reload the affected files. |
I will split this issue into two things. One is to provide a lambda example (covered by #52) and if it requires too much configuration, I will try to provide some helpers, maybe even as a plugin So closing this for housekeeping purposes 🔒 |
@Zn4rK Did you ever find a good solution? I'm running into the same issue with Serverless-offline+nestjs+type-graphql. Also where did you put your |
@joshstrange What I eventually settled on was the banner plugin for Webpack (I use serverless-webpack which I can highly recommend): if (slsw.lib.webpack.isLocal) {
plugins.push(
/**
* This is due to the fact the both TypeORM and TypeGraphQL is using a global variable for storage.
* This is only needed in development.
*
* When the module that's been hot reloaded is requested, the decorators are executed again, and we get
* new entries.
*
* @see https://github.com/typeorm/typeorm/blob/ba1798f29d5adca941cf9b70d8874f84efd44595/src/index.ts#L176-L180
* @see https://github.com/MichalLytek/type-graphql/blob/1eb65b44ca70df1b253e45ee6081bf5838ebba37/src/metadata/getMetadataStorage.ts#L5
*/
new webpack.BannerPlugin({
entryOnly: true,
banner: `
delete global.TypeGraphQLMetadataStorage;
delete global.typeormMetadataArgsStorage;
`,
raw: true,
}),
);
} If you choose to go the |
@Zn4rK I'm sorry to bug you again and we can (if you are ok with it) move to email if GH is not the best place for this discussion (josh at joshstrange dot com). Is there anyway I could get a little more information on how you have your project setup? Like your serverless.yml file, webpack.config.js, tsconfig.js? I know it's a huge ask and if you don't want to that is fine, I just figured I'd ask. I've been futzing with this for days now and I can't seem to get serverless-offline, serverless-webpack, nestjs, typeorm, type-graphql to all play nicely. I feel like I'm SO close as I've got as far as everything except type-graphql playing nice in offline and on AWS (unoptimized and without using webpack, using serverless-typescript). I know it's asking a lot but I really want my environment setup for local dev (that also works on lambda) so I can leverage graphql through nestjs but also have custom endpoints if I need them. I'm about to throw in the towel and drop nestjs and try for something simpler but that would be re-inventing the wheel of a bunch of stuff... Either way thank you for what you've already done trying to help me! Edit: If you aren't using NestJS or you are using some other framework I would love to know even that much so I could attempt to go down that path. |
I don't think you need anything from this library. For local testing, use normal apollo (don't use serverless-offline), for lambda use apollo-lambda. To achieve this, use a different entry point for local or an environment variable. This works on AWS lambda as today:
|
I've been struggling with serverless-offline for the past couple of hours. Should we give up on it? |
@carlosdubus can you share your webpack.config.js? I'm trying to deploy in AWS my Graphql API using serverless, apollo-server-lambda, type-graphql but when I try to use the playground I got the error "message": "Internal server error". |
@MichalLytek , Do you have an example with Apollo server 4 and type-graphql@next ? |
Getting this going on serverless has been a time sink, it would be nice if this worked 'out-of-the-box'. There are two main issues I've encountered, the first leading to the second.
These issues stem from however the types are being registered in graphql, and these symptoms don't appear when using standard techniques, but only when introducing type-graphql.
As described here (Using this in a serverless environment (e.g. apollo-lambda-server) #96) you can only run
buildSchema
once. Thus the solution is to set it to a global and check that a schema exists or not before building it.This now breaks using
serverless-offline
when one makes changes to their schema, offline will re-compile and reload the typescript, but because of the workaround in point 1 above, the schema will not be re-built. This requires one to manually kill the offline process and restart it.Now, I don't know at this point how the internals work, but my instinct is that there should be a means of re-compiling the schema without killing the process, by clearing out any types wherever they are cached.
It seems wrong that two different schemas should be sharing definitions anyway. Why are types globally cached? Why does this symptom not occur when using standard graphql setups?
Possible workarounds not requiring changes to this project:
These solutions are not ideal.
The text was updated successfully, but these errors were encountered: