Skip to content

Commit

Permalink
Merge branch 'master' into fixModelConfigBuildWarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
aklish committed Mar 31, 2021
2 parents 323d96a + e51178e commit 0c93875
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import javax.sql.DataSource;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

/**
Expand Down Expand Up @@ -1375,4 +1376,59 @@ public void testTimeDimensionArgumentsInFilter() throws Exception {

runQueryWithExpectedResult(graphQLRequest, expected);
}

@Test
public void testSchemaIntrospection() throws Exception {
String graphQLRequest = "{"
+ "__schema {"
+ " mutationType {"
+ " name "
+ " fields {"
+ " name "
+ " args {"
+ " name"
+ " defaultValue"
+ " }"
+ " }"
+ " }"
+ "}"
+ "}";

String query = toJsonQuery(graphQLRequest, new HashMap<>());

given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(query)
.post("/graphQL")
.then()
.statusCode(HttpStatus.SC_OK)
// Verify that the orderDetails Model has an argument "denominator".
.body("data.__schema.mutationType.fields.find { it.name == 'orderDetails' }.args.name[7] ", equalTo("denominator"));

graphQLRequest = "{"
+ "__type(name: \"OrderDetails\") {"
+ " name"
+ " fields {"
+ " name "
+ " args {"
+ " name"
+ " defaultValue"
+ " }"
+ " }"
+ "}"
+ "}";

query = toJsonQuery(graphQLRequest, new HashMap<>());

given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(query)
.post("/graphQL")
.then()
.statusCode(HttpStatus.SC_OK)
// Verify that the orderTotal attribute has an argument "precision".
.body("data.__type.fields.find { it.name == 'orderTotal' }.args.name[0]", equalTo("precision"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public void tableMetaDataTest() {
"playerStats.highScore", "playerStats.dailyAverageScorePerPeriod"))
.body("data.relationships.timeDimensions.data.id", containsInAnyOrder("playerStats.recordedDate",
"playerStats.updatedDate"));
// Verify Table Arguments
given()
.accept("application/vnd.api+json")
.get("/table/orderDetails?include=arguments")
.then()
.statusCode(HttpStatus.SC_OK)
.body("data.relationships.arguments.data.id", containsInAnyOrder("orderDetails.denominator"));
}

@Test
Expand Down Expand Up @@ -295,6 +302,15 @@ public void metricMetaDataTest() {
.body("data.attributes.arguments", nullValue()) // No Arguments were set.
.body("data.relationships.table.data.id", equalTo("videoGame"));

// Verify Metric Arguments
given()
.accept("application/vnd.api+json")
.get("/table/orderDetails/metrics/orderDetails.orderTotal?include=arguments")
.then()
.statusCode(HttpStatus.SC_OK)
.body("data.attributes.name", equalTo("orderTotal"))
.body("data.attributes.friendlyName", equalTo("orderTotal"))
.body("data.relationships.arguments.data.id", containsInAnyOrder("orderDetails.orderTotal.precision"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
dbConnectionName: SalesDBConnection
cardinality: large
readAccess: guest user
arguments:
[
{ // An argument that can be used to divide orderTotal to convert orders in Millions, Thousands, etc.
name: denominator
type: DECIMAL
default: 1
}
]
joins:
[
{
Expand All @@ -32,8 +40,17 @@
{
name: orderTotal
type: DECIMAL
// TODO : Use Arguments
definition: 'SUM({{ $order_total }})'
readAccess: admin.user
arguments:
[
{ // An argument that can be used to divide orderTotal to convert orders in Millions, Thousands, etc.
name: precision
type: DECIMAL
default: 0.00
}
]
}
]
dimensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -195,6 +196,10 @@ private EntityProjection createProjection(Type<?> entityType, Field entityField)
.type(entityType)
.pagination(PaginationImpl.getDefaultPagination(entityType, elideSettings));

// Add the Entity Arguments to the Projection
projectionBuilder.arguments(new HashSet<>(
getArguments(entityField, entityDictionary.getEntityArguments(entityType))
));
entityField.getSelectionSet().getSelections().forEach(selection -> addSelection(selection, projectionBuilder));
entityField.getArguments().forEach(argument -> addArgument(argument, projectionBuilder));

Expand Down Expand Up @@ -349,11 +354,10 @@ private void addArgument(Argument argument, EntityProjectionBuilder projectionBu
addSorting(argument, projectionBuilder);
} else if (ModelBuilder.ARGUMENT_FILTER.equals(argumentName)) {
addFilter(argument, projectionBuilder);
} else if (isEntityArgument(argumentName, entityDictionary, projectionBuilder.getType())) {
addEntityArgument(argument, projectionBuilder);
} else if (!ModelBuilder.ARGUMENT_OPERATION.equals(argumentName)
&& !(ModelBuilder.ARGUMENT_IDS.equals(argumentName))
&& !(ModelBuilder.ARGUMENT_DATA.equals(argumentName))) {
&& !(ModelBuilder.ARGUMENT_DATA.equals(argumentName))
&& !isEntityArgument(argumentName, entityDictionary, projectionBuilder.getType())) {
addAttributeArgument(argument, projectionBuilder);
}
}
Expand All @@ -373,20 +377,6 @@ private static boolean isEntityArgument(String argumentName, EntityDictionary di
.anyMatch(a -> a.getName().equals(argumentName));
}

/**
* Create a {@link com.yahoo.elide.core.request.Argument} object from GraphQL argument and attach it to the building
* {@link EntityProjection}.
*
* @param argument graphQL argument
* @param projectionBuilder projection that is being built
*/
private void addEntityArgument(Argument argument, EntityProjectionBuilder projectionBuilder) {
projectionBuilder.argument(com.yahoo.elide.core.request.Argument.builder()
.name(argument.getName())
.value(variableResolver.resolveValue(argument.getValue()))
.build());
}

