-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Mappers (in typescript-resolvers template) #757
Conversation
@@ -1,6 +1,6 @@ | |||
import { Field, Interface, Type } from 'graphql-codegen-core'; | |||
import { set } from 'lodash'; | |||
import { getResultType } from '../../../typescript/src/utils/get-result-type'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed we use relative path instead of a package
@@ -16,7 +16,8 @@ | |||
"graphql-codegen-core": "0.12.6" | |||
}, | |||
"dependencies": { | |||
"lodash": "4.17.11" | |||
"lodash": "4.17.11", | |||
"graphql-codegen-typescript-template": "0.12.6" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it as a dependency because mongodb template depends on it
@@ -9,8 +9,10 @@ | |||
"pretest": "yarn build", | |||
"test": "codegen-templates-scripts test" | |||
}, | |||
"dependencies": { | |||
"graphql-codegen-typescript-template": "0.12.6" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, we depend on it
[path: string]: string[]; | ||
} | ||
|
||
export function importMappers(types: Type[], options: Handlebars.HelperOptions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't emit unused types and there's no duplicates
source?: string; | ||
} | ||
|
||
function isExternal(value: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this says if Mapper comes from external module
return realType; | ||
} else { | ||
return [realType, 'null'].join(' | '); | ||
} | ||
} | ||
} | ||
|
||
export function convertedType(type: Field, options: Handlebars.HelperOptions, skipPascalCase = false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resolvers template needs to share that logic
export function getResultType(type: Field, options: Handlebars.HelperOptions, skipPascalCase = false) { | ||
const baseType = type.type; | ||
const underscorePrefix = type.type.match(/^[\_]+/) || ''; | ||
export function getFieldType(field: Field, realType: string, options: Handlebars.HelperOptions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted that because we need that logic in resolvers-template but without prefixes and stuff (things that convertedType has)
@@ -424,4 +424,148 @@ describe('Resolvers', () => { | |||
export type UserNameResolver<R = string | null, Parent = User, Context = any> = Resolver<R, Parent, Context>; | |||
`); | |||
}); | |||
|
|||
it('should accept a map of parent types', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it shows that fields and types get a mapper both in Result and Parent generics
`); | ||
}); | ||
|
||
it('should accept mappers that reuse generated types', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I show that we can reuse generated types and even use primitives or even ts things like Pick<>
or Exclude<>
I think tests show exactly what we want to achieve.