Netflix / dgs-codegen

Apache License 2.0
177 stars 92 forks source link

Error generating classes from generateClientApi or generateClientApiV2 #692

Open annarafaeladev opened 1 month ago

annarafaeladev commented 1 month ago

Error generating classes from generateClientApi or generateClientApiV2.

Constructors with invalid number of arguments.

schemas:

type Query {
    shows: [Show]
}

type Show {
    title: String,
    id: String
}

I'm using graphqlcodegen-maven-plugin to generate the code. When I run mvn clean install the command generates the classes but with an error:

file: ShowsProjectionRoot.java

package com.graphql.engine.codegen.client;

import com.netflix.graphql.dgs.client.codegen.BaseSubProjectionNode;
import javax.annotation.processing.Generated;

@Generated(
    value = "com.netflix.graphql.dgs.codegen.CodeGen",
    date = "2024-05-21T18:53:11.998886400Z"
)
@com.graphql.engine.codegen.Generated
public class ShowsProjectionRoot<PARENT extends BaseSubProjectionNode<?, ?>, ROOT extends BaseSubProjectionNode<?, ?>> extends BaseSubProjectionNode<PARENT, ROOT> {
  public ShowsProjectionRoot() {
    super(null, null, java.util.Optional.of("Show")); //current  line with the error because the constructor only accepts two argument
  }

  public ShowsProjectionRoot<PARENT, ROOT> title() {
    getFields().put("title", null);
    return this;
  }

  public ShowsProjectionRoot<PARENT, ROOT> ano() {
    getFields().put("ano", null);
    return this;
  }
}

this is error: constructor BaseSubProjectionNode in class com.netflix.graphql.dgs.client.codegen.BaseSubProjectionNode<T,R> cannot be applied to given types;

other file ShowsGraphQLQuery with error:

package com.graphql.engine.codegen.client;

import com.netflix.graphql.dgs.client.codegen.GraphQLQuery;
import java.lang.Override;
import java.lang.String;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.Generated;

@Generated(
    value = "com.netflix.graphql.dgs.codegen.CodeGen",
    date = "2024-05-21T18:53:11.998886400Z"
)
@com.graphql.engine.codegen.Generated
public class ShowsGraphQLQuery extends GraphQLQuery {
  public ShowsGraphQLQuery(String queryName) {
    super("query", queryName); // current  line with the error because the constructor only accepts one argument
  }

  public ShowsGraphQLQuery() {
    super("query");
  }

  @Override
  public String getOperationName() {
     return "shows";

  }

  public static Builder newRequest() {
    return new Builder();
  }

  @Generated(
      value = "com.netflix.graphql.dgs.codegen.CodeGen",
      date = "2024-05-21T18:53:11.998886400Z"
  )
  @com.graphql.engine.codegen.Generated
  public static class Builder {
    private Set<String> fieldsSet = new HashSet<>();

    private String queryName;

    public ShowsGraphQLQuery build() {
      return new ShowsGraphQLQuery(queryName);                                     
    }

    public Builder queryName(String queryName) {
      this.queryName = queryName;
      return this;
    }
  }
}

ERROR: no suitable constructor found for GraphQLQuery(java.lang.String,java.lang.String)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.graphql</groupId>
    <artifactId>engine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>engine</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <!-- DGS dependencies. We don't have to specify a version here! -->
        <dependency>
            <groupId>com.netflix.graphql.dgs</groupId>
            <artifactId>graphql-dgs-spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.netflix.graphql.dgs</groupId>
                <artifactId>graphql-dgs-platform-dependencies</artifactId>
                <!-- The DGS BOM/platform dependency. This is the only place you set version of DGS -->
                <version>3.10.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>io.github.deweyjose</groupId>
                <artifactId>graphqlcodegen-maven-plugin</artifactId>
                <version>1.50</version>
                <executions>
                    <execution>
                        <id>dgs-codegen</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaPaths>
                                <param>src/main/resources/graphql-client</param>
                            </schemaPaths>
                            <packageName>com.graphql.engine.codegen</packageName>
                            <addGeneratedAnnotation>true</addGeneratedAnnotation>
                            <generateBoxedTypes>true</generateBoxedTypes>
                            <generateDataTypes>true</generateDataTypes>
                            <generateClientApiV2>true</generateClientApiV2>
                            <writeToFiles>true</writeToFiles>
                            <generateDocs>true</generateDocs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-dgs-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
kailyak commented 1 month ago

Hi @annarafaeladev. What version of the dgs-codegen are you using? The generateClientv2 configuration option was created to allow users to opt into the v2 API. It's still there for legacy reasons and as of v6.0.1, setting either generateClient or generateClientv2 to true will result in the v2 API being generated.

For your issue, in the latest version of the dgs-codegen gradle plugin v6.2.1, I am not able to replicate the errors with the schema provided. It may be related to the maven port, have you tried that community? That would be this github page

annarafaeladev commented 1 month ago

