Skip to content

Commit

Permalink
feat(client-preset): print warning on skipped anonymous operation (#8500
Browse files Browse the repository at this point in the history
)
  • Loading branch information
charlypoly authored Oct 20, 2022
1 parent 7c2bb60 commit 71aae7a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-windows-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': patch
---

Add warning and errors to prevent unwanted configuration
2 changes: 1 addition & 1 deletion dev-test/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ const config: CodegenConfig = {
preset: 'gql-tag-operations-preset',
plugins: [],
},
'./dev-test/gql-tag-operations/graphql': {
'./dev-test/gql-tag-operations/graphql/': {
schema: './dev-test/gql-tag-operations/schema.graphql',
documents: './dev-test/gql-tag-operations/src/**/*.ts',
preset: 'client',
Expand Down
18 changes: 14 additions & 4 deletions packages/presets/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@ export type ClientPresetConfig = {
gqlTagName?: string;
};

const isOutputFolderLike = (baseOutputDir: string) => baseOutputDir.endsWith('/');

export const preset: Types.OutputPreset<ClientPresetConfig> = {
prepareDocuments: (outputFilePath, outputSpecificDocuments) => [...outputSpecificDocuments, `!${outputFilePath}`],
buildGeneratesSection: options => {
if (!isOutputFolderLike(options.baseOutputDir)) {
throw new Error('[client-preset] target output should be a directory, ex: "src/gql/"');
}

if (options.plugins.length > 0) {
throw new Error('[client-preset] providing `plugins` with `preset: "client" leads to duplicated generated types');
}

const reexports: Array<string> = [];

// the `client` preset is restricting the config options inherited from `typescript`, `typescript-operations` and others.
Expand Down Expand Up @@ -139,7 +149,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
reexports.push('fragment-masking');

fragmentMaskingFileGenerateConfig = {
filename: `${options.baseOutputDir}/fragment-masking${fragmentMaskingArtifactFileExtension}`,
filename: `${options.baseOutputDir}fragment-masking${fragmentMaskingArtifactFileExtension}`,
pluginMap: {
[`fragment-masking`]: fragmentMaskingPlugin,
},
Expand All @@ -163,7 +173,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {

if (reexports.length) {
indexFileGenerateConfig = {
filename: `${options.baseOutputDir}/index.ts`,
filename: `${options.baseOutputDir}index.ts`,
pluginMap: {
[`add`]: addPlugin,
},
Expand All @@ -182,7 +192,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {

return [
{
filename: `${options.baseOutputDir}/graphql.ts`,
filename: `${options.baseOutputDir}graphql.ts`,
plugins,
pluginMap,
schema: options.schema,
Expand All @@ -193,7 +203,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
documents: sources,
},
{
filename: `${options.baseOutputDir}/gql${gqlArtifactFileExtension}`,
filename: `${options.baseOutputDir}gql${gqlArtifactFileExtension}`,
plugins: genDtsPlugins,
pluginMap,
schema: options.schema,
Expand Down
8 changes: 7 additions & 1 deletion packages/presets/client/src/process-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export function processSources(sources: Array<Source>, buildName: BuildNameFunct
for (const definition of document?.definitions ?? []) {
if (definition?.kind !== `OperationDefinition` && definition?.kind !== 'FragmentDefinition') continue;

if (definition.name?.kind !== `Name`) continue;
if (definition.name?.kind !== `Name`) {
if (definition?.kind === `OperationDefinition`) {
// eslint-disable-next-line no-console
console.warn(`[client-preset] the following anonymous operation is skipped: ${source.rawSDL}`);
}
continue;
}

operations.push({
initialName: buildName(definition),
Expand Down
30 changes: 15 additions & 15 deletions packages/presets/client/tests/client-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { executeCodegen } from '@graphql-codegen/cli';
import { mergeOutputs } from '@graphql-codegen/plugin-helpers';
import '@graphql-codegen/testing';
import { validateTs } from '@graphql-codegen/testing';
import { readFileSync } from 'fs';
import * as fs from 'fs';
import path from 'path';
import { preset } from '../src/index.js';

Expand All @@ -20,7 +20,7 @@ describe('client-preset', () => {
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -76,7 +76,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-lowercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -132,7 +132,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/crlf-operation.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -176,7 +176,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -271,7 +271,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/duplicate-operation.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -350,7 +350,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
presetConfig: {
Expand Down Expand Up @@ -406,7 +406,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
presetConfig: {
Expand All @@ -432,7 +432,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
presetConfig: {
Expand Down Expand Up @@ -541,7 +541,7 @@ export * from "./fragment-masking"`);
],
documents: docPath,
generates: {
out1: {
'out1/': {
preset,
plugins: [],
presetConfig: {
Expand All @@ -553,7 +553,7 @@ export * from "./fragment-masking"`);

const content = mergeOutputs([
...result,
readFileSync(docPath, 'utf8'),
fs.readFileSync(docPath, 'utf8'),
`
function App(props: { data: FooQuery }) {
const fragment: FooFragment | null | undefined = useFragment(Fragment, props.data.foo);
Expand Down Expand Up @@ -581,7 +581,7 @@ export * from "./fragment-masking"`);
],
documents: docPath,
generates: {
out1: {
'out1/': {
preset,
plugins: [],
presetConfig: {
Expand All @@ -593,7 +593,7 @@ export * from "./fragment-masking"`);

const content = mergeOutputs([
...result,
readFileSync(docPath, 'utf8'),
fs.readFileSync(docPath, 'utf8'),
`
function App(props: { data: FoosQuery }) {
const fragments: ReadonlyArray<FooFragment> | null | undefined = useFragment(Fragment, props.data.foos);
Expand All @@ -618,7 +618,7 @@ export * from "./fragment-masking"`);
],
documents: path.join(__dirname, 'fixtures/simple-uppercase-operation-name.ts'),
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down Expand Up @@ -675,7 +675,7 @@ export * from "./fragment-masking.js"`);
`,
],
generates: {
out1: {
'out1/': {
preset,
plugins: [],
},
Expand Down

0 comments on commit 71aae7a

Please sign in to comment.