graphql-java-codegen icon indicating copy to clipboard operation
graphql-java-codegen copied to clipboard

Add option to configure `customTypesMapping` beyond scalars to configure custom mappings for types

Open yeikel opened this issue 10 months ago • 2 comments

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 https://github.com/kobylynskyi/graphql-java-codegen/issues/1297 although I believe that this will only exclude the generation but it won't import my custom class

yeikel avatar Aug 16 '23 00:08 yeikel