Skip to content
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

Merged
merged 6 commits into from
Oct 30, 2018

Conversation

kamilkisiela
Copy link
Collaborator

{
  mappers: {
    User: './interfaces#UserParent',
    Post: 'string' // or even 'Post'
  }
}

I think tests show exactly what we want to achieve.

@@ -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';
Copy link
Collaborator Author

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"
Copy link
Collaborator Author

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"
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

@kamilkisiela kamilkisiela Oct 29, 2018

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) {
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

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 () => {
Copy link
Collaborator Author

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 () => {
Copy link
Collaborator Author

@kamilkisiela kamilkisiela Oct 29, 2018

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<>

@dotansimha dotansimha merged commit 4466f6e into master Oct 30, 2018
@dotansimha dotansimha deleted the feature/generate-parent-types branch October 30, 2018 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants