Skip to content

Commit

Permalink
Merge branch 'master' into typescript-resolvers-extract-interface-types
Browse files Browse the repository at this point in the history
  • Loading branch information
eddeee888 authored Apr 3, 2023
2 parents 8f0a018 + c673fb1 commit 553afcd
Show file tree
Hide file tree
Showing 37 changed files with 631 additions and 569 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-clouds-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Don't emit import statements for unused fragments
6 changes: 3 additions & 3 deletions examples/persisted-documents-string-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"babel-jest": "28.1.3",
"@graphql-codegen/cli": "3.2.2",
"@graphql-codegen/client-preset": "2.1.1",
"@babel/core": "7.21.3",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.21.0"
"@babel/core": "7.21.4",
"@babel/preset-env": "7.21.4",
"@babel/preset-typescript": "7.21.4"
},
"scripts": {
"test": "jest",
Expand Down
6 changes: 3 additions & 3 deletions examples/persisted-documents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"babel-jest": "28.1.3",
"@graphql-codegen/cli": "3.2.2",
"@graphql-codegen/client-preset": "2.1.1",
"@babel/core": "7.21.3",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.21.0"
"@babel/core": "7.21.4",
"@babel/preset-env": "7.21.4",
"@babel/preset-typescript": "7.21.4"
},
"scripts": {
"test": "jest",
Expand Down
4 changes: 2 additions & 2 deletions examples/react/apollo-client-swc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"@graphql-codegen/client-preset": "^2.1.1",
"@graphql-codegen/cli": "^3.2.2",
"@vitejs/plugin-react-swc": "^3.2.0",
"@types/react": "18.0.30",
"@types/react": "18.0.32",
"@types/react-dom": "18.0.11",
"typescript": "5.0.2",
"typescript": "5.0.3",
"vite": "^4.1.0"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/apollo-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@types/node": "^18.11.18",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.10",
"typescript": "5.0.2",
"typescript": "5.0.3",
"serve": "14.2.0",
"cypress": "12.9.0",
"start-server-and-test": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/http-executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/node": "^18.11.18",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.10",
"typescript": "5.0.2",
"typescript": "5.0.3",
"serve": "14.2.0",
"cypress": "12.9.0",
"start-server-and-test": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/nextjs-swr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"@types/react-dom": "^18.0.10",
"eslint": "^8.21.0",
"eslint-config-next": "^13.0.0",
"typescript": "5.0.2"
"typescript": "5.0.3"
}
}
2 changes: 1 addition & 1 deletion examples/react/tanstack-react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/node": "^18.11.18",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.10",
"typescript": "5.0.2",
"typescript": "5.0.3",
"serve": "14.2.0",
"cypress": "12.9.0",
"start-server-and-test": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/urql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@graphql-codegen/cli": "^3.2.2",
"@graphql-codegen/client-preset": "^2.1.1",
"@vitejs/plugin-react": "^3.1.0",
"typescript": "5.0.2",
"typescript": "5.0.3",
"serve": "14.2.0",
"cypress": "12.9.0",
"start-server-and-test": "2.0.0",
Expand Down
6 changes: 3 additions & 3 deletions examples/yoga-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"babel-jest": "28.1.3",
"@graphql-codegen/cli": "3.2.2",
"@graphql-codegen/client-preset": "2.1.1",
"@babel/core": "7.21.3",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.21.0"
"@babel/core": "7.21.4",
"@babel/preset-env": "7.21.4",
"@babel/preset-typescript": "7.21.4"
},
"scripts": {
"test": "jest",
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
"examples/**/*"
],
"devDependencies": {
"@babel/core": "7.21.3",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.21.0",
"@babel/core": "7.21.4",
"@babel/preset-env": "7.21.4",
"@babel/preset-typescript": "7.21.4",
"@changesets/changelog-github": "0.4.8",
"@changesets/cli": "2.26.0",
"@theguild/eslint-config": "0.8.1",
"@theguild/eslint-config": "0.9.0",
"@theguild/prettier-config": "0.1.1",
"@types/jest": "28.1.8",
"babel-jest": "28.1.3",
"bob-the-bundler": "4.1.1",
"eslint": "8.36.0",
"eslint": "8.37.0",
"graphql": "16.6.0",
"husky": "8.0.3",
"jest": "28.1.3",
Expand All @@ -61,7 +61,7 @@
"ts-node": "10.9.1",
"tslib": "2.5.0",
"tsx": "3.12.6",
"typescript": "5.0.2"
"typescript": "5.0.3"
},
"lint-staged": {
"packages/**/src/**/*.{ts,tsx}": [
Expand All @@ -83,6 +83,6 @@
"**/apollo-language-server/graphql": "^16.0.0",
"**/@types/graphql-upload/graphql": "^16.0.0",
"ts-node": "10.9.1",
"typescript": "5.0.2"
"typescript": "5.0.3"
}
}
8 changes: 5 additions & 3 deletions packages/graphql-codegen-cli/src/generate-and-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export async function generate(
await context.profiler.run(() => lifecycleHooks(config.hooks).afterStart(), 'Lifecycle: afterStart');

let previouslyGeneratedFilenames: string[] = [];

function removeStaleFiles(config: Types.Config, generationResult: Types.FileOutput[]) {
const filenames = generationResult.map(o => o.filename);
// find stale files from previous build which are not present in current build
const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
staleFilenames.forEach(filename => {
for (const filename of staleFilenames) {
if (shouldOverwrite(config, filename)) {
return unlinkFile(filename, err => {
unlinkFile(filename, err => {
const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
if (err) {
debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
Expand All @@ -34,11 +35,12 @@ export async function generate(
}
});
}
});
}
previouslyGeneratedFilenames = filenames;
}

const recentOutputHash = new Map<string, string>();

async function writeOutput(generationResult: Types.FileOutput[]) {
if (!saveToFile) {
return generationResult;
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/init/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Answers, Tags } from './types.js';
function jsObjectToBabelObjectExpression<T extends object>(obj: T): ReturnType<typeof t.objectExpression> {
const objExp = t.objectExpression([]);

Object.entries(obj).forEach(([key, val]) => {
for (const [key, val] of Object.entries(obj)) {
if (Array.isArray(val)) {
objExp.properties.push(
t.objectProperty(
Expand All @@ -30,7 +30,7 @@ function jsObjectToBabelObjectExpression<T extends object>(obj: T): ReturnType<t
)
);
}
});
}

return objExp;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function getPluginByName(
.map(name =>
`
- ${name}
`.trimRight()
`.trimEnd()
)
.join('');

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function getPresetByName(
.map(name =>
`
- ${name}
`.trimRight()
`.trimEnd()
)
.join('');

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/utils/debugging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function debugLog(message: string, ...meta: any[]) {
}

export function printLogs() {
queue.forEach(log => {
for (const log of queue) {
getLogger().info(log.message, ...log.meta);
});
}
resetLogs();
}

Expand Down
44 changes: 22 additions & 22 deletions packages/graphql-codegen-cli/src/utils/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,28 @@ export const createWatcher = (
const schemas = normalizeInstanceOrArray<Types.Schema>(config.schema);

// Add schemas and documents from "generates"
Object.keys(config.generates)
.map(filename => normalizeOutputParam(config.generates[filename]))
.forEach(conf => {
schemas.push(...normalizeInstanceOrArray<Types.Schema>(conf.schema));
documents.push(...normalizeInstanceOrArray<Types.OperationDocument>(conf.documents));
files.push(...normalizeInstanceOrArray(conf.watchPattern));
});
for (const conf of Object.keys(config.generates).map(filename => normalizeOutputParam(config.generates[filename]))) {
schemas.push(...normalizeInstanceOrArray<Types.Schema>(conf.schema));
documents.push(...normalizeInstanceOrArray<Types.OperationDocument>(conf.documents));
files.push(...normalizeInstanceOrArray(conf.watchPattern));
}

if (documents) {
documents.forEach(doc => {
for (const doc of documents) {
if (typeof doc === 'string') {
files.push(doc);
} else {
files.push(...Object.keys(doc));
}
});
}
}

schemas.forEach((schema: string) => {
for (const s of schemas) {
const schema = s as string;
if (isGlob(schema) || isValidPath(schema)) {
files.push(schema);
}
});
}

if (typeof config.watch !== 'boolean') {
files.push(...normalizeInstanceOrArray<string>(config.watch));
Expand All @@ -78,18 +77,19 @@ export const createWatcher = (
emitWatching();

const ignored: string[] = [];
Object.keys(config.generates)
.map(filename => ({ filename, config: normalizeOutputParam(config.generates[filename]) }))
.forEach(entry => {
if (entry.config.preset) {
const extension = entry.config.presetConfig?.extension;
if (extension) {
ignored.push(join(entry.filename, '**', '*' + extension));
}
} else {
ignored.push(entry.filename);
for (const entry of Object.keys(config.generates).map(filename => ({
filename,
config: normalizeOutputParam(config.generates[filename]),
}))) {
if (entry.config.preset) {
const extension = entry.config.presetConfig?.extension;
if (extension) {
ignored.push(join(entry.filename, '**', '*' + extension));
}
});
} else {
ignored.push(entry.filename);
}
}

watcherSubscription = await parcelWatcher.subscribe(
process.cwd(),
Expand Down
14 changes: 7 additions & 7 deletions packages/graphql-codegen-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
return deduplicatedDefinitions.add(node);
}

files.forEach(file => {
for (const file of files) {
const deduplicatedDefinitions = new Set<DefinitionNode>();
visit(file.document, {
OperationDefinition(node) {
Expand All @@ -273,18 +273,18 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
},
});
(file.document as any).definitions = Array.from(deduplicatedDefinitions);
});
}

const kinds = Object.keys(definitionMap);

kinds.forEach(kind => {
for (const kind of kinds) {
const definitionKindMap = definitionMap[kind];
const names = Object.keys(definitionKindMap);
if (names.length) {
const duplicated = names.filter(name => definitionKindMap[name].contents.size > 1);

if (!duplicated.length) {
return;
continue;
}

const list = duplicated
Expand All @@ -295,10 +295,10 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
.map(filepath => {
return `
- ${filepath}
`.trimRight();
`.trimEnd();
})
.join('')}
`.trimRight()
`.trimEnd()
)
.join('');

Expand All @@ -309,5 +309,5 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
`
);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,12 @@ export class BaseResolversVisitor<

// avoid checking all types recursively if we have no `mappers` defined
if (Object.keys(this.config.mappers).length > 0) {
typeNames.forEach(typeName => {
for (const typeName of typeNames) {
if (this._shouldMapType[typeName] === undefined) {
const schemaType = allSchemaTypes[typeName];
this._shouldMapType[typeName] = this.shouldMapType(schemaType);
}
});
}
}

return typeNames.reduce((prev: ResolverTypes, typeName: string) => {
Expand Down Expand Up @@ -1171,14 +1171,13 @@ export class BaseResolversVisitor<
}
};

Object.keys(this.config.mappers)
for (const { mapper } of Object.keys(this.config.mappers)
.map(gqlTypeName => ({ gqlType: gqlTypeName, mapper: this.config.mappers[gqlTypeName] }))
.filter(({ mapper }) => mapper.isExternal)
.forEach(({ mapper }) => {
const externalMapper = mapper as ExternalParsedMapper;
const identifier = stripMapperTypeInterpolation(externalMapper.import);
addMapper(externalMapper.source, identifier, externalMapper.default);
});
.filter(({ mapper }) => mapper.isExternal)) {
const externalMapper = mapper as ExternalParsedMapper;
const identifier = stripMapperTypeInterpolation(externalMapper.import);
addMapper(externalMapper.source, identifier, externalMapper.default);
}

if (this.config.contextType.isExternal) {
addMapper(this.config.contextType.source, this.config.contextType.import, this.config.contextType.default);
Expand All @@ -1193,17 +1192,17 @@ export class BaseResolversVisitor<
addMapper(this.config.defaultMapper.source, identifier, this.config.defaultMapper.default);
}

Object.values(this._fieldContextTypeMap).forEach(parsedMapper => {
for (const parsedMapper of Object.values(this._fieldContextTypeMap)) {
if (parsedMapper.isExternal) {
addMapper(parsedMapper.source, parsedMapper.import, parsedMapper.default);
}
});
}

Object.values(this._directiveContextTypesMap).forEach(parsedMapper => {
for (const parsedMapper of Object.values(this._directiveContextTypesMap)) {
if (parsedMapper.isExternal) {
addMapper(parsedMapper.source, parsedMapper.import, parsedMapper.default);
}
});
}

return Object.keys(groupedMappers)
.map(source => buildMapperImport(source, groupedMappers[source], this.config.useTypeImports))
Expand Down
Loading

0 comments on commit 553afcd

Please sign in to comment.