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

Unable to limit types that are generated #167

Open
davidvc opened this issue Jul 28, 2024 · 16 comments
Open

Unable to limit types that are generated #167

davidvc opened this issue Jul 28, 2024 · 16 comments

Comments

@davidvc
Copy link

davidvc commented Jul 28, 2024

Hello. I am trying to follow the instructions documented here to limit the types generated. We have a very large federated supergraph (sorry I am not able to share it publicly), and I just want the code necessary for a specific mutation.

Here is what the pom configuration looks like for the plugin:

	<configuration>
					<generateClientApiV2>true</generateClientApiV2>
					<includeMutations>
						<param>createListingDraft</param>
					</includeMutations>
					<includeQueries>
					</includeQueries>
					<includeSubscriptions>
					</includeSubscriptions>
					<skipEntityQueries>true</skipEntityQueries>
					<maxProjectionDepth>2</maxProjectionDepth>
					<generateDataTypes>false</generateDataTypes>
					<schemaPaths>
						<param>src/main/resources/schema/full-schema.graphqls</param>
					</schemaPaths>
				</configuration>

I run mvn clean generate-sources and it generates code for every single type, input type, query, mutation and enum in the entire schema.

Here is the output provided by your plugin for the call made to the core codegen library:

[INFO] Codegen config: 
            --output-dir=/Users/dvancouvering/projects/dvancouvering/lps/lpsService/target/generated-sources
            --package-name=com.ebay.app.generated.schema
            --sub-package-name-client=client
            --sub-package-name-datafetchers=datafetchers
            --sub-package-name-types=types
            --sub-package-name-docs=docs
            
            --write-to-disk
            --language=JAVA
            
            --skip-generate-data-types
            
            --include-mutation=createListingDraft
            --skip-entities

This all looks correct according to the documentation. So I'm not sure what I'm doing wrong here. It makes the codegen essentialy unusable because such a massive amount of code is being generated. There should be about 25 types generated, instead I get around 500.

Thanks!

@davidvc
Copy link
Author

davidvc commented Jul 28, 2024

Sorry, forgot to mention, I am using version 1.50 of your plugin. Thanks.

			<plugin>
				<groupId>io.github.deweyjose</groupId>
				<artifactId>graphqlcodegen-maven-plugin</artifactId>
				<version>1.50</version>

@deweyjose
Copy link
Owner

Hi @davidvc - I will take a look tonight and follow up.

@davidvc
Copy link
Author

davidvc commented Jul 30, 2024 via email

@deweyjose
Copy link
Owner

I've created a PR example repo run some experiments.

With the following configuration

<configuration>
                    <packageName>com.acme</packageName>
                    <generateClientApiV2>true</generateClientApiV2>
                    <includeMutations>
                        <param>addPage</param>
                    </includeMutations>
                    <includeQueries></includeQueries>
                    <includeSubscriptions></includeSubscriptions>
                    <skipEntityQueries>true</skipEntityQueries>
                    <maxProjectionDepth>2</maxProjectionDepth>
                    <generateDataTypes>false</generateDataTypes>
                    <schemaPaths>
                        <param>src/main/resources/schema/schema.graphqls</param>
                    </schemaPaths>
                </configuration>

I see the following output (similar to yours):

--output-dir=/Users/dewey/repos/graphqlcodegen-example/client/target/generated-sources
--package-name=com.acme
--sub-package-name-client=client
--sub-package-name-datafetchers=datafetchers
--sub-package-name-types=types
--sub-package-name-docs=docs

--write-to-disk
--language=JAVA

--skip-generate-data-types

--include-mutation=addPage
--skip-entities

And I see the following generated client code:

image

By types did you mean query, projection root etc in the client package?

@deweyjose
Copy link
Owner

deweyjose commented Aug 3, 2024

I think what's probably happening is the <includeQueries> configuration is not behaving as expected. Leaving it emptyt like that is essentially telling the plugin to generate all queries, which will pull in all the types associated with them.

By adding the following to my configuration:

<includeMutations>
  <param>addBook</param>
</includeMutations>
<includeQueries>
  <param>""</param>
</includeQueries>

The generated code is much smaller for me now. I also updated the mutation to accept an input type to verify types only has the type required by the single mutation.

schema:

type Query {
    books: [Book]
    publishers: [Publisher]
    authors: [Author]
}

type Mutation {
    addPage(bookId: ID!, number: Int!, content: String!): Page
    addPublisher(name: String!): Publisher
    addAuthor(name: String!): Author
    addBook(book: BookInput!): Book
}

