Search code examples
javaandroidapolloapollo-android

How to import generated class from Apollo Client (GraphQL) in Android Studio


I'm new to Android studio and want to write a small example app with GraphQL and the Apollo Client.

I've successfully setup my build environment and the automated code generation. Here's a screenshot of the file structure, build/generated/apollo/AllPostsQuery is the class that was autogenerated based on src/main/graphql/AllPosts.graphql:

My only issue now is that I don't know how to import the AllPostsQuery class into my MainActivy.

I simply tried:

import AllPostsQuery; 

but that doesn't work:

Can not resolve symbol AllPostsQuery.

Do I need to add any prefixes to the import path?


Solution

  • The way your project is structured in the question, you have your GraphQL document and the schema JSON directly in graphql/. That will result in your generated Java class not being put into a package. That's probably not what you want.

    Apollo-Android takes the approach that is used for some other forms of source code (Java, AIDL, etc.), and uses a package directory tree. In this sample project (from this book), I have my GraphQL document and schema JSON in graphql/com/commonsware/graphql/trips/api. As a result, my generated Java classes are in the com.commonsware.graphql.trips.api package.

    So, set up a series of subdirectories for whatever Java package you want your generated code to go into, and move your GraphQL document and schema JSON there.