Skip to content

Commit

Permalink
closer behavior to existing
Browse files Browse the repository at this point in the history
  • Loading branch information
znarf committed Dec 7, 2023
1 parent 4621f7e commit 2639d5a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('processor.preprocess() with graphql-config', () => {
expect(blocks).toMatchInlineSnapshot(`
[
{
filename: default_document.graphql,
filename: document.graphql,
lineOffset: 2,
offset: 77,
text: query users { id },
Expand All @@ -47,7 +47,7 @@ describe('processor.preprocess() with graphql-config', () => {
expect(blocks).toMatchInlineSnapshot(`
[
{
filename: default_document.graphql,
filename: document.graphql,
lineOffset: 0,
offset: 16,
text: query users { id },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('processor.preprocess() without graphql-config', () => {
expect(blocks).toMatchInlineSnapshot(`
[
{
filename: default_document.graphql,
filename: document.graphql,
lineOffset: 2,
offset: 64,
text: query users { id },
Expand All @@ -34,7 +34,7 @@ describe('processor.preprocess() without graphql-config', () => {
expect(blocks).toMatchInlineSnapshot(`
[
{
filename: default_document.graphql,
filename: document.graphql,
lineOffset: 0,
offset: 17,
text: query users { id },
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/__tests__/unique-fragment-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ruleTester.run('unique-fragment-name', rule, {
{
// Compare filepath of code as real instead of virtual with siblings
...SIBLING_FRAGMENTS(join(__dirname, 'mocks/unique-fragment.js')),
filename: join(__dirname, 'mocks/unique-fragment.js/0_default_document.graphql'),
filename: join(__dirname, 'mocks/unique-fragment.js/0_document.graphql'),
code: /* GraphQL */ `
fragment UserFields on User {
id
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/__tests__/unique-operation-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ruleTester.run('unique-operation-name', rule, {
{
// Compare filepath of code as real instead of virtual with siblings
...SIBLING_OPERATIONS(join(__dirname, 'mocks/unique-fragment.js')),
filename: join(__dirname, 'mocks/unique-fragment.js/1_default_document.graphql'),
filename: join(__dirname, 'mocks/unique-fragment.js/1_document.graphql'),
code: /* GraphQL */ `
query User {
user {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const handleVirtualPath = (documents: Source[], project: GraphQLProjectConfig):
}
filepathMap[location] ??= -1;
const index = (filepathMap[location] += 1);
const prefix = project.name && project.name !== 'default' ? `${project.name}_` : '';
return {
...source,
location: resolve(location, `${index}_${project.name ?? 'default'}_document.graphql`),
location: resolve(location, `${index}_${prefix}document.graphql`),
};
});
};
Expand Down
29 changes: 17 additions & 12 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ const RELEVANT_KEYWORDS = ['gql', 'graphql', 'GraphQL'] as const;
function getMatchingProjects(onDiskConfig: GraphQLConfig, filePath: string) {
const matchingProjects: Record<string, GraphQLProjectConfig> = {};

if (onDiskConfig?.projects) {
// Default project for file
// - ok if project is null
// - getProjectForFile may be mocked in tests
const project = onDiskConfig?.getProjectForFile(filePath);
const projectName = project?.name ?? 'default';
if (!matchingProjects[projectName]) {
matchingProjects[projectName] = project;
}

// If more than one project
if (onDiskConfig?.projects && Object.keys(onDiskConfig.projects).length > 1) {
// Reference:
// https://github.com/kamilkisiela/graphql-config/blob/423de0e07214ad6d800fb508a74951a5bfc045e6/src/config.ts#L143
for (const [projectName, project] of Object.entries(onDiskConfig.projects)) {
for (const project of Object.values(onDiskConfig.projects)) {
if (project.match(filePath)) {
matchingProjects[projectName] = project;
matchingProjects[project.name] = project;
} else if (!project.include && !project.exclude) {
matchingProjects[projectName] = project;
matchingProjects[project.name] = project;
}
}
}

// Default, also help with tests
const project = onDiskConfig?.getProjectForFile(filePath);
const projectName = project?.name ?? 'default';
if (!matchingProjects[projectName]) {
matchingProjects[projectName] = project;
}

return matchingProjects;
}

Expand Down Expand Up @@ -92,8 +95,10 @@ export const processor = {
});

for (const item of sources) {
// We avoid an unnecessary prefix when it's falsy or default
const prefix = projectName && projectName !== 'default' ? `${projectName}_` : '';
blocks.push({
filename: `${projectName ?? 'default'}_document.graphql`,
filename: `${prefix}document.graphql`,
text: item.body,
lineOffset: item.locationOffset.line - 1,
// @ts-expect-error -- `index` field exist but show ts error
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const logger = {

export const normalizePath = (path: string): string => (path || '').replace(/\\/g, '/');

export const VIRTUAL_DOCUMENT_REGEX = /\/\d+_([a-zA-Z0-9]*)_document.graphql$/;
export const VIRTUAL_DOCUMENT_REGEX = /\/\d+_([a-zA-Z0-9]*)?_?document.graphql$/;

export const CWD = process.cwd();

Expand Down

0 comments on commit 2639d5a

Please sign in to comment.