Skip to content

Commit

Permalink
Fix graphiql react issue with setting tabs title (#2968)
Browse files Browse the repository at this point in the history
* updates fuzzyExtractOperationName regex

* adds/rearranges tests for "comment line handling"
  • Loading branch information
jonathanawesome authored Jan 10, 2023
1 parent 0669767 commit 0434d94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/graphiql-react/src/editor/__tests__/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,27 @@ describe('fuzzyExtractionOperationTitle', () => {
it('should return null for anonymous queries', () => {
expect(fuzzyExtractOperationName('{}')).toBeNull();
});
it('should not extract query names with comments', () => {
expect(
fuzzyExtractOperationName('# query My_3ExampleQuery() {}'),
).toBeNull();

describe('comment line handling', () => {
it('should not extract query names within commented out lines', () => {
expect(
fuzzyExtractOperationName('# query My_3ExampleQuery() {}'),
).toBeNull();
});
it('should extract query names when there is a single leading comment line', () => {
expect(
fuzzyExtractOperationName(
'# comment line 1 \n query MyExampleQueryWithSingleCommentLine() {}',
),
).toEqual('MyExampleQueryWithSingleCommentLine');
});
it('should extract query names when there are more than one leading comment lines', () => {
expect(
fuzzyExtractOperationName(
'# comment line 1 \n # comment line 2 \n query MyExampleQueryWithMultipleCommentLines() {}',
),
).toEqual('MyExampleQueryWithMultipleCommentLines');
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/graphiql-react/src/editor/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ function hashFromTabContents(args: {
}

export function fuzzyExtractOperationName(str: string): string | null {
const regex = /^(?!.*#).*(query|subscription|mutation)\s+([a-zA-Z0-9_]+)/;
const regex = /^(?!#).*(query|subscription|mutation)\s+([a-zA-Z0-9_]+)/m;

const match = regex.exec(str);

return match?.[2] ?? null;
Expand Down

0 comments on commit 0434d94

Please sign in to comment.