import gql from 'graphql-tag';
import 'jest';
import { createTestClient } from 'apollo-server-testing';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloServer } from 'apollo-server';
import { ApolloClient } from 'apollo-client';
import {
    makeExecutableSchema,
    addMockFunctionsToSchema,
} from 'graphql-tools';




const typeDefs = `
type Query {
  fetchMore(listType: String!, amount: Int!, offset: Int!): List
}

type Distributor {
  id: Int
  name: String
}

type Product {
  id: Int
  name: String
}

interface List {
  amount: Int
  offset: Int
  total: Int
  remaining: Int
}

type DistributorList implements List {
  amount: Int
  offset: Int
  total: Int
  remaining: Int
  items: [Distributor]
}

type ProductList implements List {
  amount: Int
  offset: Int
  total: Int
  remaining: Int
  items: [Product]
}

type RootQuery {
    getList: List
}
schema {
    query: RootQuery
}
`;



const testQuery = `{
    list {
        ... on ProductList {
            id
            name
        }
        ... on DistributorList {
            amount
            offset
            total
            remaining
        }
    }
}`;
let spy = 0;

const mockMap = {
    ProductList: (root: any, args: any) => ({
        id: args.id,
        name: 'Product',
    }),
    DistributorList: (root: any, args: any) => ({
        amount: 10,
        offset: 10,
        total: 10,
        remaining: 10,
    }),
    list: (root: any, args: any) => {
        spy++;
        // const { id } = args;
        // const type = id.split(':')[0];
        // return {
        //     __typename: ['ProductList', 'DistributorList'].find(r => r.toLowerCase() === type),
        // };
        return {
            typename: 'ProductList',
        };
    },
}
    ;
const typeResolvers = {
    List: {
        __resolveType(data) {
            return data.typename; // typename property must be set by your mock functions
        },
    },
};

const schema = makeExecutableSchema({
    typeDefs,
    resolvers: typeResolvers,
});


addMockFunctionsToSchema({
    schema,
    mocks: mockMap,
    preserveResolvers: true,
});
