-
Notifications
You must be signed in to change notification settings - Fork 290
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
doc: Add FAQ page #1160
Merged
Merged
doc: Add FAQ page #1160
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ module.exports = { | |
'modules.md', | ||
], | ||
}, | ||
['faq.md', 'FAQ'], | ||
{ | ||
title: 'Community', | ||
children: [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Frequently Asked Questions | ||
|
||
This page contains answers to common questions and issues with using Fx. | ||
|
||
## Does the order of `fx.Option`s matter? | ||
|
||
No, the order in which you provide Fx options | ||
to `fx.Options`, `fx.New`, `fx.Module`, and others does not matter. | ||
|
||
Ordering of options relative to each other is as follows: | ||
|
||
* Adding values: | ||
Operations like `fx.Provide` and `fx.Supply` are run in dependency order. | ||
Dependencies are determined by the function parameters and results. | ||
|
||
```go | ||
// The following are all equivalent: | ||
fx.Options(fx.Provide(ParseConfig, NewLogger)) | ||
fx.Options(fx.Provide(NewLogger, ParseConfig)) | ||
fx.Options(fx.Provide(ParseConfig), fx.Provide(NewLogger)) | ||
fx.Options(fx.Provide(NewLogger), fx.Provide(ParseConfig)) | ||
``` | ||
|
||
* Consuming values: | ||
Operations like `fx.Invoke` and `fx.Populate` are run | ||
after their dependencies have been satisfied: after `fx.Provide`s. | ||
|
||
Relative to each other, invokes are run in the order they were specified. | ||
|
||
```go | ||
fx.Invoke(a, b) | ||
// a() is run before b() | ||
``` | ||
|
||
`fx.Module` hierarchies affect invocation order: | ||
invocations in a parent module are run after those of a child module. | ||
|
||
```go | ||
fx.Options( | ||
fx.Invoke(a), | ||
fx.Module("child", fx.Invoke(b)), | ||
), | ||
// b() is run before a() | ||
``` | ||
|
||
* Replacing values: | ||
Operations like `fx.Decorate` and `fx.Replace` are run | ||
after the Provide operations that they depend on, | ||
but before the Invoke operations that consume those values. | ||
|
||
Ordering of decorations relative to each other | ||
is determined by `fx.Module` hierarchies: | ||
decorations in a parent module are applied after those of a child module. | ||
|
||
## Why does `fx.Supply` not accept interfaces? | ||
|
||
This is a technical limitation of how reflection in Go works. | ||
Suppose you have: | ||
|
||
```go | ||
var redisClient ClientInterface = &redis.Client{ ... } | ||
``` | ||
|
||
When you call `fx.Supply(redisClient)`, | ||
the knowledge that you intended to use this as a `ClientInterface` is lost. | ||
Fx has to use runtime reflection to inspect the type of the value, | ||
and at that point the Go runtime only tells it that it’s a `*redis.Client`. | ||
|
||
You can work around this with the `fx.Annotate` function | ||
and the `fx.As` annotation. | ||
|
||
```go | ||
fx.Supply( | ||
fx.Annotate(redisClient, fx.As(new(ClientInterface))), | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think this is true for determining the order in which
fx.Invoke
d functions get run, which was a recent point of confusion (ref: #1152). Consider exempting Invoke from this statement.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.
Fair. There's a weird situation here.
On paper, there's no guarantee about the order in which invokes run.
But you're right, their order matters.
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.
@JacobOaks I realized that this was actually too simplified and lost some information.
Ordering of options relative to fx.New is irrelevant, but ordering of operations relative to each other can matter.
I've rewritten this section to better clarify this.
LMK what you think
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.
Actually, looks like we do document the fx.Invoke invocation order.
I'll update this entry.