Maven Home version: 3.9.6 Java version: 17.0.10 codegen version: 8.6.0 The DGS BOM/platform dependency: 3.2.0

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.netflix.graphql.dgs</groupId>
            <artifactId>graphql-dgs-platform-dependencies</artifactId>
            <!-- The DGS BOM/platform dependency. This is the only place you set version of DGS -->
            <version>3.10.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

graphqlcodegen-maven-plugin version: 1.50

<plugins>
    <plugin>
        <groupId>io.github.deweyjose</groupId>
        <artifactId>graphqlcodegen-maven-plugin</artifactId>
        <version>1.50</version>
        <executions>
            <execution>
                <id>dgs-codegen</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <schemaPaths>
                        <param>src/main/resources/graphql-client/schema.graphqls</param>
                    </schemaPaths>
                    <packageName>com.graphql.engine.codegen</packageName>
                    <addGeneratedAnnotation>true</addGeneratedAnnotation>
                    <generateClientApi>true</generateClientApi>

                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>add-dgs-source</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.build.directory}/generated-sources</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                </exclude>
            </excludes>
        </configuration>
    </plugin>
</plugins>
kailyak commented 1 month ago

I noticed that the version you are using for the dgs BOM is bit outdated at v3.10.2. Right now latest is v8.6.1. Can you try upgrading to the latest version? I'm not sure if that will solve your issue but it might be a good start. Note that post v6.0.0 spring boot 3 is required.

annarafaeladev commented 1 month ago

its ok, so i updated dependencies for version 8.6.1 but the problem persist. I copied the pom.xml with dependencies correctly updated.

Versão Maven Home: 3.9.6 Versão Java: 17.0.10 Spring version: 3.2.5 Versão codegen: 8.6.1 A dependência da BOM/plataforma DGS: 8.6.1


<dependency>
    <groupId>com.netflix.graphql.dgs</groupId>
    <artifactId>graphql-dgs-spring-boot-starter</artifactId>
    <version>8.6.1</version>
</dependency>

<dependencyManagement>
   <dependencies>
    <dependency>
        <groupId>com.netflix.graphql.dgs</groupId>
        <artifactId>graphql-dgs-platform-dependencies</artifactId>
        <!-- The DGS BOM/platform dependency. This is the only place you set version of DGS -->
        <version>8.6.1</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    </dependencies>
</dependencyManagement>

ERROR after update dependency version 8.6.1

package com.netflix.graphql.dgs.client.codegen does not exist cannot find symbol

error ocurrently in line 3

image

srinivasankavitha commented 1 month ago

Per the docs here: https://netflix.github.io/dgs/generating-code-from-schema/#client-api-v2

You are missing an additional <> for the ProjectionRoot.

String query = new GraphQLQueryRequest(
        new MoviesGraphQLQuery(),
        new MoviesProjectionRoot<>().movieId()).serialize();
annarafaeladev commented 1 month ago

Sorry, I don't understand where I should add this code snippet since the classes MoviesGraphQLQuery and MoviesProjectionRoot are automatically generated by the codegen plugin inside the target folder.

Could you explain me?

And after modifying the plugin version, the codegen client import is giving a not found error

srinivasankavitha commented 1 month ago

Ok, after lookingcloser, the error is in the generated classes. My guess is that somehow you are missing the com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core in your deps which should be brought in by codegen. I am not familiar with your maven project set up, so would verify first whether you have that in your dependencies. I would also maybe try posting on the maven port project since there could be something missing there.

annarafaeladev commented 1 month ago

I added the mentioned dependency but the problem persists:

<dependency>
    <groupId>com.netflix.graphql.dgs.codegen</groupId>
    <artifactId>graphql-dgs-codegen-shared-core</artifactId>
    <version>6.2.1</version>
    <scope>runtime</scope>
</dependency>

My project is a project created in spring initializer java 17 maven with spring version 3.2.5 this is pom.xml updated:

pom.xml Details

maven

org.springframework.boot spring-boot-starter-graphql org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework spring-webflux test org.springframework.graphql spring-graphql-test test org.springframework.boot spring-boot-starter-webflux com.netflix.graphql.dgs graphql-dgs-spring-boot-starter 8.6.1 com.netflix.graphql.dgs.codegen graphql-dgs-codegen-shared-core 6.2.1 runtime com.netflix.graphql.dgs graphql-dgs-platform-dependencies 8.6.1 pom import io.github.deweyjose graphqlcodegen-maven-plugin 1.50 dgs-codegen generate src/main/resources/graphql-client/schema.graphqls com.graphql.engine.codegen true true org.codehaus.mojo build-helper-maven-plugin add-dgs-source generate-sources add-source ${project.build.directory}/generated-sources org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok

srinivasankavitha commented 1 month ago

Hi @annarafaeladev - we don't support the maven port of codegen here. Please try https://github.com/deweyjose/graphqlcodegen for help with this issue.