package com.coxautodev.graphql.tools;

import graphql.relay.*;
import graphql.schema.DataFetchingEnvironment;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RelayConnectionTest {

    @Test
    public void compiles() {
        SchemaParser.newParser().file("RelayConnection.graphqls")
                .resolvers(new QueryResolver())
                .dictionary(User.class)
                .build()
                .makeExecutableSchema();
    }

    static class QueryResolver implements GraphQLQueryResolver {
        // fixme #114: desired return type to use: Connection<User>
        public UserConnection users(int first, String after, DataFetchingEnvironment env) {
            return (UserConnection) new SimpleListConnection<User>(new ArrayList()).get(env);
        }
    }

    // fixme #114: remove this implementation
    static class UserConnection extends DefaultConnection<User> {

        UserConnection(List<Edge<User>> edges, PageInfo pageInfo) {
            super(edges, pageInfo);
        }

        public List<UserEdge> edges() {
            return super.getEdges().stream()
                    .map(UserEdge.class::cast)
                    .collect(Collectors.toList());
        }

        public PageInfo getPageInfo() {
            return super.getPageInfo();
        }
    }

    // fixme #114: remove this implementation
    static class UserEdge extends DefaultEdge<User> {

        UserEdge(User node, ConnectionCursor cursor) {
            super(node, cursor);
        }

    }

    static class User {
        Long id;
        String name;
    }
}

@@ -57,9 +57,61 @@
 lay.
-*
+Connection;%0Aimport graphql.relay.SimpleListConnection
 ;%0Aim
@@ -210,67 +210,8 @@
 ist;
-%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;
 %0A%0Apu
@@ -658,20 +658,16 @@
  public 
-User
 Connecti
@@ -668,16 +668,22 @@
 nnection
+%3CUser%3E
  users(i
@@ -759,25 +759,8 @@
 turn
- (UserConnection)
  new
@@ -800,16 +800,18 @@
 rrayList
+%3C%3E
 ()).get(
@@ -817,733 +817,8 @@
 (env
-);%0A        %7D%0A    %7D%0A%0A    // fixme #114: remove this implementation%0A    static class UserConnection extends DefaultConnection%3CUser%3E %7B%0A%0A        UserConnection(List%3CEdge%3CUser%3E%3E edges, PageInfo pageInfo) %7B%0A            super(edges, pageInfo);%0A        %7D%0A%0A        public List%3CUserEdge%3E edges() %7B%0A            return super.getEdges().stream()%0A                    .map(UserEdge.class::cast)%0A                    .collect(Collectors.toList());%0A        %7D%0A%0A        public PageInfo getPageInfo() %7B%0A            return super.getPageInfo();%0A        %7D%0A    %7D%0A%0A    // fixme #114: remove this implementation%0A    static class UserEdge extends DefaultEdge%3CUser%3E %7B%0A%0A        UserEdge(User node, ConnectionCursor cursor) %7B%0A            super(node, cursor
 );%0A 
@@ -822,25 +822,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    s

Update RelayConnectionTest with generics #114