DGS Code Generation plugin generate only the types for the schema

2.8k Views Asked by At

I'm a novice to GraphQL. Currently I'm trying to generate a GraphQL client with Netflix's DGS framework. I used the schema of SWAPI as my Domain Graph Service’s GraphQL schema file. But the tool only generates only the Classes and Interfaces related to types. But do not generate the query API as expected. This is my build.gradle file

plugins {
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id("com.netflix.dgs.codegen") version "5.1.2"
}

group = 'com.clients.netflix'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.5.5'
    developmentOnly 'org.springframework.boot:spring-boot-devtools:2.5.5'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.5'
    implementation 'com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter:4.8.3'
}

test {
    useJUnitPlatform()
}

generateJava {
    generateClient = true
    generateDataTypes = true
}

Is there a correct way to do this or is it a problem in the framework?

2

There are 2 best solutions below

0
On

That is the correct behavior. As you can read in the documentation the generator generates exmaple APIs!

The DGS Code Generation plugin generates code during your project’s build process based on your Domain Graph Service’s GraphQL schema file. The plugin generates the following: Data types for types, input types, enums and interfaces. A DgsConstants class containing the names of types and fields Example data fetchers A type safe query API that represents your queries

DOC

The generator does not know how you want to fetch data from your DB or what ever.

If you want to generate the examples you must add

generateJava {
  ...
  includeQueries = ["hello"]
  includeMutations = [""]
}
0
On

Just stumbled across the same issue!

I think there's a small discrepancy in how the Query / Mutation / Subscription types are defined in the GQL spec, and subsequently handled in the DGS library:

private fun generateJavaClientApi(definitions: Collection<Definition<*>>): CodeGenResult {
    return if (config.generateClientApi) {
        definitions.asSequence()
            .filterIsInstance<ObjectTypeDefinition>()
            .filter { it.name == "Query" || it.name == "Mutation" || it.name == "Subscription" }
            .map { ClientApiGenerator(config, document).generate(it) }
            .fold(CodeGenResult()) { t: CodeGenResult, u: CodeGenResult -> t.merge(u) }
    } else CodeGenResult()
}

and how they are specified in the swapi gql schema:

schema {
  query: Root
}

Whoops! Renaming the object type definition to Query would fix the problem. Imo this is a bug with the swapi schema though...