diff --git a/elide-graphql/pom.xml b/elide-graphql/pom.xml index abea7a016d..ef81adabcf 100644 --- a/elide-graphql/pom.xml +++ b/elide-graphql/pom.xml @@ -51,6 +51,11 @@ elide-test-helpers 5.0.0-pr9-SNAPSHOT + + com.yahoo.elide + elide-example-models + 5.0.0-pr9-SNAPSHOT + com.fasterxml.jackson.core jackson-databind diff --git a/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLEntityProjectionMakerTest.java b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLEntityProjectionMakerTest.java new file mode 100644 index 0000000000..910d8ec136 --- /dev/null +++ b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLEntityProjectionMakerTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020, Yahoo Inc. + * Licensed under the Apache License, Version 2.0 + * See LICENSE file in project root for terms. + */ + +package com.yahoo.elide.graphql; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.yahoo.elide.ElideSettings; +import com.yahoo.elide.ElideSettingsBuilder; +import com.yahoo.elide.core.filter.dialect.RSQLFilterDialect; +import com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker; +import com.yahoo.elide.graphql.parser.GraphQLProjectionInfo; +import com.yahoo.elide.request.Attribute; +import com.yahoo.elide.request.EntityProjection; +import example.models.inheritance.Character; +import example.models.inheritance.Droid; +import org.junit.jupiter.api.Test; + +import java.util.TimeZone; + +public class GraphQLEntityProjectionMakerTest extends GraphQLTest { + ElideSettings settings; + + public GraphQLEntityProjectionMakerTest() { + RSQLFilterDialect filterDialect = new RSQLFilterDialect(dictionary); + + settings = new ElideSettingsBuilder(null) + .withEntityDictionary(dictionary) + .withJoinFilterDialect(filterDialect) + .withSubqueryFilterDialect(filterDialect) + .withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC")) + .build(); + } + + @Test + public void testInlineFragment() { + GraphQLEntityProjectionMaker maker = new GraphQLEntityProjectionMaker(settings); + + String query = "{ character { edges { node { " + + "__typename ... on Character { name } " + + "__typename ... on Droid { primaryFunction }}}}}"; + + GraphQLProjectionInfo info = maker.make(query); + + EntityProjection projection = info.getProjection("", "character"); + + Attribute nameAttribute = projection.getAttributes().stream() + .filter(attr -> attr.getName().equals("name")) + .findFirst() + .orElseThrow(() -> new IllegalStateException()); + + assertEquals(Character.class, nameAttribute.getParentType()); + + Attribute primaryFunctionAttribute = projection.getAttributes().stream() + .filter(attr -> attr.getName().equals("primaryFunction")) + .findFirst() + .orElseThrow(() -> new IllegalStateException()); + + assertEquals(Droid.class, primaryFunctionAttribute.getParentType()); + } +} diff --git a/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLNameUtilsTest.java b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLNameUtilsTest.java new file mode 100644 index 0000000000..3c6f923ea7 --- /dev/null +++ b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLNameUtilsTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2020, Yahoo Inc. + * Licensed under the Apache License, Version 2.0 + * See LICENSE file in project root for terms. + */ + +package com.yahoo.elide.graphql; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.yahoo.elide.core.EntityDictionary; +import example.models.inheritance.Character; +import example.models.inheritance.Droid; +import example.models.inheritance.Hero; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +public class GraphQLNameUtilsTest { + + @Test + public void testBoundNameMapping() { + EntityDictionary dictionary = new EntityDictionary(Collections.EMPTY_MAP); + + dictionary.bindEntity(Droid.class); + dictionary.bindEntity(Hero.class); + dictionary.bindEntity(Character.class); + + GraphQLNameUtils nameUtils = new GraphQLNameUtils(dictionary); + + assertEquals("droid", nameUtils.toBoundName("Droid")); + assertEquals("hero", nameUtils.toBoundName("Hero")); + assertEquals("character", nameUtils.toBoundName("Character")); + } +} diff --git a/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLTest.java b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLTest.java index 417b4678ab..78d201abc6 100644 --- a/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLTest.java +++ b/elide-graphql/src/test/java/com/yahoo/elide/graphql/GraphQLTest.java @@ -14,6 +14,9 @@ import example.Book; import example.Pseudonym; import example.Publisher; +import example.models.inheritance.Character; +import example.models.inheritance.Droid; +import example.models.inheritance.Hero; import java.util.HashMap; import java.util.Map; @@ -35,5 +38,8 @@ public GraphQLTest() { dictionary.bindEntity(Publisher.class); dictionary.bindEntity(Pseudonym.class); dictionary.bindEntity(Address.class); + dictionary.bindEntity(Hero.class); + dictionary.bindEntity(Droid.class); + dictionary.bindEntity(Character.class); } } diff --git a/elide-graphql/src/test/java/com/yahoo/elide/graphql/ModelBuilderTest.java b/elide-graphql/src/test/java/com/yahoo/elide/graphql/ModelBuilderTest.java index bdcfb73b93..d2347cf509 100644 --- a/elide-graphql/src/test/java/com/yahoo/elide/graphql/ModelBuilderTest.java +++ b/elide-graphql/src/test/java/com/yahoo/elide/graphql/ModelBuilderTest.java @@ -17,12 +17,15 @@ import com.yahoo.elide.core.ArgumentType; import com.yahoo.elide.core.EntityDictionary; - import com.yahoo.elide.request.Sorting; + import example.Author; import example.Book; import example.Publisher; +import example.models.inheritance.Character; +import example.models.inheritance.Droid; +import example.models.inheritance.Hero; import org.junit.jupiter.api.Test; import graphql.Scalars; @@ -30,6 +33,7 @@ import graphql.schema.GraphQLEnumType; import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLInputObjectType; +import graphql.schema.GraphQLInterfaceType; import graphql.schema.GraphQLList; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; @@ -57,6 +61,9 @@ public class ModelBuilderTest { private static final String PAGE_INFO = "pageInfo"; private static final String TYPE_QUERY = "Query"; + private static final String TYPE_CHARACTER = "Character"; + private static final String TYPE_DROID = "Droid"; + private static final String TYPE_HERO = "Hero"; private static final String TYPE_BOOK_CONNECTION = "BookConnection"; private static final String TYPE_BOOK_INPUT = "BookInput"; private static final String TYPE_BOOK = "Book"; @@ -92,6 +99,9 @@ public ModelBuilderTest() { dictionary.bindEntity(Book.class); dictionary.bindEntity(Author.class); dictionary.bindEntity(Publisher.class); + dictionary.bindEntity(Droid.class); + dictionary.bindEntity(Hero.class); + dictionary.bindEntity(Character.class); } @Test @@ -148,11 +158,11 @@ public void testBuild() { GraphQLSchema schema = builder.build(); - assertNotEquals(schema.getType(TYPE_AUTHOR_CONNECTION), null); - assertNotEquals(schema.getType(TYPE_BOOK_CONNECTION), null); - assertNotEquals(schema.getType(TYPE_AUTHOR_INPUT), null); - assertNotEquals(schema.getType(TYPE_BOOK_INPUT), null); - assertNotEquals(schema.getType(TYPE_QUERY), null); + assertNotEquals(null, schema.getType(TYPE_AUTHOR_CONNECTION)); + assertNotEquals(null, schema.getType(TYPE_BOOK_CONNECTION)); + assertNotEquals(null, schema.getType(TYPE_AUTHOR_INPUT)); + assertNotEquals(null, schema.getType(TYPE_BOOK_INPUT)); + assertNotEquals(null, schema.getType(TYPE_QUERY)); GraphQLObjectType bookType = getConnectedType((GraphQLObjectType) schema.getType(TYPE_BOOK_CONNECTION), null); GraphQLObjectType authorType = getConnectedType((GraphQLObjectType) schema.getType(TYPE_AUTHOR_CONNECTION), null); @@ -199,6 +209,25 @@ public void testBuild() { assertTrue(booksInputType.getWrappedType().equals(bookInputType)); } + @Test + public void testInterfaces() { + DataFetcher fetcher = mock(DataFetcher.class); + ModelBuilder builder = new ModelBuilder(dictionary, fetcher, NO_VERSION); + + GraphQLSchema schema = builder.build(); + assertNotEquals(null, schema.getType(TYPE_CHARACTER)); + assertNotEquals(null, schema.getType(TYPE_DROID)); + assertNotEquals(null, schema.getType(TYPE_HERO)); + GraphQLInterfaceType characterType = (GraphQLInterfaceType) schema.getType(TYPE_CHARACTER); + assertEquals("DeferredID", characterType.getFieldDefinition("name").getType().getName()); + + GraphQLObjectType heroType = (GraphQLObjectType) schema.getType(TYPE_HERO); + assertTrue(heroType.getFieldDefinition("forceSensitive").getType().equals(Scalars.GraphQLBoolean)); + + GraphQLObjectType droidType = (GraphQLObjectType) schema.getType(TYPE_DROID); + assertTrue(droidType.getFieldDefinition("primaryFunction").getType().equals(Scalars.GraphQLString)); + } + @Test public void checkAttributeArguments() { Set arguments = new HashSet<>();