import { GraphQLObjectType, GraphQLID } from 'graphql';
import buildAttributes from './buildAttributes';
import buildRelationships from './buildRelationships';
import titleizeType from './titleizeType';

const getDefaultDescription = (name) => `The ${titleizeType(name)} Cohere model.`;

export default (schema) => {
  const typeCache = {};

  const graphQLObjectTypes = schema.types.reduce((prev, type) => {
    const { meta, name } = type;

    if (!meta) {
      throw new Error(
        'Micrograph requires every defined type to have a valid meta key. The ' +
        `${name} type did not have a meta key.`
      );
    }

    if (typeCache.hasOwnProperty(name)) return prev;
    typeCache[name] = true;

    return {
      ...prev,
      [name]: new GraphQLObjectType({
        description: meta.description || getDefaultDescription(name),
        name: `${titleizeType(name)}`,
        fields: () => ({
          id: { type: GraphQLID },
          ...buildAttributes(type),
          ...buildRelationships(type, graphQLObjectTypes),
        }),
      }),
    };
  }, {});

  return graphQLObjectTypes;
};

@@ -279,16 +279,38 @@
 model.%60;
+%0Aconst typeCache = %7B%7D;
 %0A%0Aexport
@@ -336,33 +336,8 @@
 %3E %7B%0A
-  const typeCache = %7B%7D;%0A%0A
   co

Fix type cache getting result on func calls