-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add relay-config package for centralised configuration #2746
Conversation
For our apps, this: $ relay-compiler \
--src ./src \
--schema data/schema.graphql \
--language typescript \
--artifactDirectory ./src/__generated__ \
--persist-output ./data/complete.queryMap.json \
--exclude '**/node_modules/**,**/__mocks__/**,**/__generated__/**' {
"plugins": [
["relay", { "artifactDirectory": "./src/__generated__" }]
]
} …is replaced with: module.exports = {
src: "./src",
schema: "./data/schema.graphql",
language: "typescript",
artifactDirectory: "./src/__generated__",
persistOutput: "./data/complete.queryMap.json",
exclude: ["**/node_modules/**", "**/__mocks__/**", "**/__generated__/**"],
} {
"plugins": ["relay"]
} |
languagePlugin = languagePlugin.default; | ||
let languagePlugin: LanguagePlugin; | ||
if (typeof language === 'string') { | ||
const pluginPath = path.resolve(process.cwd(), language); |
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.
This module loading from a path could probably be deprecated with the ability to just pass a function using a relay.config.js
file.
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.
yeah, let's do that. we can deprecate this in a follow-up and delete in a later release
This is so great @alloy! Centralized configuration, yummies! |
noMinify: true, // Note: uglify can't yet handle modern JS | ||
}, | ||
], | ||
}, |
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.
(Cargo-culted this from the test utils package.)
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.
Thank you so much for implementing this, it's really exciting and should make the compiler and babel plugin easier to use. See questions/comments but overall looks great.
packages/relay-compiler/bin/__tests__/RelayCompilerMain-test.js
Outdated
Show resolved
Hide resolved
packages/relay-compiler/bin/__tests__/RelayCompilerMain-test.js
Outdated
Show resolved
Hide resolved
packages/relay-compiler/bin/__tests__/RelayCompilerMain-test.js
Outdated
Show resolved
Hide resolved
languagePlugin = languagePlugin.default; | ||
let languagePlugin: LanguagePlugin; | ||
if (typeof language === 'string') { | ||
const pluginPath = path.resolve(process.cwd(), language); |
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.
yeah, let's do that. we can deprecate this in a follow-up and delete in a later release
const reporter = new ConsoleReporter({ | ||
verbose: options.verbose, | ||
quiet: options.quiet, | ||
const watchman = config.watchman && (await WatchmanClient.isAvailable()); |
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.
hmm the previous name made it clear that this wasn't identical to config.watchman
- the logic has actually changed, see comment below
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 agree on the name being better–perhaps we can rename the option–but after having added test coverage for all behaviour and looking at the old behaviour (current master) I can’t spot any difference in logic.
useWatchman
was determined based onoptions.watchman
and if the client is availableconst useWatchman = options.watchman && (await WatchmanClient.isAvailable()); useWatchman
was from then on only used,options.watchman
wasn’t ever used for anything other substantive.
Additionally, while adding full test coverage, I also noticed that previously there would be no error thrown if one were to enable watch
and watchman
but the client isn’t available. I changed that now, so that should improve the UX of the CLI slightly.
getFileFilter: sourceModuleParser.getFileFilter, | ||
getParser: sourceModuleParser.getParser, | ||
getSchema: () => schema, | ||
watchmanExpression: useWatchman | ||
watchmanExpression: config.watchman |
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.
revert? we should only pass a watchman expression if watchman is available, not just if config.watchman is set.
? buildWatchExpression(sourceSearchOptions) | ||
: null, | ||
filepaths: useWatchman | ||
filepaths: config.watchman |
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.
same here
getParser: DotGraphQLParser.getParser, | ||
getSchema: () => schema, | ||
watchmanExpression: useWatchman | ||
watchmanExpression: config.watchman |
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.
same here and below
One more thought: should we only support |
I’m up for that 👍 I’ll follow up with a doc PR first. |
config = getPathBasedConfig(config); | ||
config = await getWatchConfig(config); | ||
|
||
const codegenRunner = module.exports.getCodegenRunner(config); |
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.
Other than adding optional function parameters to these functions to allow for DI, this appears to be a suggestion to being able to mock functions that reside in the same module as the caller jestjs/jest#936 (comment).
I have no real preference for one or the other, let me know if you do.
Btw here’s a PoC of how having this unified config interface will allow other tooling to hook in better with one’s relay setup–in this case we can have (and share with the community) a generic vscode-apollo config: https://github.com/artsy/emission/blob/b7f138d2b4311d0eed5923b39219e124a6a96a65/apollo.config.js (I would have loved to include validation rules about eg operation/fragment names having to start with the name of the module they reside in, but I just noticed those aren’t proper validation rules, but operate directly on the parsed GraphQL tags 😞.) |
One more thought on my end, if we were to only allow configuration through That would allow other tooling relying on the configuration to get the same quality as |
As noted here, the CI failures exist on |
Just in case somebody ran `yarn install` in one of those directories.
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.
@jstejada has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
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.
@jstejada has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Maybe I am doing it wrong, but the config file is not picked up for me... What I did:
Any ideas? Thanks! :) |
Never mind guys, I was searching for |
Closes #2518
Note: Includes #2745, so that should probably be merged first and then this can be rebased.
I tried multiple times to come up with a doc describing the integration a few times, but then yesterday I had a whole day and figured I’d just write a implementation to show what it could look like. Please feel free to poke holes as much as you need.
What this change does:
Add a new
relay-config
packageUsage of the package is optional. If installed it will be picked-up, otherwise relay will function as it does today.
RelayConfig relies on cosmiconfig to do its bidding. It’s configured to load from:
a
relay
key inpackage.json
a
relay.config.json
fileor a
relay.config.js
fileIt accepts all the same configuration as the CLI does. (As a bonus, I typed the configuration object and the Yargs options object.)
Additionally, when using the
relay.config.js
file, a configuration entry like the language plugin also accepts an actual function:In the future, other entries such as
persistedQueries
andcustomScalars
could also be configured as such and allow for project specific setup.It is used by all pieces of Relay that take configuration–i.e.
relay-compiler
andbabel-plugin-relay
. For instance, previously one would have needed to specifyartifactDirectory
on the CLI torelay-compiler
and the same setting in.babelrc
forbabel-plugin-relay
. This can now be reduced to a single centralised setting.Additional external tooling can also start relying on this. For instance, the
vscode-apollo
extension, with this change, will allow for custom GraphQL validation rules to surface in the UI. Leveraging the centralised config allows one to surfacerelay-compiler
specific validations without needing to take in the same configuration in a different place yet again.Finally, this also adds some previously missing test coverage around language plugin loading.