-
Notifications
You must be signed in to change notification settings - Fork 2k
Remove access to process.env from hot code paths #5657
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
Changes from 6 commits
0d7113c
8e1d2cc
61be692
34acf3f
15f7b30
e8b1f0d
ec9ea61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ describe('runHttpQuery', () => { | |
| query: '{ testString }', | ||
| }, | ||
| options: { | ||
| debug: false, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was required to avoid returning a stack trace, which the test didn't expect. Without running through ApolloServer first, the |
||
| schema, | ||
| schemaHash: generateSchemaHash(schema), | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,11 +114,13 @@ export function throwHttpGraphQLError<E extends Error>( | |
| ); | ||
| } | ||
|
|
||
| const NODE_ENV = process.env.NODE_ENV ?? ''; | ||
|
|
||
| export async function runHttpQuery( | ||
| handlerArguments: Array<any>, | ||
| request: HttpQueryRequest, | ||
|
moberegger marked this conversation as resolved.
|
||
| ): Promise<HttpQueryResponse> { | ||
| function debugFromNodeEnv(nodeEnv: string | undefined) { | ||
| function debugFromNodeEnv(nodeEnv: string = NODE_ENV) { | ||
| return nodeEnv !== 'production' && nodeEnv !== 'test'; | ||
| } | ||
|
|
||
|
|
@@ -129,18 +131,15 @@ export async function runHttpQuery( | |
| // The options can be generated asynchronously, so we don't have access to | ||
| // the normal options provided by the user, such as: formatError, | ||
| // debug. Therefore, we need to do some unnatural things, such | ||
| // as use NODE_ENV to determine the debug settings | ||
| // as use NODE_ENV to determine the debug settings. Please note that this | ||
| // will not be sensitive to any runtime changes made to NODE_ENV. | ||
| return throwHttpGraphQLError(500, [e as Error], { | ||
| debug: debugFromNodeEnv(process.env.NODE_ENV), | ||
| debug: debugFromNodeEnv(), | ||
| }); | ||
| } | ||
|
|
||
| if (options.debug === undefined) { | ||
| const nodeEnv = | ||
| '__testing_nodeEnv__' in options | ||
| ? options.__testing_nodeEnv__ | ||
| : process.env.NODE_ENV; | ||
| options.debug = debugFromNodeEnv(nodeEnv); | ||
| options.debug = debugFromNodeEnv(options.nodeEnv); | ||
|
moberegger marked this conversation as resolved.
|
||
| } | ||
|
Comment on lines
136
to
143
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one also looks like it would happen in production environments, where
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I would like to be sensitive to runtime NODE_ENV changes. We actually already capture NODE_ENV once in the ApolloServer constructor and pass that through to various ways. Unfortunately for historical reasons much of the core processing in this package happens technically outside of the ApolloServer class even though it is only usable when called from ApolloServer, so this file doesn't have access to that. That said, I basically implemented this already but just as a "testing" hack. What I'd suggest is taking the Is this something you can try?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've spent a few days trying to do this, and I'm not sure what to do. The logic differs on the existence of What I would expect to happen is that if you don't provide This doesn't work with the current implementation, at least as per the tests. The current implementation will simply check if you attempt to set it, even if it's value is So what is happening is that the tests fail, because when The old implementation would avoid defaulting to I'm not sure how to accommodate this.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That being said, I don't think it's possible to get this to not refer This would also introduce a quirk that even if you provided
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When How about we just change that? So we could make Does that resolve all the concerns you have here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! |
||
|
|
||
| // TODO: Errors thrown while resolving the context in | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,12 +99,5 @@ export interface Config<ContextFunctionParams = any> extends BaseConfig { | |
| experimental_approximateDocumentStoreMiB?: number; | ||
| stopOnTerminationSignals?: boolean; | ||
| apollo?: ApolloConfigInput; | ||
| // Apollo Server only uses process.env.NODE_ENV to determine defaults for | ||
| // other behavior which have other mechanisms of setting explicitly. Sometimes | ||
| // our tests want to test the exact logic of how NODE_ENV affects defaults; | ||
| // they can set this parameter, but there's no reason to do so other than for | ||
| // tests. Note that an explicit `__testing_nodeEnv__: undefined` means "act as | ||
| // if the environment variable is not set", whereas the absence of | ||
| // `__testing_nodeEnv__` means to honor the environment variable. | ||
|
Comment on lines
-102
to
-108
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't think that this comment was necessary anymore, since I suppose you can consider |
||
| __testing_nodeEnv__?: string | undefined; | ||
| nodeEnv?: string; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear in the diff, but this is for the
tracemethod. A production environment would still pay the cost for this check, even though it would eventually evaluate tofalseand skip the tracing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is fine. RESTDataSource hasn't been actively maintained in a little while (the current Apollo Server team has not yet ramped up on it; it's basically an entirely unrelated project that is very loosely coupled to Apollo Server). I don't know if anyone depends on the ability to set process.env.NODE_ENV at runtime (eg, potentially after this file is imported) and have it take effect. But if it breaks people we can revert it.