0

I'm trying to convert a working Maven project to Gradle and I'm having issues while trying to convert GraphQL-Kotlin configuration which requires a schema provided by another jar dependency.

I think this might not even be GraphQL-Kotlin specific, I simply don't know how to tell Gradle to use a file form another jar while trying to run a plugin task.

This is what I'm doing in my build.gradle.kts. Notice schemaFile param which accepts a file with a relative path - graphql/schemas/some-service/schema.graphql. How do you load it from com.company.schemasgraphql:some-service:1.1.0 dependency in gradle ktl?

plugins {
    kotlin("jvm") version "1.9.0"
    ...
    id("com.expediagroup.graphql") version "6.5.3"
}

dependencies {
    compileOnly("com.company.schemasgraphql:some-service:1.1.0")
    ...
}

graphql {
    client {
        packageName = "com.company.someservice"
        schemaFile = file("graphql/schemas/some-service/schema.graphql")
        queryFileDirectory = "${project.projectDir}/src/main/resources/queries"
        allowDeprecatedFields = true
    }
}

Above configuration fails with:

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':some-service:graphqlGenerateClient' (type 'GraphQLGenerateClientTask').
  - In plugin 'com.expediagroup.graphql' type 'com.expediagroup.graphql.plugin.gradle.tasks.GraphQLGenerateClientTask' property 'schemaFile' specifies file '(...)graphql/schemas/some-service/schema.graphql' which doesn't exist.

    Reason: An input file was expected to be present but it doesn't exist.

However, this simply works on Maven. Notice Maven declares a plugin dependency with the schema file in a relative format:

<plugin>
    <groupId>com.expediagroup</groupId>
    <artifactId>graphql-kotlin-maven-plugin</artifactId>
    <version>${graphql-kotlin.version}</version>
    <dependencies>
        <dependency>
            <groupId>com.company.schemasgraphql</groupId>
            <artifactId>some-service</artifactId>
            <version>${some-service.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>generate-some-service-client</id>
            <goals>
                <goal>generate-client</goal>
            </goals>
            <configuration>
                <packageName>com.company.someservice</packageName>
                <schemaFile>graphql/schemas/some-service/schema.graphql</schemaFile>
                <queryFileDirectory>
                    ${project.basedir}/src/main/resources/queries
                </queryFileDirectory>
                <allowDeprecatedFields>true</allowDeprecatedFields>
            </configuration>
        </execution>
    </executions>
</plugin>
aSemy
  • 5,485
  • 2
  • 25
  • 51
Mike Minicki
  • 8,216
  • 11
  • 39
  • 43

1 Answers1

0

This works but I'm not sure if this is the best (or easiest) way to achieve it:

val graphqlSchema = configurations.create("graphqlSchema")

dependencies {
    graphqlSchema("com.company.schemasgraphql:some-service:1.1.0")
}

tasks.register<Copy>("copyGraphQLSchema") {
    from(zipTree(configurations["graphqlSchema"].singleFile).matching {
        include("graphql/schemas/some-service/schema.graphql")
    })
    into("$buildDir")
}

graphql {
    client {
        packageName = "com.company.someservice"
        schemaFile = file("${buildDir}/graphql/schemas/some-service/schema.graphql")
        queryFileDirectory = "${project.projectDir}/src/main/resources/queries"
        allowDeprecatedFields = true
    }
}

tasks.named("graphqlGenerateClient") {
    dependsOn("copyGraphQLSchema")
}
Mike Minicki
  • 8,216
  • 11
  • 39
  • 43