Skip to content

Commit

Permalink
[Security Solution] Fixes exception modal not loading content (#72770)
Browse files Browse the repository at this point in the history
## Summary

When using the `useFetchIndexPatterns` hook multiple times within a component (e.g. add_exception_modal & edit_exception_modal), the `apolloClient` will perform `queryDeduplication` and prevent the first query from executing. A deep compare is not performed on `indices`, so another field must be passed to circumvent this.


For all the lovely details, see apollographql/react-apollo#2202

Note: As of yesterday, [support has been added](apollographql/apollo-client#6526) for configuring `queryDeduplicating` via `context`. This is available in `apollo-client` `2.6`, so when upgrading (currently on `2.3.8`) we can swap out this workaround to leverage this functionality.

Note II: This [link](https://www.apollographql.com/docs/link/links/dedup/#context) may also be an option after upgrading to a supported version.
  • Loading branch information
spong committed Jul 22, 2020
1 parent 9c7d65c commit 1a1d704
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ export const AddExceptionModal = memo(function AddExceptionModal({
const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex();
const [
{ isLoading: isSignalIndexPatternLoading, indexPatterns: signalIndexPatterns },
] = useFetchIndexPatterns(signalIndexName !== null ? [signalIndexName] : []);
] = useFetchIndexPatterns(signalIndexName !== null ? [signalIndexName] : [], 'signals');

const [{ isLoading: isIndexPatternLoading, indexPatterns }] = useFetchIndexPatterns(ruleIndices);
const [{ isLoading: isIndexPatternLoading, indexPatterns }] = useFetchIndexPatterns(
ruleIndices,
'rules'
);

const onError = useCallback(
(error: Error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ export const EditExceptionModal = memo(function EditExceptionModal({
const { loading: isSignalIndexLoading, signalIndexName } = useSignalIndex();
const [
{ isLoading: isSignalIndexPatternLoading, indexPatterns: signalIndexPatterns },
] = useFetchIndexPatterns(signalIndexName !== null ? [signalIndexName] : []);
] = useFetchIndexPatterns(signalIndexName !== null ? [signalIndexName] : [], 'signals');

const [{ isLoading: isIndexPatternLoading, indexPatterns }] = useFetchIndexPatterns(ruleIndices);
const [{ isLoading: isIndexPatternLoading, indexPatterns }] = useFetchIndexPatterns(
ruleIndices,
'rules'
);

const onError = useCallback(
(error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ const DEFAULT_BROWSER_FIELDS = {};
const DEFAULT_INDEX_PATTERNS = { fields: [], title: '' };
const DEFAULT_DOC_VALUE_FIELDS: DocValueFields[] = [];

export const useFetchIndexPatterns = (defaultIndices: string[] = []): Return => {
// Fun fact: When using this hook multiple times within a component (e.g. add_exception_modal & edit_exception_modal),
// the apolloClient will perform queryDeduplication and prevent the first query from executing. A deep compare is not
// performed on `indices`, so another field must be passed to circumvent this.
// For details, see https://github.com/apollographql/react-apollo/issues/2202
export const useFetchIndexPatterns = (
defaultIndices: string[] = [],
queryDeduplication?: string
): Return => {
const apolloClient = useApolloClient();
const [indices, setIndices] = useState<string[]>(defaultIndices);

Expand Down Expand Up @@ -74,6 +81,7 @@ export const useFetchIndexPatterns = (defaultIndices: string[] = []): Return =>
variables: {
sourceId: 'default',
defaultIndex: indices,
...(queryDeduplication != null ? { queryDeduplication } : {}),
},
context: {
fetchOptions: {
Expand Down

0 comments on commit 1a1d704

Please sign in to comment.