Skip to content
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

Change autocorrected in search query from boolean to string #174

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions src/features/search/__tests__/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,55 @@ describe('search resolver', () => {
expect(noCursor).toEqual(-1);
});

it('can autocorrect queries', async () => {
(findCorrection as jest.Mock).mockReturnValueOnce('가다');
describe('autocorrect', () => {
it('can autocorrect queries', async () => {
(findCorrection as jest.Mock).mockReturnValueOnce('가다');

const { results, cursor, autocorrected } = await (
resolvers.Query.search as any
)(null, {
query: '가디',
});

const { _id, ...rest } = entries[1];
expect(findCorrection).toHaveBeenCalledWith('가디');
expect(results.length).toEqual(1);
expect(cursor).toEqual(1);
expect(autocorrected).toEqual('가다');
expect(omit(results[0], ['score'])).toEqual({
id: _id.toString(),
...rest,
});
});

const { results, cursor } = await (resolvers.Query.search as any)(null, {
query: '가디',
it('handles no autocorrection found', async () => {
(findCorrection as jest.Mock).mockReturnValueOnce(null);

const { results, cursor, autocorrected } = await (
resolvers.Query.search as any
)(null, {
query: '가디',
});

expect(findCorrection).toHaveBeenCalledWith('가디');
expect(results.length).toEqual(0);
expect(cursor).toEqual(-1);
expect(autocorrected).toEqual(undefined);
});

const { _id, ...rest } = entries[1];
expect(findCorrection).toHaveBeenCalledWith('가디');
expect(results.length).toEqual(1);
expect(cursor).toEqual(1);
expect(omit(results[0], ['score'])).toEqual({
id: _id.toString(),
...rest,
it('handles no results found', async () => {
(findCorrection as jest.Mock).mockReturnValueOnce('no result');

const { results, cursor, autocorrected } = await (
resolvers.Query.search as any
)(null, {
query: '가디',
});

expect(findCorrection).toHaveBeenCalledWith('가디');
expect(results.length).toEqual(0);
expect(cursor).toEqual(-1);
expect(autocorrected).toEqual('no result');
});
});
});
Expand Down
13 changes: 6 additions & 7 deletions src/features/search/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ const resolvers: Resolvers = {
Query: {
search: async (_, { query, cursor }) => {
query = query.trim();
let autocorrected = false;

if (!cursor || cursor < 0) cursor = 0;
if (!query) return { cursor, autocorrected, results: [] };
if (!query) return { cursor, results: [] };

let autocorrected: undefined | string;
let results: Entry[];
if (isKorean(query)) {
results = await searchKorean(query);

// Do it all over again but with autocorrect
if (!results.length) {
const autocorrect = findCorrection(query);

if (autocorrect) {
results = await searchKorean(autocorrect);
autocorrected = true;
const possibleCorrection = findCorrection(query);
if (possibleCorrection) {
results = await searchKorean(possibleCorrection);
autocorrected = possibleCorrection;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/search/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const typeDef = gql`

type Result {
cursor: Int
autocorrected: Boolean!
autocorrected: String
results: [Entry]!
}
`;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export type Question = {

export type Result = {
__typename?: 'Result';
autocorrected: Scalars['Boolean'];
autocorrected?: Maybe<Scalars['String']>;
cursor?: Maybe<Scalars['Int']>;
results: Array<Maybe<Entry>>;
};
Expand Down Expand Up @@ -427,7 +427,7 @@ export type QueryResolvers<ContextType = any, ParentType extends ResolversParent
};

export type ResultResolvers<ContextType = any, ParentType extends ResolversParentTypes['Result'] = ResolversParentTypes['Result']> = {
autocorrected?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
autocorrected?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
cursor?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
results?: Resolver<Array<Maybe<ResolversTypes['Entry']>>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
Expand Down