<commit_before>import { GraphQLList } from 'graphql';
import _ from 'lodash';
import simplifyAST from './simplifyAST';
import generateIncludes from './generateIncludes';
import argsToFindOptions from './argsToFindOptions';

module.exports = function (target, options) {
  var resolver
    , targetAttributes
    , isModel = !!target.getTableName
    , isAssociation = !!target.associationType
    , association = isAssociation && target
    , model = isAssociation && target.target || isModel && target;

  targetAttributes = Object.keys(model.rawAttributes);

  options = options || {};
  if (options.include === undefined) options.include = true;
  if (options.before === undefined) options.before = (options) => options;
  if (options.after === undefined) options.after = (result) => result;

  resolver = function (source, args, root, ast, type) {
    if (association && source.get(association.as)) {
      return source.get(association.as);
    }

    var list = type instanceof GraphQLList
      , includeResult
      , simpleAST
      , findOptions = argsToFindOptions(args, model);

    simpleAST = simplifyAST(ast);
    root = root || {};
    type = type.ofType || type;

    findOptions.attributes = Object.keys(simpleAST.fields)
                             .filter(attribute => ~targetAttributes.indexOf(attribute));

    findOptions.attributes.push(model.primaryKeyAttribute);

    includeResult = generateIncludes(
      simpleAST,
      type,
      root,
      options
    );

    findOptions.include = includeResult.include;
    findOptions.root = root;
    findOptions.attributes = _.unique(findOptions.attributes.concat(includeResult.attributes));
    findOptions.logging = findOptions.logging || root.logging;

    if (includeResult.order) {<commit_msg>fix(resolver): null is a valid association value for 1:1 relations<commit_after>import { GraphQLList } from 'graphql';
import _ from 'lodash';
import simplifyAST from './simplifyAST';
import generateIncludes from './generateIncludes';
import argsToFindOptions from './argsToFindOptions';

module.exports = function (target, options) {
  var resolver
    , targetAttributes
    , isModel = !!target.getTableName
    , isAssociation = !!target.associationType
    , association = isAssociation && target
    , model = isAssociation && target.target || isModel && target;

  targetAttributes = Object.keys(model.rawAttributes);

  options = options || {};
  if (options.include === undefined) options.include = true;
  if (options.before === undefined) options.before = (options) => options;
  if (options.after === undefined) options.after = (result) => result;

  resolver = function (source, args, root, ast, type) {
    if (association && source.get(association.as) !== undefined) {
      return source.get(association.as);
    }

    var list = type instanceof GraphQLList
      , includeResult
      , simpleAST
      , findOptions = argsToFindOptions(args, model);

    simpleAST = simplifyAST(ast);
    root = root || {};
    type = type.ofType || type;

    findOptions.attributes = Object.keys(simpleAST.fields)
                             .filter(attribute => ~targetAttributes.indexOf(attribute));

    findOptions.attributes.push(model.primaryKeyAttribute);

    includeResult = generateIncludes(
      simpleAST,
      type,
      root,
      options
    );

    findOptions.include = includeResult.include;
    findOptions.root = root;
    findOptions.attributes = _.unique(findOptions.attributes.concat(includeResult.attributes));
    findOptions.logging = findOptions.logging || root.logging;

    if (includeResult.order) {
{'fraction_non_alphanumeric': 0.11769592910551094, 'fraction_numerical': 0.0005538631957906397, 'mean_word_length': 3.5606060606060606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3659243', 'n_tokens_mistral': 996, 'n_tokens_neox': 958, 'n_words': 230}