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

Deduplicate fragment definition before codegen #38

Merged
merged 1 commit into from
Feb 18, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jspm_packages/
/*.d.ts
/workers
!/babel.config.js
!/jest.config.js
!/index.js
!/.pnp.js

Expand Down
1,899 changes: 1,865 additions & 34 deletions .pnp.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions __tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
import { visit } from 'graphql';
import { deduplicateFragmentFromDocuments } from '~/src/common';

const readFile = promisify(fs.readFile);

test('deduplicateFragmentFromDocuments()', async () => {
const data = await readFile(path.resolve(__dirname, 'data/gh-33.json'), 'utf-8');
const documents = JSON.parse(data);
const resultDocuments = deduplicateFragmentFromDocuments(documents);

const fragmentNames = new Set<string>();
for (const { document } of resultDocuments) {
visit(document, {
FragmentDefinition(node) {
const duplicated = fragmentNames.has(node.name.value);
expect(duplicated).toBe(false);

if (!duplicated) {
fragmentNames.add(node.name.value);
}
},
});
}
});
1 change: 1 addition & 0 deletions __tests__/data/gh-33.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
verbose: true,
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
},
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"typescript"
],
"scripts": {
"test": "jest",
"tsc": "tsc --emitDeclarationOnly --skipLibCheck",
"flowtype": "cp -f src/types.ts src/types.js && babel src/types.js -d . --plugins module:@babel/plugin-transform-flow-comments",
"build": "babel src -d . --extensions .ts,.tsx",
Expand All @@ -58,7 +59,8 @@
"@graphql-toolkit/common": "^0.9.7",
"@graphql-toolkit/core": "^0.9.7",
"async": "^3.1.1",
"common-tags": "^1.8.0"
"common-tags": "^1.8.0",
"~": "link:."
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand All @@ -68,14 +70,18 @@
"@babel/plugin-transform-flow-comments": "^7.8.3",
"@babel/plugin-transform-named-capturing-groups-regex": "^7.8.3",
"@babel/preset-typescript": "^7.8.3",
"@babel/runtime": "^7.8.4",
"@types/async": "^3.0.7",
"@types/common-tags": "^1.8.0",
"@types/graphql": "^14.5.0",
"@types/node": "^12.7.3",
"@types/jest": "^25.1.2",
"@types/node": "^13.7.1",
"@yarnpkg/pnpify": "^2.0.0-rc.18",
"babel-jest": "^25.1.0",
"babel-preset-gatsby-package": "^0.2.16",
"gatsby": "^2.19.17",
"graphql": "^14.6.0",
"jest": "^25.1.0",
"typescript": "^3.7.5",
"utility-types": "^3.10.0"
}
Expand Down
32 changes: 32 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import { dirname } from 'path';
import { promisify } from 'util';
import { Source } from '@graphql-toolkit/common';

const _mkdir = promisify(fs.mkdir);
const _readFile = promisify(fs.readFile);
Expand All @@ -19,3 +20,34 @@ export const formatLanguage = (lang: 'typescript' | 'flow') => (
);

export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;

export function deduplicateFragmentFromDocuments(documents: Source[]) {
const existFragmentNames = new Set<string>();
return documents.map(source => {
const { document } = source;

// Nothing to do for other sources
if (!document) {
return source;
}

const uniqDefinitions = document.definitions.filter(def => {
// De-dup only cares about fragments
if (def.kind !== 'FragmentDefinition') {
return true;
}
const fragmentName = def.name.value;
const duplicated = existFragmentNames.has(fragmentName);
if (!duplicated) existFragmentNames.add(fragmentName);
return !duplicated;
});

return {
...source,
document: {
...document,
definitions: uniqDefinitions,
},
};
});
}
7 changes: 4 additions & 3 deletions src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import FileParser from 'gatsby/dist/query/file-parser';
import { parseGraphQLSDL, Source } from '@graphql-toolkit/common';

import { writeFile, UnwrapPromise, readFile } from './common';
import { writeFile, UnwrapPromise, readFile, deduplicateFragmentFromDocuments } from './common';
import { setupCodegenWorker, setupInsertTypeWorker, InsertTypeTask } from './workers';
import { requirePluginOptions, RequiredPluginOptions } from './plugin-utils';
import { GatsbyKnownAction } from './gatsby-utils';
Expand Down Expand Up @@ -136,8 +136,9 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({
});

const pushCodegenTask = () => {
// @ts-ignore
codegenWorker.push({ documents: [...trackedSource.values()].filter(Boolean) });
codegenWorker.push({
documents: deduplicateFragmentFromDocuments([...trackedSource.values()].filter(Boolean)),
});
};

pushCodegenTask();
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"esModuleInterop": true,
"downlevelIteration": true,
"declaration": true,
"outDir": "."
"outDir": ".",
"baseUrl": ".",
"paths": {
"~/*": ["./*"]
}
},
"include": [
"src",
Expand Down
Loading