Skip to content

Commit

Permalink
Merge pull request #7 from cliedeman/feature/connection-cursor-mapper
Browse files Browse the repository at this point in the history
Implement connection cursor mapper
  • Loading branch information
bpatters authored Jan 21, 2017
2 parents 138f5c2 + 4365953 commit 541d281
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bretpatterson.schemagen.graphql.typemappers.relay;

import com.bretpatterson.schemagen.graphql.IGraphQLObjectMapper;
import com.bretpatterson.schemagen.graphql.annotations.GraphQLTypeMapper;
import com.bretpatterson.schemagen.graphql.relay.ConnectionCursor;
import com.bretpatterson.schemagen.graphql.typemappers.IGraphQLTypeMapper;
import graphql.Scalars;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLOutputType;

import java.lang.reflect.Type;

/**
* Specification Compliant ConnectionCursor mapper
*
* @see <a href="https://facebook.github.io/relay/graphql/connections.htm">Connection Specification</a>
*/
@GraphQLTypeMapper(type = ConnectionCursor.class)
public class ConnectionCursorMapper implements IGraphQLTypeMapper {

@Override
public boolean handlesType(IGraphQLObjectMapper graphQLObjectMapper, Type type) {
return ConnectionCursor.class.isAssignableFrom(graphQLObjectMapper.getClassFromType(type));
}

@Override
public GraphQLOutputType getOutputType(IGraphQLObjectMapper graphQLObjectMapper, Type type) {
return Scalars.GraphQLString;
}

@Override
public GraphQLInputType getInputType(IGraphQLObjectMapper graphQLObjectMapper, Type type) {
return Scalars.GraphQLString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public void testRelayConnectionType() {
assertNotNull(edgeObject.getFieldDefinition("node"));
assertNotNull(edgeObject.getFieldDefinition("cursor"));
assertEquals(Scalars.GraphQLString, edgeObject.getFieldDefinition("node").getType());
assertEquals(GraphQLObjectType.class, edgeObject.getFieldDefinition("cursor").getType().getClass());
assertNotNull(((GraphQLObjectType) edgeObject.getFieldDefinition("cursor").getType()).getFieldDefinition("value"));
assertEquals(Scalars.GraphQLString, ((GraphQLObjectType) edgeObject.getFieldDefinition("cursor").getType()).getFieldDefinition("value").getType());
assertEquals(Scalars.GraphQLString, edgeObject.getFieldDefinition("cursor").getType());
}

private class InnerGeneric<R, S> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ private UserDTO getUserNode(String id) throws IOException {
private RelayConnection<GameDTO> findGames(int first) throws IOException {

ExecutionResult result = new GraphQL(schema).execute(String
.format("{ Queries { games(first:%d) { edges { node {id, name}, cursor {value} } , pageInfo {hasPreviousPage, hasNextPage} } } }", first));
assertEquals(0, result.getErrors().size());
.format("{ Queries { games(first:%d) { edges { node {id, name}, cursor } , pageInfo {hasPreviousPage, hasNextPage} } } }", first));
assertEquals(result.getErrors().toString(), 0, result.getErrors().size());

return deserialize(objectMapper.writeValueAsString(getQueryResults(result, "games")), new TypeReference<RelayConnection<GameDTO>>() {
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.bretpatterson.schemagen.graphql.typemappers.relay;

import com.bretpatterson.schemagen.graphql.IGraphQLObjectMapper;
import com.bretpatterson.schemagen.graphql.relay.ConnectionCursor;
import graphql.Scalars;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLOutputType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.lang.reflect.Type;

import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;

@RunWith(MockitoJUnitRunner.class)
public class ConnectionCursorMapperTest {

@Mock
private IGraphQLObjectMapper mockMapper;

@Test
public void test_handlesType_ConnectionCursor_true() throws Exception {
// given
Type type = ConnectionCursor.class;
willReturn(ConnectionCursor.class).given(mockMapper).getClassFromType(type);
ConnectionCursorMapper cut = createCut();

// when
boolean result = cut.handlesType(mockMapper, type);

// then
assertTrue(result);
}

@Test
public void test_handlesType_Other_false() throws Exception {
// given
Type type = Object.class;
willReturn(Object.class).given(mockMapper).getClassFromType(type);
ConnectionCursorMapper cut = createCut();

// when
boolean result = cut.handlesType(mockMapper, type);

// then
assertFalse(result);
}

@Test
public void test_getOutputType() throws Exception {
// given
ConnectionCursorMapper cut = createCut();

// when
GraphQLOutputType result = cut.getOutputType(null, null);

// then
assertEquals(result, Scalars.GraphQLString);
}

@Test
public void test_getInputType() throws Exception {
// given
ConnectionCursorMapper cut = createCut();

// when
GraphQLOutputType result = cut.getOutputType(null, null);

// then
assertEquals(result, Scalars.GraphQLString);
}

private ConnectionCursorMapper createCut() {
return new ConnectionCursorMapper();
}
}

0 comments on commit 541d281

Please sign in to comment.