Netflix / dgs-codegen

Apache License 2.0
178 stars 92 forks source link

typeMapping does no import types to graphql schema #383

Closed EugenMayer closed 2 years ago

EugenMayer commented 2 years ago

Since we are new to the codegen project (not to DGS itself) i'am trying to first state my motivation (not that it is an issue of expectations here).

My motivation is, that i have Java Types (mostly DTO's and Enums) which are used outside of GraphQL / DGS too, thus those are defined in the java project itself. That said, i want to use those types in GraphQL without redefining them (especially an issue for enums). So basically, generated graphql types from java types (dry DTOs and Enums).

For that we expected that using typeMapping would import / declare a java type for graphql, so for example for Enums it will create a EnumTypeDefinition which is then used by the graphql validator.

This seems not to work right now.

Steps to reproduce

type Query {
   appleKinds: `ApplesKindsDTO`
}

And now the java types

package
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApplesKindsDTO {
   List<AppleKind> kinds;
}

And the enum

public enum AppleKind {
  PinkLady,
  SquareOnes,
  TriangleOnes
}

Finally we configure those types to be mapped

typeMapping = [
  "ApplesKindsDTO": "org.example.dto.ApplesKindsDTO",
  "AppleKind": "org.example.AppleKind"
]

Now when just running/smoke testing the application, the DGS validation will fail with

Caused by: SchemaProblem{errors=[The field type 'ApplesKindsDTO' is not present when resolving type 'ApplesKindsDTO' [@1:1]]}
    at graphql.schema.idl.SchemaGenerator.makeExecutableSchema(SchemaGenerator.java:82)
    at com.apollographql.federation.graphqljava.Federation.transform(Federation.java:50)
    at com.netflix.graphql.dgs.internal.DgsSchemaProvider.schema(DgsSchemaProvider.kt:145)
    at com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration.schema(DgsAutoConfiguration.kt:192)
srinivasankavitha commented 2 years ago

The type mapping is used to specify the graphql type (as defined in the schema) to map to corresponding Java type. In the example you provided, "ApplesKindsDTO" is not in your schema?

Note that codegen generates POJOs based on the types in the graphQL schema, so you will need that.

EugenMayer commented 2 years ago

thank you @srinivasankavitha for re-approving that we mis-understood codegen and actually had to learn that we need to integrate a code 2 schema library for types in our project in addition to DGS, since DGS is schema 2 code.

Thank you for helping out!