https://kotlinlang.org logo
#graphql-kotlin
Title
# graphql-kotlin
t

tim

05/13/2020, 11:06 AM
Hey folks, i'm trying to generate client classes from a schema.graphql file but the generated file is empty? I don't think my setup can get any simplier ... but any suggestions on what I'm doing wrong are appreciated 🙏 schema.graphql
Copy code
type Query {
 hello: String
}
command:
Copy code
gradle graphqlGenerateClient --schemaFileName="src/main/resources/schema.graphq" --packageName="com.example.graphql.generated"
Oh and im running 3.0.0-RC2
okay I just needed to add
--queryFileDirectory
to get it to run successfully
I'm also having to move the generated files out of a build folder to make them available to my tests ... is there a slicker way to hook it up? For example, can the generated classes be saved to a specific directory?
d

Dariusz Kuc

05/13/2020, 3:57 PM
your original command should work I think it might got confused because by default it looks for query files (i.e. files ending with
*.graphql
) under
src/main/resources
and you had the schema there
if there was an error message please send it my way 🙂
t

tim

05/13/2020, 3:58 PM
ya its working well now 🙂
d

Dariusz Kuc

05/13/2020, 3:58 PM
as for the generated files they should be automatically added to your sources so you shouldnt have to move them anywhere
currently plugin does not support custom output directories but feel free to open up a feature request (or submit pr :))
i’m working on some documentation on the client and example -> https://github.com/dariuszkuc/graphql-kotlin-client-example *disclaimer its still WIP
t

tim

05/13/2020, 4:07 PM
Ahh okay i'm not using the gradle plugin which is why its not being added in the right place I assume!
also i'm only using the client in my tests currently
but if the generated files were available inside the main src, then I could use the variables data classes off of those generated files inside my resolves
I'll check that out tomorrow actually .. i expect that's how its meant to be used?
oh i can see that the plugin is pulling the SDL from a server ... but what if i'm building the server at the same time? (which is why i'm using the client in tests). In that instance, its a chicken-and-egg aas the gradle pluging won't run because the server isnt' already started?
d

Dariusz Kuc

05/13/2020, 4:53 PM
in general I’d advise against storing generated files under
src
as you would generally want to regenerate them during your build and unless you explicitly exclude them from source control it could result in changes on every build generated files should be part of the source set so you can reference them from your main and test source files
plugin has individual goals for introspecting schema, downloading SDL and generating client - each one of those tasks can be configured independently but yes the most common use case would be get the schema and generate the client based on it
I guess you are looking into generating some integration tests for your server? yeah this is not the intended use case but it could work…. I’d suggest doing the following • generate schema file as part of the build process • use schema and test queries to generate test clients • run your integration test using generated sources (i.e. if you are using
graphql-kotlin-spring-server
you could start it up using
@SpringBootTest
and within the tests use clients to run against them)
I think it would make sense to add schema generation task to the plugin but unsure when we’ll get to it (PRs are welcome :))
In gradle you could easily custom task to do it
that leaves the part that currently generated classes are always added to the main sources -> in your use case you would want them only added to the test sources
t

tim

05/13/2020, 5:41 PM
Yep thats exactly my use case ... my tests rely on the client which relies on the generated types 🙂 I'll give what you're suggesting above a try and see if that works. We're using the library with ktor at the moment as well so can't go the spring boot route. Its currently in a decent state without any real issues (other than some generated code into the src/test). Good pt on not mixing generated code with src/main though as my approach above was suggesting on doing that but you're right I shouldn't rely on that Thanks for your help though and I'll checkout those issues tomorrow 🙏
d

Dariusz Kuc

05/13/2020, 5:51 PM
👍 i think you can also run some integration with ktor that starts up the server so basically replace the last step with that, let us know if you face any issues
fyi just released 3.0.0-RC7 includes task/goal for generating test client
t

tim

05/19/2020, 6:46 PM
wow! that was fast! I hadn't even gotten around to filling out the issues you created 🙏
We'll check it out tomorrow and let you know if we run into any issues
thank you!!
Hi @Dariusz Kuc are you sure those changes are available in RC7? Just looking now and doesn't appear to be available ...
d

Dariusz Kuc

05/20/2020, 12:09 PM
rc7 has separate generate test client task/mojo
Schema generation is still todo
Also extension applies just generate client
For test client you need to configure it explicitly
t

tim

05/20/2020, 12:19 PM
ahh k got it
d

Dariusz Kuc

05/20/2020, 12:21 PM
I'll add a note to the docs about it
t

tim

05/20/2020, 12:21 PM
ty
I think the confusing bit in the docs is that the
generateTestSources
option is shown in the build.gradle.tks extension block
I've manged to get everything working exactly as needed
no issues so thank you!
Hmm it was working fine ... but now it cannot find the generated files. From the docs it should add the generated files automatically to the test source sets right?
IntelliJ can find them correctly but gradle can't, do I need to manually add the generated folder to my test source set?
d

Dariusz Kuc

05/20/2020, 2:08 PM
i initially added option to the extension but decided against it in favor of explicit task, I possibly missed some documentation thnx I’ll take a look
t

