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

Add option to configure customTypesMapping beyond scalars to configure custom mappings for types #1307

Open
yeikel opened this issue Aug 16, 2023 · 2 comments

Comments

@yeikel
Copy link
Contributor

yeikel commented Aug 16, 2023

Is your feature request related to a problem? Please describe.

I would like to provide custom mappings for other types that are not scalar where code generation would be skipped but my class would still be referenced

Example :

Schema :

type Book {
  title: String
  author : Author
}

type Author {
  name: String
  book : Book
}

Describe the solution you'd like

Build configuration :

<plugin>
                <groupId>io.github.kobylynskyi</groupId>
                <artifactId>graphql-codegen-maven-plugin</artifactId>
                <version>${graphql-codegen.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources-product-client</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <graphqlSchemas>
                                <includePattern>schema.graphql</includePattern>
                            </graphqlSchemas>
                            <outputDir>${project.build.directory}/generated-sources/client</outputDir>
                            <modelPackageName>io.github.kobylynskyi.product.graphql.model</modelPackageName>
                            <generateApis>false</generateApis>
                            <generateClient>true</generateClient>
                            <generateAllMethodInProjection>false</generateAllMethodInProjection>
                            <customTypesMapping>
                                <Author>org.example.Author</Author>
                            </customTypesMapping>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Generated Class :

It should generate the Book class and use org.example.Author as is when it gets to the generation of the Author class

package io.github.kobylynskyi.product.graphql.model;

import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer;
import java.util.StringJoiner;

@javax.annotation.processing.Generated(
    value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
    date = "2023-08-16T21:06:56-0400"
)
public class Book implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private String title;
    private org.example.Author author;

    public Book() {
    }

    public Book(String title, org.example.Author author) {
        this.title = title;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public org.example.Author getAuthor() {
        return author;
    }
    public void setAuthor(org.example.Author author) {
        this.author = author;
    }


    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ", "{ ", " }");
        if (title != null) {
            joiner.add("title: " + GraphQLRequestSerializer.getEntry(title));
        }
        if (author != null) {
            joiner.add("author: " + GraphQLRequestSerializer.getEntry(author));
        }
        return joiner.toString();
    }

}

Provided Author class created externally :

package org.example;

import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer;
import io.github.kobylynskyi.product.graphql.model.Book;

import java.util.StringJoiner;

public class Author implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    private Book book;

    public Author() {
    }

    public Author(String name, Book book) {
        this.name = name;
        this.book = book;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }


    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ", "{ ", " }");
        if (name != null) {
            joiner.add("name: " + GraphQLRequestSerializer.getEntry(name));
        }
        if (book != null) {
            joiner.add("book: " + GraphQLRequestSerializer.getEntry(book));
        }
        return joiner.toString();
    }

}

Describe alternatives you've considered

I considered #1297 although I believe that this will only exclude the generation but it won't import my custom class

@yeikel yeikel changed the title customTypesMapping beyond scalars Add option to configure customTypesMapping beyond scalars to configure custom mappings for types Aug 16, 2023
@kobylynskyi
Copy link
Owner

Please provide more detailed example including the configuration and expected generated files.

@yeikel
Copy link
Contributor Author

yeikel commented Aug 17, 2023

@kobylynskyi I updated the examples

Interestingly enough, I just realized that for this basic example, the generated code is referring to org.example.Author as intended, but it is still generating the Author class regardless even when that's not desired

If this is the intended behavior maybe #1297 will help?

I pushed https://github.com/yeikel/graphql-java-codegen-example-custom/tree/main with a reproducer

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