-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add typescript type graphql plugin #2177
Add typescript type graphql plugin #2177
Conversation
Hi @borremosch , can you please rebase this PR? |
Thank you @borremosch !!! |
Can we have a bit of documentation ? |
You can find the documentation on the graphql-codegen website here. Also, if you need some more information about the use cases of this plugin, I have written a two part article about how I am using it:
@dotansimha it seems that the link to the TypeGraphQL plugin documentation is missing from the menu on the website. Do I need to make a new pull request to include it? |
Thanks for this fast reply! Following your documentation and articles, this plugin provides class for config:
declarationKind:
type: 'class'
input: 'class' But |
Hmm I had not considered the case where you would want to reuse your input types.. I guess this could be added in a next release |
Hello, thanks for the great work on this, but I'm facing a problem with interfaces. When I set:
All my interface properties are separated by commas, as if they were types, but they should be separated by semicolons instead. |
@digeomel This plugin is for TypeGraphQL so it uses classes and decorators. Could you explain why you generate interfaces with this plugin? |
@ardatan because I need to have interfaces for my models, not types, and I didn't find another way to do it without this plugin. |
@digeomel it does not seem to be possible to use TypeGraphQL annotations with interfaces. Even if the plugin would generate properly formatter interfaces, the decorators will yield errors. If all you need is to generate interfaces, you can use the @graphql-codegen/typescript plugin for this. |
@borremosch can you please explain how to use the @graphql-codegen/typescript plugin to generate interfaces instead of the classes which is the default? I didn't find any switch/flag in the plugin documentation to allow me to generate interfaces. |
@digeomel it works the exact same way as with @graphql-codegen/typescript-type-graphql, since this plugin actually extends @graphql-codegen/typescript. You can view the documentation here: https://graphql-code-generator.com/docs/plugins/typescript#declarationkind-declarationkindconfig |
@borremosch thanks for the quick reply. I don't see what I'm doing wrong here, this is (part of) my config:
What am I doing wrong? |
Hey,
|
@borremosch |
@borremosch is right ;) |
Thanks @borremosch @dotansimha! type MyType{
myProperty: [SomeOtherType]
} Will result in: @TypeGraphQL.Field(type => \[**Maybe**\<SomeOtherType>], { nullable: true })
myProperty!: Maybe<Array<Maybe\<SomeOtherType>>>; So ts will say: 'Maybe' only refers to a type, but is being used as a value here. |
@groege if it's still relevant, can you please open an issue with a reproduction? |
Still generates commas in interfaces and linter says: "Properties should be separated by semicolons" |
Hi @digeomel, have you found a solution for the comma issue? I'm also facing this issue. |
@smfoisal |
@smfoisal sorry, just saw your comment, no, I didn't, finally gave up and we're just writing our own interfaces, duplicating code... 😞 |
@groege @dotansimha @borremosch IMO, the reason for an error So I think we can fix this simply by checking whether the // packages/plugins/typescript/type-graphql/src/visitor.ts: line 248
const maybeType = type.type.match(MAYBE_REGEX)
let arrayReturnType = `[${type.type}]`
if (maybeType) {
const [,typeNameWithoutMaybe] = maybeType
arrayReturnType = `[${typeNameWithoutMaybe}]`
}
const decorator =
'\n' +
indent(
`@TypeGraphQL.${fieldDecorator}(type => ${type.isArray ? arrayReturnType : type.type}${
type.isNullable ? ', { nullable: true }' : ''
})`
) +
'\n'; If you have a better idea, just let me know and I made PR for this. |
This is a plugin that generates type-graphql types, loosely based on the typescript plugin. See PR #2173 for more details.