Skip to content

Commit

Permalink
Update Graphql integration test (#954)
Browse files Browse the repository at this point in the history
* Graphql IT test

* Update

* Remove jsonignore

* Split test

* make build pass

* fix graphql it

* Sync test with old groovy test
  • Loading branch information
hellohanchen authored and aklish committed Sep 6, 2019
1 parent 6254f83 commit b0aaf30
Show file tree
Hide file tree
Showing 7 changed files with 712 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -339,7 +338,7 @@ public static VariableDefinition variableDefinition(String variable, String type
* @return a field
*/
public static Selection field(String name, String value) {
return new Field(name, Arguments.emptyArgument(), Field.quoteValue(value));
return new Field(null, name, Arguments.emptyArgument(), Field.quoteValue(value));
}

/**
Expand All @@ -351,7 +350,7 @@ public static Selection field(String name, String value) {
* @return a field
*/
public static Selection field(String name, Number value) {
return new Field(name, Arguments.emptyArgument(), value);
return new Field(null, name, Arguments.emptyArgument(), value);
}

/**
Expand All @@ -363,7 +362,7 @@ public static Selection field(String name, Number value) {
* @return a field
*/
public static Selection field(String name, Boolean value) {
return new Field(name, Arguments.emptyArgument(), value);
return new Field(null, name, Arguments.emptyArgument(), value);
}

/**
Expand All @@ -376,7 +375,7 @@ public static Selection field(String name, Boolean value) {
* @return a field
*/
public static Selection field(String name, String value, boolean quoted) {
return new Field(name, Arguments.emptyArgument(), quoted ? Field.quoteValue(value) : value);
return new Field(null, name, Arguments.emptyArgument(), quoted ? Field.quoteValue(value) : value);
}

/**
Expand All @@ -391,8 +390,17 @@ public static Selection field(String name, String value, boolean quoted) {
* @see <a href="https://graphql.org/learn/schema/#object-types-and-fields">Object Types and Fields</a>
*/
public static Selection field(String name, SelectionSet... selectionSet) {
List<SelectionSet> ss = ImmutableList.copyOf(selectionSet);
return new Field(name, Arguments.emptyArgument(), relayWrap(ss));
List<SelectionSet> ss = Arrays.stream(selectionSet)
.map(i -> (SelectionSet) i)
.collect(Collectors.toList());
return new Field(null, name, Arguments.emptyArgument(), relayWrap(ss));
}

public static Selection field(String alias, String name, SelectionSet... selectionSet) {
List<SelectionSet> ss = Arrays.stream(selectionSet)
.map(i -> (SelectionSet) i)
.collect(Collectors.toList());
return new Field(alias, name, Arguments.emptyArgument(), relayWrap(ss));
}

/**
Expand All @@ -409,7 +417,7 @@ public static Selection field(String name, SelectionSet... selectionSet) {
* @see <a href="https://graphql.org/learn/schema/#object-types-and-fields">Object Types and Fields</a>
*/
public static Selection field(String name, Arguments arguments, SelectionSet... selectionSet) {
return new Field(name, arguments, relayWrap(Arrays.asList(selectionSet)));
return new Field(null, name, arguments, relayWrap(Arrays.asList(selectionSet)));
}

/**
Expand All @@ -426,7 +434,7 @@ public static Selection field(String name) {
}

public static Selection field(String name, Arguments arguments) {
return new Field(name, arguments, null);
return new Field(null, name, arguments, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.contrib.testhelpers.graphql;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
* A Jackson serializer for String entity field.
* <p>
* {@link VariableFieldSerializer} serializes a String field differently when value can both represents a concrete value
* or a GraphQL variable. On concrete value, it outputs the original value unmodified and quoted; on variable value, it
* does not quote the original value.
* <p>
* For example, given the following entity:
* <pre>
* {@code
* public class Book {
*
* {@literal @}JsonSerialize(using = VariableFieldSerializer.class, as = String.class)
* private String title;
* }
* }
* </pre>
* A {@code Book(title="Java Concurrency in Practice")} serializes to
* <pre>
* {"title": "Java Concurrency in Practice"}
* </pre>
* However a {@code Book(title="$titlePassedByClient")} serializes to
* <pre>
* {"title": $titlePassedByClient}
* </pre>
* Note in the 1st serialization {@code title} value is quoted while the 2nd serialization it is not.
* <p>
* To serialize a String entity field in such a way, add the following annotation to the field, as shown above:
* <pre>
* {@code
* {@literal @}JsonSerialize(using = VariableFieldSerializer.class, as = String.class)
* }
* </pre>
*
* @see <a href="https://graphql.org/learn/queries/#variables">Variables</a>
*/
public class VariableFieldSerializer extends JsonSerializer<String> {

private static final String VARIABLE_SIGN = "$";

@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value.startsWith(VARIABLE_SIGN)) {
// this is a variable
gen.writeRawValue(value);
} else {
gen.writeString(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ public String toQuery() {
* @return a string representation of a GraphQL response
*/
public String toResponse() {
return String.format(
"{\"data\":%s}",
getDefinitions().stream()
.map(Definition::toResponse)
.collect(Collectors.joining(" "))
);
String response = getDefinitions().stream()
.map(definition -> String.format("{\"data\":%s}", definition.toResponse()))
.collect(Collectors.joining(", "));

if (getDefinitions().size() != 1) {
return String.format("[%s]", response);
}

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public class Field extends Selection {
private static final long serialVersionUID = -5906705888838083150L;

public static Field scalarField(String name) {
return new Field(name, Arguments.emptyArgument(), null);
return new Field(null, name, Arguments.emptyArgument(), null);
}

public static String quoteValue(String value) {
return String.format("\"%s\"", value);
}

@Getter(AccessLevel.PRIVATE)
private final String alias;

/**
* The "name" TOKEN defined in GraphQL grammar.
*/
Expand All @@ -70,7 +73,8 @@ public static String quoteValue(String value) {
@Override
public String toGraphQLSpec() {
return String.format(
"%s%s%s",
"%s%s%s%s",
getAlias() == null ? "" : getAlias() + ": ",
getName(),
argument(),
selection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ String toResponse() {
return String.format(
"{%s}",
getSelections().stream().map(Selection::toResponse)
.collect(Collectors.joining(" "))
.collect(Collectors.joining(", "))
);
}
}
Loading

0 comments on commit b0aaf30

Please sign in to comment.