2

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?

doubleA
  • 2,446
  • 22
  • 45
nburk
  • 22,409
  • 18
  • 87
  • 132
  • 1
    See here how to let AS auto-import classes for you https://stackoverflow.com/questions/22272524/how-to-auto-import-the-necessary-classes-in-android-studio-with-shortcut – Adinia Oct 25 '17 at 12:45
  • Thanks for the comment! I believe my auto-import is already setup, it did work for all the other classes (I didn't write a single import myself so far). https://imgur.com/VcVFXp4 any other ideas? – nburk Oct 25 '17 at 12:48
  • 1
    You should set up a package directory tree under `graphql/`. The generated code will go into that package. See [this sample project](https://github.com/commonsguy/cw-graphql/tree/v0.3/Trips/CW/StaticList). – CommonsWare Oct 25 '17 at 12:52
  • ah great that worked @CommonsWare! if you add this as an actual answer I'll mark it as accepted and you get your points :) – nburk Oct 25 '17 at 13:00
  • Were you able to resolve the ANDROID_HOME issue? – OneCricketeer Oct 25 '17 at 14:03
  • @cricket_007 no unfortunately not! I've just started a fresh project now where the build works... I still can't run the sample project from the apollo-android repo... – nburk Oct 25 '17 at 14:05

1 Answers1

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491