Using the Apollo GraphQL client for Android

Using the Apollo GraphQL client for Android


3

I am using from the below sample, but it gets me an error when I add libraries:

Using the Apollo GraphQL client for Android

Below is my build.gradle file (project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'com.apollographql.android:gradle-plugin:0.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Below is my build.gradle file (Module):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.admin.apollotest"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'

    compile 'com.apollographql.android:api:0.1.0' // the apollo runtime classes needed by auto-generated code
    compile 'com.squareup.retrofit2:retrofit:2.1.0' // retrofit2
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' // rxjava2 to use the Observables stuff
    compile 'com.apollographql.android:converter-pojo:0.1.0' // converts retrofit responses to pojos (ApolloConverterFactory)
}
apply plugin: 'com.apollographql.android'

apollo {
    // This tells the Apollo compiler to generate actual static classes instead of just interfaces (more on that later)
    generateClasses = true
}

Get me the below error:

Using the Apollo GraphQL client for Android

Share

1

3 Answers
3

Reset to default


10

The error is “Couldn’t find a schema file. Please ensure a valid schema.json file exists in the sourceSet directory”.

Use the apollo-codegen download-schema command to create this JSON. This takes the URL of the GraphQL endpoint as a parameter, followed by --output and the path to where the JSON should be stored. The JSON should go src/main/graphql/ of your project, and in there into subdirectories representing the Java package that you want your code to go into (e.g., --output src/main/graphql/com/commonsware/graphql/trips/api/schema.json).

So, overall, you might have:

apollo-codegen download-schema https://graphql-demo.commonsware.com/0.1/graphql --output src/main/graphql/com/commonsware/graphql/trips/api/schema.json

Share

4

  • What can I do ? 'apollo-codegen' is not recognized as an internal or external command, operable program or batch file.

    – user4813855

    Apr 9, 2017 at 11:38

  • 2

    @JoJoRoid: apollo-codegen is a Node package. Run the npm install -g apollo-codegen command. This will install apollo-codegen "globally", so it is available to any Android project for which you wish to use Apollo-Android. Note, though, that using the -g switch for a global install may require superuser privileges (e.g., sudo) on your development machine.

    – CommonsWare

    Apr 9, 2017 at 11:39

  • 2

    Get me this error : Error:Execution failed for task ':app:installApolloCodegen'. > Process 'command 'D:AndroidStudioProjectTestApolloapp.gradlenodejsnode-v6.7.0-win-x64node.exe'' finished with non-zero exit value -4048

    – user4813855

    Apr 9, 2017 at 12:02

  • @JoJoRoid: Sorry, I do not know what that one means.

    – CommonsWare

    Apr 9, 2017 at 12:23


2

It seems that you’re using an old version of Apollo. Those warnings have been fixed in the recent stable version.

I suggest you replace

classpath 'com.apollographql.android:gradle-plugin:0.1.0'

with

classpath 'com.apollographql.apollo:gradle-plugin:0.3.0'

and get rid of the generateClasses = true block and the converter-pojo and apollo-api dependencies. Those are no longer needed with the latest version.

Run a clean build after that.

Share


0

Implementation for Apollo CLI v2.17.2 (apollo-android v1.0.2)

Specifically the GitHub API:

apollo client:download-schema /src/main/graphql/com/graphql/example/schema.json --endpoint https://api.github.com/graphql --header "Authorization: Bearer <github-token>"

Github. Creating a personal access token for the command line

Share



Leave a Reply

Your email address will not be published. Required fields are marked *