Skip to content

Commit

Permalink
fix(other): Makes proptype resolution behave like fieldtype
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
defaults `input` and `output` in PropertyType to false
  • Loading branch information
benkroeger authored and unlight committed Mar 15, 2022
1 parent dec9203 commit 850018a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/helpers/object-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,27 @@ export class ObjectSettings extends Array<ObjectSetting> {
return fieldType;
}

getPropertyType({ name }: ObjectSettingsFilterArgs): ObjectSetting | undefined {
getPropertyType({
name,
input,
output,
}: ObjectSettingsFilterArgs): ObjectSetting | undefined {
const propertyType = this.find(s => s.kind === 'PropertyType');

if (!propertyType) {
return undefined;
}

// eslint-disable-next-line unicorn/prefer-regexp-test
if (propertyType.match && !propertyType.match(name)) {
if (propertyType.match) {
// eslint-disable-next-line unicorn/prefer-regexp-test
return propertyType.match(name) ? propertyType : undefined;
}

if (input && !propertyType.input) {
return undefined;
}

if (output && !propertyType.output) {
return undefined;
}

Expand Down
66 changes: 65 additions & 1 deletion src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ describe('emit single and decorators', () => {
id Int @id
/// @Validator.MinLength(3)
name String
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email', input: true })
email String?
}
`,
Expand Down Expand Up @@ -2025,6 +2025,70 @@ describe('property type', () => {
expect(p('profile')?.type).toEqual('JsonObject');
});
});

describe('it respects input from generator level field configuration', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id Int @id
/// @PropertyType('JsonObject')
profile Json
}
`,
options: [
`
fields_JsonObject_from = "type-fest"
fields_JsonObject_namedImport = true
fields_JsonObject_input = true
fields_JsonObject_output = false
`,
],
}));
});

it('should use default scalar type in model', () => {
setSourceFile('user.model.ts');
expect(p('profile')?.type).toEqual('any');
});

it('user-create.input', () => {
setSourceFile('user-create.input.ts');
expect(p('profile')?.type).toEqual('JsonObject');
});
});

describe('it respects output from generator level field configuration', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id Int @id
/// @PropertyType('JsonObject')
profile Json
}
`,
options: [
`
fields_JsonObject_from = "type-fest"
fields_JsonObject_namedImport = true
fields_JsonObject_input = false
fields_JsonObject_output = true
`,
],
}));
});

it('should use default scalar type in model', () => {
setSourceFile('user.model.ts');
expect(p('profile')?.type).toEqual('JsonObject');
});

it('user-create.input', () => {
setSourceFile('user-create.input.ts');
expect(p('profile')?.type).toEqual('any');
});
});
});

describe('hidefield on groupby output', () => {
Expand Down

0 comments on commit 850018a

Please sign in to comment.