Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1238d56

Browse files
authored
feat: make search use query transformers (#499)
1 parent 66f1a26 commit 1238d56

File tree

2 files changed

+139
-123
lines changed

2 files changed

+139
-123
lines changed

src/search/backend.tsx

Lines changed: 131 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,163 @@
11
import { isEqual } from 'lodash'
22
import { Observable } from 'rxjs'
3-
import { distinctUntilChanged, map, mergeMap } from 'rxjs/operators'
3+
import { catchError, distinctUntilChanged, map, mergeMap, switchMap } from 'rxjs/operators'
44
import { SearchOptions } from '.'
55
import { gql, queryGraphQL } from '../backend/graphql'
66
import * as GQL from '../backend/graphqlschema'
77
import { mutateConfigurationGraphQL } from '../configuration/backend'
8+
import { ExtensionsControllerProps } from '../extensions/ExtensionsClientCommonContext'
89
import { currentConfiguration } from '../settings/configuration'
9-
import { createAggregateError } from '../util/errors'
10+
import { asError, createAggregateError, ErrorLike } from '../util/errors'
1011

11-
export function search(options: SearchOptions): Observable<GQL.ISearchResults> {
12-
return queryGraphQL(
13-
gql`
14-
query Search($query: String!) {
15-
search(query: $query) {
16-
results {
17-
__typename
18-
limitHit
19-
resultCount
20-
approximateResultCount
21-
missing {
22-
name
23-
}
24-
cloning {
25-
name
26-
}
27-
timedout {
28-
name
29-
}
30-
indexUnavailable
31-
dynamicFilters {
32-
value
33-
label
34-
count
35-
limitHit
36-
kind
37-
}
38-
results {
39-
... on Repository {
12+
export function search(
13+
options: SearchOptions,
14+
{ extensionsController }: ExtensionsControllerProps
15+
): Observable<GQL.ISearchResults | ErrorLike> {
16+
/**
17+
* Emits whenever a search is executed, and whenever an extension registers a query transformer.
18+
*/
19+
return extensionsController.registries.queryTransformer.transformQuery(options.query).pipe(
20+
switchMap(query =>
21+
queryGraphQL(
22+
gql`
23+
query Search($query: String!) {
24+
search(query: $query) {
25+
results {
4026
__typename
41-
id
42-
name
43-
url
44-
}
45-
... on FileMatch {
46-
__typename
47-
file {
48-
path
49-
url
50-
commit {
51-
oid
52-
}
53-
}
54-
repository {
55-
name
56-
url
57-
}
5827
limitHit
59-
symbols {
28+
resultCount
29+
approximateResultCount
30+
missing {
6031
name
61-
containerName
62-
url
63-
kind
64-
}
65-
lineMatches {
66-
preview
67-
lineNumber
68-
offsetAndLengths
6932
}
70-
}
71-
... on CommitSearchResult {
72-
__typename
73-
refs {
33+
cloning {
7434
name
75-
displayName
76-
prefix
77-
repository {
78-
name
79-
}
8035
}
81-
sourceRefs {
36+
timedout {
8237
name
83-
displayName
84-
prefix
85-
repository {
86-
name
87-
}
88-
}
89-
messagePreview {
90-
value
91-
highlights {
92-
line
93-
character
94-
length
95-
}
9638
}
97-
diffPreview {
39+
indexUnavailable
40+
dynamicFilters {
9841
value
99-
highlights {
100-
line
101-
character
102-
length
103-
}
42+
label
43+
count
44+
limitHit
45+
kind
10446
}
105-
commit {
106-
id
107-
repository {
47+
results {
48+
... on Repository {
49+
__typename
50+
id
10851
name
10952
url
11053
}
111-
oid
112-
abbreviatedOID
113-
author {
114-
person {
54+
... on FileMatch {
55+
__typename
56+
file {
57+
path
58+
url
59+
commit {
60+
oid
61+
}
62+
}
63+
repository {
64+
name
65+
url
66+
}
67+
limitHit
68+
symbols {
69+
name
70+
containerName
71+
url
72+
kind
73+
}
74+
lineMatches {
75+
preview
76+
lineNumber
77+
offsetAndLengths
78+
}
79+
}
80+
... on CommitSearchResult {
81+
__typename
82+
refs {
83+
name
11584
displayName
116-
avatarURL
85+
prefix
86+
repository {
87+
name
88+
}
89+
}
90+
sourceRefs {
91+
name
92+
displayName
93+
prefix
94+
repository {
95+
name
96+
}
97+
}
98+
messagePreview {
99+
value
100+
highlights {
101+
line
102+
character
103+
length
104+
}
105+
}
106+
diffPreview {
107+
value
108+
highlights {
109+
line
110+
character
111+
length
112+
}
113+
}
114+
commit {
115+
id
116+
repository {
117+
name
118+
url
119+
}
120+
oid
121+
abbreviatedOID
122+
author {
123+
person {
124+
displayName
125+
avatarURL
126+
}
127+
date
128+
}
129+
message
130+
url
131+
tree(path: "") {
132+
canonicalURL
133+
}
117134
}
118-
date
119135
}
120-
message
121-
url
122-
tree(path: "") {
123-
canonicalURL
136+
}
137+
alert {
138+
title
139+
description
140+
proposedQueries {
141+
description
142+
query
124143
}
125144
}
145+
elapsedMilliseconds
126146
}
127147
}
128-
alert {
129-
title
130-
description
131-
proposedQueries {
132-
description
133-
query
134-
}
135-
}
136-
elapsedMilliseconds
137148
}
138-
}
139-
}
140-
`,
141-
{ query: options.query }
142-
).pipe(
143-
map(({ data, errors }) => {
144-
if (!data || !data.search || !data.search.results) {
145-
throw createAggregateError(errors)
146-
}
147-
return data.search.results
148-
})
149+
`,
150+
{ query }
151+
).pipe(
152+
map(({ data, errors }) => {
153+
if (!data || !data.search || !data.search.results) {
154+
throw createAggregateError(errors)
155+
}
156+
return data.search.results
157+
}),
158+
catchError(error => [asError(error)])
159+
)
160+
)
149161
)
150162
}
151163

src/search/results/SearchResults.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { catchError, distinctUntilChanged, filter, map, startWith, switchMap, ta
66
import { parseSearchURLQuery, SearchOptions } from '..'
77
import * as GQL from '../../backend/graphqlschema'
88
import { PageTitle } from '../../components/PageTitle'
9+
import { ExtensionsControllerProps } from '../../extensions/ExtensionsClientCommonContext'
910
import { currentConfiguration } from '../../settings/configuration'
1011
import { eventLogger } from '../../tracking/eventLogger'
12+
import { isErrorLike } from '../../util/errors'
1113
import { search } from '../backend'
1214
import { FilterChip } from '../FilterChip'
1315
import { isSearchResults, submitSearch, toggleSearchFilter } from '../helpers'
@@ -17,7 +19,7 @@ import { SearchResultsListOld } from './SearchResultsListOld'
1719

1820
const UI_PAGE_SIZE = 75
1921

20-
interface SearchResultsProps {
22+
interface SearchResultsProps extends ExtensionsControllerProps {
2123
authenticatedUser: GQL.IUser | null
2224
location: H.Location
2325
history: H.History
@@ -87,16 +89,18 @@ export class SearchResults extends React.Component<SearchResultsProps, SearchRes
8789
// Reset view state
8890
[{ resultsOrError: undefined, didSave: false }],
8991
// Do async search request
90-
search(searchOptions).pipe(
92+
search(searchOptions, this.props).pipe(
9193
// Log telemetry
9294
tap(
9395
results =>
9496
eventLogger.log('SearchResultsFetched', {
9597
code_search: {
9698
// 🚨 PRIVACY: never provide any private data in { code_search: { results } }.
9799
results: {
98-
results_count: results.results.length,
99-
any_cloning: results.cloning.length > 0,
100+
results_count: isErrorLike(results) ? 0 : results.results.length,
101+
any_cloning: isErrorLike(results)
102+
? false
103+
: results.cloning.length > 0,
100104
},
101105
},
102106
}),

0 commit comments

Comments
 (0)