/**
* Returns whether or not a GraphQL argument name corresponding to a pagination argument.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ public GraphQLTest() {

dictionary.addArgumentToAttribute(ClassType.of(ParameterizedExample.class), "attribute",
new ArgumentType("argument", ClassType.STRING_TYPE, "defaultValue"));
dictionary.addArgumentToEntity(ClassType.of(ParameterizedExample.class),
new ArgumentType("entityArgument", ClassType.STRING_TYPE, "defaultArgValue"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ public void checkAttributeArguments() {
assertEquals(Scalars.GraphQLString, bookType.getFieldDefinition(FIELD_PUBLISH_DATE).getArgument(TYPE).getType());
}

@Test
public void checkModelArguments() {
// Add test arguments to entities
dictionary.addArgumentToEntity(getClassType(Book.class), new ArgumentType("filterBook", ClassType.STRING_TYPE));
dictionary.addArgumentToEntity(getClassType(Publisher.class), new ArgumentType("filterPublisher", ClassType.STRING_TYPE));
dictionary.addArgumentToEntity(getClassType(Author.class), new ArgumentType("filterAuthor", ClassType.STRING_TYPE));

DataFetcher fetcher = mock(DataFetcher.class);
ModelBuilder builder = new ModelBuilder(dictionary, new NonEntityDictionary(), fetcher, NO_VERSION);

GraphQLSchema schema = builder.build();

GraphQLObjectType root = schema.getQueryType();
assertNotNull(root);
assertNotNull(root.getFieldDefinition(FIELD_BOOK));

/* The root 'book' should have the "filterBook" argument defined */
GraphQLFieldDefinition bookField = root.getFieldDefinition(FIELD_BOOK);
assertNotNull(bookField.getArgument("filterBook"));

/* book.publisher is a "toOne" relationship and has the argument "filterPublisher" defined */
GraphQLObjectType bookType = (GraphQLObjectType) schema.getType(TYPE_BOOK);
GraphQLFieldDefinition publisherField = bookType.getFieldDefinition(FIELD_PUBLISHER);
assertNotNull(publisherField.getArgument("filterPublisher"));

/* book.authors is a 'to many' relationship and has the argument "filterAuthor" defined */
GraphQLFieldDefinition authorField = bookType.getFieldDefinition(FIELD_AUTHORS);
assertNotNull(authorField.getArgument("filterAuthor"));
}

private GraphQLObjectType getConnectedType(GraphQLObjectType root, String connectionName) {
GraphQLList edgesType = (GraphQLList) root.getFieldDefinition(EDGES).getType();
GraphQLObjectType rootType = (GraphQLObjectType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public void testParameterizedAttribute() {
String graphQLRequest = document(
selection(
field(
"parameterizedExample",
"parameterizedExample", arguments(
argument("entityArgument", "xyz", true)
),
selections(
field("attribute", arguments(
argument("argument", "abc", true)
Expand All @@ -66,6 +68,16 @@ public void testParameterizedAttribute() {

EntityProjection projection = projectionInfo.getProjections().values().iterator().next();

// Verify Entity Argument
assertEquals(1, projection.getArguments().size());

Argument entityArgument = projection.getArguments().iterator().next();

assertEquals("entityArgument", entityArgument.getName());
assertEquals(String.class, entityArgument.getType());
assertEquals("xyz", entityArgument.getValue());

// Verify Attribute Argument
assertEquals(1, projection.getAttributes().size());

Attribute attribute = projection.getAttributes().iterator().next();
Expand Down Expand Up @@ -98,6 +110,16 @@ public void testParameterizedAttributeDefaultValue() {

EntityProjection projection = projectionInfo.getProjections().values().iterator().next();

// Verify Entity Argument
assertEquals(1, projection.getArguments().size());

Argument entityArgument = projection.getArguments().iterator().next();

assertEquals("entityArgument", entityArgument.getName());
assertEquals(String.class, entityArgument.getType());
assertEquals("defaultArgValue", entityArgument.getValue());

// Verify Attribute Argument
assertEquals(1, projection.getAttributes().size());

Attribute attribute = projection.getAttributes().iterator().next();
Expand Down

0 comments on commit 0c93875

Please sign in to comment.