Skip to content

added typesPrefix config #45

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

Merged
merged 2 commits into from
Apr 18, 2022
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ import { GeneratedInput } from './graphql'
/* generates validation schema here */
```

### `typesPrefix`

type: `string` default: (empty)

Prefixes all import types from generated typescript type.

```yml
generates:
path/to/graphql.ts:
plugins:
- typescript
path/to/validation.ts:
plugins:
- typescript-validation-schema
config:
typesPrefix: I
importFrom: ./graphql # path for generated ts code
```

Then the generator generates code with import statement like below.

```ts
import { IGeneratedInput } from './graphql'

/* generates validation schema here */
```

### `enumsAsTypes`

type: `boolean` default: `false`
Expand Down
19 changes: 19 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
* ```
*/
importFrom?: string;
/**
* @description Prefixes all import types from generated typescript type.
* @default ""
*
* @exampleMarkdown
* ```yml
* generates:
* path/to/types.ts:
* plugins:
* - typescript
* path/to/schemas.ts:
* plugins:
* - graphql-codegen-validation-schema
* config:
* typesPrefix: I
* importFrom: ./path/to/types
* ```
*/
typesPrefix?: string;
/**
* @description Generates validation schema for enum as TypeScript `type`
* @default false
Expand Down
19 changes: 19 additions & 0 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ describe('myzod', () => {
expect(result.content).toContain(wantContain);
}
});
it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
phrase: String!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'myzod',
typesPrefix: 'I',
importFrom: './types',
},
{}
);
expect(result.prepend).toContain("import { ISay } from './types'");
expect(result.content).toContain('export function ISaySchema(): myzod.Type<ISay> {');
});
describe('issues #19', () => {
it('string field', async () => {
const schema = buildSchema(/* GraphQL */ `
Expand Down
19 changes: 19 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,23 @@ describe('yup', () => {
expect(result.content).toContain(wantContain);
}
});

it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
phrase: String!
}
`);
const result = await plugin(
schema,
[],
{
typesPrefix: 'I',
importFrom: './types',
},
{}
);
expect(result.prepend).toContain("import { ISay } from './types'");
expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf<ISay> {');
});
});
20 changes: 20 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ describe('zod', () => {
expect(result.content).toContain(wantContain);
}
});

it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
phrase: String!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
typesPrefix: 'I',
importFrom: './types',
},
{}
);
expect(result.prepend).toContain("import { ISay } from './types'");
expect(result.content).toContain('export function ISaySchema(): z.ZodObject<Properties<ISay>> {');
});
describe('issues #19', () => {
it('string field', async () => {
const schema = buildSchema(/* GraphQL */ `
Expand Down