Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce explicit re-fetch query for mutation operation. #504

Merged
merged 15 commits into from
May 27, 2017
Next Next commit
Prepare for query re-fetching.
Introduce separate interfaces for query and mutation calls.
Generate OperationName type for operations to be used as identifier for refetching.
  • Loading branch information
sav007 committed May 13, 2017
commit 817fa5a7e9b153d5acfc8aa302cff5c2a0f27b56
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public interface Operation<D extends Operation.Data, T, V extends Operation.Vari
*/
T wrapData(D data);

/**
* Returns GraphQL operation name.
*
* @return {@link OperationName} operation name
*/
@Nonnull OperationName name();

/**
* Abstraction for data returned by the server in response to this operation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.apollographql.apollo.api;

/**
* GraphQL operation name.
*/
public interface OperationName {
/**
* Returns operation name.
*
* @return operation name
*/
String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.apollographql.android.compiler

import com.apollographql.apollo.api.ResponseFieldMapper
import com.apollographql.android.compiler.ir.*
import com.apollographql.apollo.api.OperationName
import com.squareup.javapoet.*
import javax.lang.model.element.Modifier

Expand All @@ -26,6 +27,7 @@ class OperationTypeSpecBuilder(
.addResponseFieldMapperMethod()
.addBuilder(context)
.addType(operation.toTypeSpec(newContext))
.addOperationName()
.build()
.flatten(excludeTypeNames = listOf(Util.MAPPER_TYPE_NAME, SchemaTypeSpecBuilder.FRAGMENTS_TYPE_NAME))
}
Expand Down Expand Up @@ -180,6 +182,35 @@ class OperationTypeSpecBuilder(
else
ClassNames.GRAPHQL_OPERATION_VARIABLES

private fun TypeSpec.Builder.addOperationName(): TypeSpec.Builder {
fun operationNameTypeSpec() = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(OperationName::class.java)
.addMethod(MethodSpec.methodBuilder("name")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override::class.java)
.returns(java.lang.String::class.java)
.addStatement("return \$S", operation.operationName)
.build())
.build()

fun TypeSpec.Builder.addOperationNameField(): TypeSpec.Builder =
addField(FieldSpec.builder(OperationName::class.java, "OPERATION_NAME", Modifier.PRIVATE, Modifier.STATIC,
Modifier.FINAL)
.initializer("\$L", operationNameTypeSpec())
.build())

fun TypeSpec.Builder.addOperationNameAccessor(): TypeSpec.Builder =
addMethod(MethodSpec.methodBuilder("name")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override::class.java)
.returns(OperationName::class.java)
.addStatement("return \$L", "OPERATION_NAME")
.build())

return addOperationNameField()
.addOperationNameAccessor()
}

companion object {
private val OPERATION_DEFINITION_FIELD_NAME = "OPERATION_DEFINITION"
private val QUERY_DOCUMENT_FIELD_NAME = "QUERY_DOCUMENT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -32,6 +33,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery

public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final TestQuery.Variables variables;

public TestQuery(@Nullable Episode episode, int stars, double greenValue) {
Expand Down Expand Up @@ -62,6 +70,11 @@ public static Builder builder() {
return new Builder();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static final class Variables extends Operation.Variables {
private final @Nullable Episode episode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -30,6 +31,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery

public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final TestQuery.Variables variables;

public TestQuery(@Nullable Episode episode, boolean includeName) {
Expand Down Expand Up @@ -60,6 +68,11 @@ public static Builder builder() {
return new Builder();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static final class Variables extends Operation.Variables {
private final @Nullable Episode episode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -33,6 +34,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery

public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -59,6 +67,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<Hero> hero;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand All @@ -25,6 +26,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery

public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -51,6 +59,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<Hero> hero;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -29,6 +30,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery

public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -55,6 +63,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<Hero> hero;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.FragmentResponseFieldMapper;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand All @@ -28,6 +29,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery
public static final String QUERY_DOCUMENT = OPERATION_DEFINITION + "\n"
+ HeroDetails.FRAGMENT_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -54,6 +62,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<Hero> hero;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.FragmentResponseFieldMapper;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -38,6 +39,13 @@ public final class AllStarships implements Query<AllStarships.Data, Optional<All
+ StarshipFragment.FRAGMENT_DEFINITION + "\n"
+ PilotFragment.FRAGMENT_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "AllStarships";
}
};

private final Operation.Variables variables;

public AllStarships() {
Expand All @@ -64,6 +72,11 @@ public ResponseFieldMapper<AllStarships.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<AllStarships1> allStarships;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.FragmentResponseFieldMapper;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -32,6 +33,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery
public static final String QUERY_DOCUMENT = OPERATION_DEFINITION + "\n"
+ HeroDetails.FRAGMENT_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -58,6 +66,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<Hero> hero;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Field;
import com.apollographql.apollo.api.FragmentResponseFieldMapper;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.ResponseFieldMapper;
import com.apollographql.apollo.api.ResponseReader;
Expand Down Expand Up @@ -36,6 +37,13 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery
+ HumanDetails.FRAGMENT_DEFINITION + "\n"
+ DroidDetails.FRAGMENT_DEFINITION;

private static final OperationName OPERATION_NAME = new OperationName() {
@Override
public String name() {
return "TestQuery";
}
};

private final Operation.Variables variables;

public TestQuery() {
Expand All @@ -62,6 +70,11 @@ public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

@Override
public OperationName name() {
return OPERATION_NAME;
}

public static class Data implements Operation.Data {
private final Optional<R2> r2;

Expand Down
Loading