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

Fix graphiql react issue with setting tabs title #2968

Merged
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
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 @@ -75,10 +75,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 @@ -332,7 +332,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