tim

05/20/2020, 2:09 PM
ahh k
d

Dariusz Kuc

05/20/2020, 2:09 PM
yeah tasks should add generated classes to main or test sources -> you might need to reload the project
*for IDE to pick it up
t

tim

05/20/2020, 2:10 PM
okay will try that shrotly (just in a meeting now)
d

Dariusz Kuc

05/20/2020, 2:10 PM
building from command line should work as is
t

tim

05/20/2020, 3:09 PM
oddly intellij is picking up the generated files (I can browse to the generate files from my tests with CTRL + Click for example) but they're not being picked up when I run
gradle :projectName:test
via the cli ... its throwing an unresolved reference error
d

Dariusz Kuc

05/20/2020, 3:26 PM
that seems weird -> both examples (https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/client) and integration tests worked fine for me
ah
actually nvm
t

tim

05/20/2020, 3:31 PM
nearly quitting time over here ... i'll start fresh tomorrow and if I still have the issue I'll share the code
likely user error 🙂
d

Dariusz Kuc

05/20/2020, 3:32 PM
yeah it would be great if you can share the code that reproduces the issue
thnx
t

tim

05/20/2020, 3:42 PM
👍
sorry not going to get back to this today unfortunately
will aim to revist tomorrow
Hey @Dariusz Kuc I've just pushed my skeleton app here: https://github.com/glimpse-protocol/advertiser-api Still can't get
gradle test
to run without failing at the compile step. I don't understand gradle well enough to see where I've gone wrong but any help you can provide is appreciated! Basically what I am trying to do: • run
scripts/generate-gql-test.sh
• run
gradle test
And this results in a compile error as gradle cannot find the generated files. Intellij can find though.
d

Dariusz Kuc

05/25/2020, 3:53 PM
👍 i'll look into this tomorrow
t

tim

05/25/2020, 3:53 PM
ty 🙏
its likely my own fault ahah 🙂
d

Dariusz Kuc

05/26/2020, 4:15 AM
t

tim

05/26/2020, 8:03 AM
Brilliant will check it out this afternoon 🙏
Everything is working as expected now thank you!
d

Dariusz Kuc

05/26/2020, 1:26 PM
👍
t

tim

05/31/2020, 2:47 PM
Hey @Dariusz Kuc I'm running into an issue with RC7 where as soon as I add a mutation inside
src/test/resources
it fails and gradle output says "No value present". The command I'm running:
gradle graphqlGenerateTestClient --packageName="io.glimpseprotocol.generated" --schemaFileName="Schema.graphql"
So when I have this schema.graphql it works:
Copy code
type Query {
    health: String!
    version: ServiceVersion!
    test(value: Int!): Int!
}

type ServiceVersion {
    created: String!
    environment: String!
    instance: String!
    service: String!
    version: String!
}
But as soon as I add a mutation I get the "No value present" output: /Schema.graphql:
Copy code
type Query {
    health: String!
    version: ServiceVersion!
    test(value: Int!): Int!
}

type ServiceVersion {
    created: String!
    environment: String!
    instance: String!
    service: String!
    version: String!
}

type Mutation {
    testMutate: String!
}
test/resources/Mutation.graphql:
Copy code
mutation TestMutate {
    testMutate
}
Any thoughts on what I'm doing wrong here?
d

Dariusz Kuc

05/31/2020, 5:20 PM
is there some additional stack trace with the error? e.g. our example seems to be working fine https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples/client/ (it generates main sources)
t

tim

05/31/2020, 6:01 PM
Excerpt from the stack trace (not sure if the rest is useful?):
Copy code
Caused by: java.util.NoSuchElementException: No value present
        at com.expediagroup.graphql.plugin.generator.GraphQLClientGenerator.findRootType(GraphQLClientGenerator.kt:129)
        at com.expediagroup.graphql.plugin.generator.GraphQLClientGenerator.generate(GraphQLClientGenerator.kt:74)
        at com.expediagroup.graphql.plugin.GenerateClientKt.generateClient(generateClient.kt:36)
        at com.expediagroup.graphql.plugin.gradle.tasks.GraphQLGenerateClientTask.generateGraphQLClientAction(GraphQLGenerateClientTask.kt:166)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:104)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.doExecute(StandardTaskAction.java:49)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:42)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:28)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:722)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:689)
ive got to jet for a bit but i'll try and run the example code a bit later and see if i can spot what I'm doing wrong in the extension source
d

Dariusz Kuc

05/31/2020, 6:16 PM
based on stacktrace it fails here -> https://github.com/ExpediaGroup/graphql-kotlin/blob/3.0.0-RC7/plugins/graphql-kotlin-plugin-core/src/main/kotlin/com/expediagroup/graphql/plugin/generator/GraphQLClientGenerator.kt#L129 which would imply either your schema definition is incorrect or there is something odd with your query file
t

tim

05/31/2020, 8:08 PM
adding
Copy code
schema {
    mutation: Mutation
    query: Query
}
to my schema does the trick!
sorry for bothering you
137 Views