input BookInput {
    title: String!
}

type Book {
    id: ID!
    title: String!
    pages: [Page]
}

type Page {
    id: ID!
    number: Int!
    content: String!
}

type Publisher {
    id: ID!
    name: String!
    books: [Book]
}

type Author {
    id: ID!
    name: String!
    books: [Book]
}

image

@deweyjose
Copy link
Owner

deweyjose commented Aug 3, 2024

@davidvc Could you try adding <param>""</param> to each of the include lists you want to disable. This is more inline with the gradle example you referenced above.

The client code generation that uses the config object constructed by input params (both this maven plugin and the DGS gradle plugin) needs something in the array to filter against. If the array is empty there is no filter.

@davidvc
Copy link
Author

davidvc commented Aug 3, 2024 via email

@deweyjose
Copy link
Owner

Hi @davidvc just following up to see if you're all set. Would like to close this issue if so.

@davidvc
Copy link
Author

davidvc commented Aug 19, 2024 via email

@davidvc
Copy link
Author

davidvc commented Aug 19, 2024

Yep, that worked! I also got a much smaller set! Thanks so much!

@davidvc
Copy link
Author

davidvc commented Aug 19, 2024

Do you want to keep this open, since this is just a workaround?

Or is the bug in the core codegen repo?

@deweyjose
Copy link
Owner

I'm inclined to just close and highlight the subtleties in the README.

IMO <param>""</param> is aligned with how the gradle plugin managed by Netflix behaves and is documented.

image

@davidvc
Copy link
Author

davidvc commented Aug 19, 2024 via email

@davidvc
Copy link
Author

davidvc commented Aug 20, 2024

I'm not sure what happened. Perhaps I didn't test with full schema like I thought I did. With this configuration I got the following output, and 2,880 types were generated from a 78,000 line supergraph :(

				<configuration>
					<schemaPaths>
						<param>src/main/resources/schema/supergraph.graphqls</param>
					</schemaPaths>
					<packageName>com.ebay.app.generated.schema</packageName>
					<typeMapping>
						<Long>java.lang.Long</Long>
						<Decimal>java.math.BigDecimal</Decimal>
						<CountryCode>java.lang.String</CountryCode>
						<CurrencyCode>com.ebay.cos.type.v3.base.CurrencyCodeEnum</CurrencyCode>
						<Time>java.time.OffsetTime</Time>
						<Date>java.time.OffsetDateTime</Date>
					</typeMapping>
					<generateClientApiV2>true</generateClientApiV2>
					<skipEntityQueries>true</skipEntityQueries>
					<includeMutations>
						<param>createListingDraft</param>
					</includeMutations>
					<includeQueries>
						<param>""</param>
					</includeQueries>
				</configuration>
			</plugin>

Here's the debug output

[INFO] Codegen config: 
            --output-dir=/Users/dvancouvering/projects/dvancouvering/lps/lpsService/target/generated-sources
            --package-name=com.ebay.app.generated.schema
            --sub-package-name-client=client
            --sub-package-name-datafetchers=datafetchers
            --sub-package-name-types=types
            --sub-package-name-docs=docs
            
            --write-to-disk
            --language=JAVA
            
            --generate-data-types
            --include-query=""
            --include-mutation=createListingDraft
            --skip-entities

@davidvc
Copy link
Author

davidvc commented Aug 20, 2024

I just saw generate-data-type was true. I changed it to false, same result, all types generated. I'm sure I'm missing something obvious :(

[INFO] Codegen config: 
            --output-dir=/Users/dvancouvering/projects/dvancouvering/lps/lpsService/target/generated-sources
            --package-name=com.ebay.app.generated.schema
            --sub-package-name-client=client
            --sub-package-name-datafetchers=datafetchers
            --sub-package-name-types=types
            --sub-package-name-docs=docs
            
            --write-to-disk
            --language=JAVA
            
            --skip-generate-data-types
            --include-query=""
            --include-mutation=createListingDraft
            --skip-entities
            --type-mapping CountryCode=java.lang.String
--type-mapping CurrencyCode=com.ebay.cos.type.v3.base.CurrencyCodeEnum
--type-mapping Date=java.time.OffsetDateTime
--type-mapping Decimal=java.math.BigDecimal
--type-mapping Long=java.lang.Long
--type-mapping Time=java.time.OffsetTime 

@deweyjose
Copy link
Owner

No worries, @davidvc - any chance you can test with a newer version of the plugin?

I'll try again tonight to make sure I wasn't missing something in my config as well.

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

No branches or pull requests

2 participants