{"size": 1263, "ext": "js", "max_stars_repo_path": "src/helpers/schema-factory.js", "max_stars_repo_name": "rod750/mongolens", "max_stars_repo_stars_event_min_datetime": "2020-10-16T18:02:55.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-16T18:02:55.000Z", "max_issues_repo_path": "src/helpers/schema-factory.js", "max_issues_repo_name": "rod750/mongolens", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/helpers/schema-factory.js", "max_forks_repo_name": "rod750/mongolens", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.7045454545, "max_line_length": 91, "alphanum_fraction": 0.6832937451}
import { schemaComposer } from "graphql-compose"

export function schemaFactory(modelName, ModelTC) {
  const defineFields = (fields, operation) => {
    fields[`${modelName}${operation[0]}`] = ModelTC.getResolver(operation[1], operation[3])
    return fields
  }

  let queryOperations = [
    ["ById", "findById"],
    ["ByIds", "findByIds"],
    ["One", "findOne"],
    ["Many", "findMany"],
    ["Count", "count"],
    ["Connection", "connection"],
    ["Pagination", "pagination"]
  ]

  if(ModelTC.queryOperations)
    queryOperations = queryOperations.concat(ModelTC.queryOperations)

  const queryFields = queryOperations.reduce(defineFields, {})
  schemaComposer.Query.addFields(queryFields);

  let mutationOperations = [
    ["CreateOne", "createOne"],
    ["CreateMany", "createMany"],
    ["UpdateById", "updateById"],
    ["UpdateOne", "updateOne"],
    ["UpdateMany", "updateMany"],
    ["RemoveById", "removeById"],
    ["RemoveOne", "removeOne"],
    ["RemoveMany", "removeMany"]
  ]

  if(ModelTC.mutationOperations)
    mutationOperations = mutationOperations.concat(ModelTC.mutationOperations)

  const mutationFields = mutationOperations.reduce(defineFields, {})
  schemaComposer.Mutation.addFields(mutationFields)

  return schemaComposer
}
