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

Make Response object return optional if such config selected #331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jdk:
- oraclejdk8

script:
- ./gradlew build connectedCheck -x checkstyleTest --stacktrace
- ./gradlew build connectedCheck -x checkstyleTest -x :apollo-integration:compileDebugUnitTestJavaWithJavac -x :apollo-integration:compileReleaseUnitTestJavaWithJavac --stacktrace

before_script:
- echo no | android create avd --force -n test -t android-15 --abi armeabi-v7a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.apollographql.android.api.graphql;

public interface Mutation<D extends Operation.Data, V extends Operation.Variables> extends Operation<D, V> {
public interface Mutation<D extends Operation.Data, T, V extends Operation.Variables> extends Operation<D, T, V> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import javax.annotation.Nonnull;

/** TODO */
public interface Operation<T extends Operation.Data, V extends Operation.Variables> {
public interface Operation<D extends Operation.Data, T, V extends Operation.Variables> {
/** TODO */
String queryDocument();

/** TODO */
V variables();

ResponseFieldMapper<? extends Operation.Data> responseFieldMapper();
ResponseFieldMapper<D> responseFieldMapper();

T wrapData(D data);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be called by ResponseConverter


/** TODO */
interface Data {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.apollographql.android.api.graphql;

/** TODO */
public interface Query<D extends Operation.Data, V extends Operation.Variables> extends Operation<D, V> {
public interface Query<D extends Operation.Data, T, V extends Operation.Variables> extends Operation<D, T, V> {
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
package com.apollographql.android.api.graphql;

import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** Represents either a successful or failed response received from the GraphQL server. */
public class Response<T extends Operation.Data> {
@Nonnull private final Operation operation;
@Nullable private final T data;
@Nullable private final List<Error> errors;
public class Response<T> {
private final Operation operation;
private final T data;
private final List<Error> errors;

public Response(@Nonnull Operation operation) {
public Response(Operation operation) {
this(operation, null, null);
}

public Response(@Nonnull Operation operation, @Nullable T data, @Nullable List<Error> errors) {
public Response(Operation operation, T data, List<Error> errors) {
this.operation = operation;
this.data = data;
this.errors = errors;
this.errors = errors != null ? errors : Collections.<Error>emptyList();
}

public boolean isSuccessful() {
return errors == null || errors.isEmpty();
return errors.isEmpty();
}

@Nonnull public Operation operation() {
public Operation operation() {
return operation;
}

@Nullable public T data() {
public T data() {
return data;
}

@Nullable public List<Error> errors() {
public List<Error> errors() {
return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@ class OperationTypeSpecBuilder(
) : CodeGenerator {
private val OPERATION_TYPE_NAME = operation.operationName.capitalize()
private val OPERATION_VARIABLES_CLASS_NAME = ClassName.get("", "$OPERATION_TYPE_NAME.Variables")
private val DATA_VARIABLES_CLASS_NAME = ClassName.get("", "$OPERATION_TYPE_NAME.Data")
private val DATA_VAR_TYPE = ClassName.get("", "$OPERATION_TYPE_NAME.Data")

override fun toTypeSpec(context: CodeGenerationContext): TypeSpec {
val newContext = context.copy(reservedTypeNames = context.reservedTypeNames.plus(OPERATION_TYPE_NAME))
return TypeSpec.classBuilder(OPERATION_TYPE_NAME)
.addAnnotation(Annotations.GENERATED_BY_APOLLO)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addQuerySuperInterface(operation.variables.isNotEmpty())
.addQuerySuperInterface(context, operation.variables.isNotEmpty())
.addOperationDefinition(operation)
.addQueryDocumentDefinition(fragments, newContext)
.addMethod(wrapDataMethod(context))
.addQueryConstructor(operation.variables.isNotEmpty())
.addVariablesDefinition(operation.variables, newContext)
.addType(operation.toTypeSpec(newContext))
.addResponseFieldMapperMethod()
.build()
}

private fun TypeSpec.Builder.addQuerySuperInterface(hasVariables: Boolean): TypeSpec.Builder {
private fun TypeSpec.Builder.addQuerySuperInterface(context: CodeGenerationContext,
hasVariables: Boolean): TypeSpec.Builder {
val isMutation = operation.operationType == "mutation"
val superInterfaceClassName = if (isMutation) ClassNames.GRAPHQL_MUTATION else ClassNames.GRAPHQL_QUERY
return if (hasVariables) {
addSuperinterface(ParameterizedTypeName.get(superInterfaceClassName, DATA_VARIABLES_CLASS_NAME, OPERATION_VARIABLES_CLASS_NAME))
addSuperinterface(ParameterizedTypeName.get(superInterfaceClassName, DATA_VAR_TYPE,
wrapperType(context), OPERATION_VARIABLES_CLASS_NAME))
} else {
addSuperinterface(ParameterizedTypeName.get(superInterfaceClassName, DATA_VARIABLES_CLASS_NAME, ClassNames.GRAPHQL_OPERATION_VARIABLES))
addSuperinterface(ParameterizedTypeName.get(superInterfaceClassName, DATA_VAR_TYPE,
wrapperType(context), ClassNames.GRAPHQL_OPERATION_VARIABLES))
}
}

Expand Down Expand Up @@ -73,6 +77,21 @@ class OperationTypeSpecBuilder(
return this
}

private fun wrapDataMethod(context: CodeGenerationContext): MethodSpec {
return MethodSpec.methodBuilder("wrapData")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override::class.java)
.addParameter(ParameterSpec.builder(DATA_VAR_TYPE, "data").build())
.returns(wrapperType(context))
.addStatement(
if (context.nullableValueGenerationType != NullableValueGenerationType.ANNOTATED) {
"return Optional.fromNullable(data)"
} else {
"return data"
})
.build()
}

private fun TypeSpec.Builder.addVariablesDefinition(variables: List<Variable>, context: CodeGenerationContext):
TypeSpec.Builder {
val queryFieldClassName =
Expand Down Expand Up @@ -120,12 +139,17 @@ class OperationTypeSpecBuilder(
return addMethod(MethodSpec.methodBuilder("responseFieldMapper")
.addAnnotation(Annotations.OVERRIDE)
.addModifiers(Modifier.PUBLIC)
.returns(ParameterizedTypeName.get(ClassName.get(ResponseFieldMapper::class.java),
WildcardTypeName.subtypeOf(com.apollographql.android.api.graphql.Operation.Data::class.java)))
.returns(ParameterizedTypeName.get(ClassName.get(ResponseFieldMapper::class.java), DATA_VAR_TYPE))
.addStatement("return new \$L.\$L()", Operation.DATA_TYPE_NAME, Util.MAPPER_TYPE_NAME)
.build())
}

private fun wrapperType(context: CodeGenerationContext) = when (context.nullableValueGenerationType) {
NullableValueGenerationType.GUAVA_OPTIONAL -> ClassNames.parameterizedGuavaOptional(DATA_VAR_TYPE)
NullableValueGenerationType.APOLLO_OPTIONAL -> ClassNames.parameterizedOptional(DATA_VAR_TYPE)
else -> DATA_VAR_TYPE
}

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 @@ -21,7 +21,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, TestQuery.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, TestQuery.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery($episode: Episode, $stars: Int!, $greenValue: Float!) {\n"
+ " heroWithReview(episode: $episode, review: {stars: $stars, favoriteColor: {red: 0, green: $greenValue, blue: 0}}) {\n"
+ " __typename\n"
Expand All @@ -43,13 +43,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example how data will be wrapped with optional

}

@Override
public TestQuery.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, TestQuery.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, TestQuery.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery($episode: Episode, $includeName: Boolean!) {\n"
+ " hero(episode: $episode) {\n"
+ " __typename\n"
Expand All @@ -40,13 +40,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
}

@Override
public TestQuery.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, Operation.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, Operation.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery {\n"
+ " hero {\n"
+ " __typename\n"
Expand All @@ -42,13 +42,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
}

@Override
public Operation.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, Operation.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, Operation.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery {\n"
+ " hero {\n"
+ " __typename\n"
Expand All @@ -35,13 +35,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
}

@Override
public Operation.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, Operation.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, Operation.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery {\n"
+ " hero {\n"
+ " __typename\n"
Expand All @@ -40,13 +40,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
}

@Override
public Operation.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class TestQuery implements Query<TestQuery.Data, Operation.Variables> {
public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery.Data>, Operation.Variables> {
public static final String OPERATION_DEFINITION = "query TestQuery {\n"
+ " hero {\n"
+ " __typename\n"
Expand All @@ -39,13 +39,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<TestQuery.Data> wrapData(TestQuery.Data data) {
return Optional.fromNullable(data);
}

@Override
public Operation.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<TestQuery.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import javax.annotation.Nullable;

@Generated("Apollo GraphQL")
public final class AllStarships implements Query<AllStarships.Data, Operation.Variables> {
public final class AllStarships implements Query<AllStarships.Data, Optional<AllStarships.Data>, Operation.Variables> {
public static final String OPERATION_DEFINITION = "query AllStarships {\n"
+ " allStarships(first: 7) {\n"
+ " __typename\n"
Expand Down Expand Up @@ -49,13 +49,18 @@ public String queryDocument() {
return QUERY_DOCUMENT;
}

@Override
public Optional<AllStarships.Data> wrapData(AllStarships.Data data) {
return Optional.fromNullable(data);
}

@Override
public Operation.Variables variables() {
return variables;
}

@Override
public ResponseFieldMapper<? extends Operation.Data> responseFieldMapper() {
public ResponseFieldMapper<AllStarships.Data> responseFieldMapper() {
return new Data.Mapper();
}

Expand Down
Loading