<commit_before>import {
  GraphQLObjectType,
  GraphQLList,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean
} from 'graphql'

import {
  globalIdField,
  connectionArgs,
  forwardConnectionArgs,
  connectionDefinitions,
  connectionFromArray
} from 'graphql-relay'

import { PostConnection } from './Post'
import { CommentConnection } from './Comment'
import { nodeInterface } from './Node'
import { AuthoredConnection } from './Authored/Interface'
import { q, bool } from '../shared'

const { PAGINATE, STRATEGY, DB } = process.env

const User = new GraphQLObjectType({
  description: 'a stem contract account',
  name: 'User',
  sqlTable: `(SELECT * FROM ${q('accounts', DB)})`,
  uniqueKey: 'id',
  interfaces: [ nodeInterface ],
  fields: () => ({
    id: {
      description: 'The global ID for the Relay spec',
      ...globalIdField('User'),
      sqlDeps: [ 'id' ]
    },
    email: {
      type: GraphQLString,
      sqlColumn: 'email_address'
    },
    fullName: {
      description: 'A user\'s first and last name',
      type: GraphQLString,
      sqlDeps: [ 'first_name', 'last_name' ],
      resolve: user => `${user.first_name} ${user.last_name}`
    },
    comments: {
      description: 'Comments the user has written on people\'s posts',
      type: CommentConnection,
      args: {
        active: { type: GraphQLBoolean },
        ...PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs
      },
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({ orderBy: 'id' })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'desc',
              key: 'id'
            }
          })
        } else {
          ({
            resolve: (user, args) => {
              user.comments.sort((a, b) => a.id - b.id)
              return connectionFromArray(user.comments, args)
            }
          })
        }
      },
      ... do {
        if (STRATEGY === 'batch' || STRATEGY === 'mix') {
          ({
            sqlBatch: {
              thisKey: 'author_id',
              parentKey: 'id'
            },
            where: (table, args) => args.active ? `${table}.${q('archived', DB)} = ${bool(false, DB)}` : null 
          })
        } else {
          ({
            sqlJoin: (userTable, commentTable, args) => `${commentTable}.${q('author_id', DB)} = ${userTable}.${q('id', DB)} ${args.active ? `AND ${commentTable}.${q('archived', DB)} = ${bool(false, DB)}` : ''}`
          })
        }
      }
    },
    posts: {
      description: 'A list of Posts the user has written',
      type: PostConnection, 
      args: {
        search: { type: GraphQLString },
        ...PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs
      },
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: args => ({ // eslint-disable-line no-unused-vars
              created_at: 'desc',
              id: 'asc'
            })
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: args => ({ // eslint-disable-line no-unused-vars
              order: 'desc',
              key: [ 'created_at', 'id' ]
            })
          })
        } else {
          ({
            resolve: (user, args) => {
              user.posts.sort((a, b) => a.id - b.id)
              return connectionFromArray(user.posts, args)
            }
          })
        }
      },
      where: (table, args) => {
        if (args.search) return `lower(${table}.${q('body', DB)}) LIKE lower('%${args.search}%')`
      },
      ... do {
        if (STRATEGY === 'batch') {
          ({
            sqlBatch: {
              thisKey: 'author_id',
              parentKey: 'id'
            }
          })
        } else {
          ({
            sqlJoin: (userTable, postTable) => `${postTable}.${q('author_id', DB)} = ${userTable}.${q('id', DB)}`
          })
        }
      }
    },
    following: {
      description: 'Users that this user is following',
      type: UserConnection,
      args: PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs,
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: {
              created_at: 'DESC',
              followee_id: 'ASC'
            }
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'ASC',
              key: [ 'created_at', 'followee_id' ]
            }
          })
        } else {
          ({
            resolve: (user, args) => {
              return connectionFromArray(user.following, args)
            }
          })
        }
      },
      //junctionTable: q('relationships', DB),
      junctionTable: `(SELECT * FROM ${q('relationships', DB)})`,
      ... do {
        if (STRATEGY === 'batch' || STRATEGY === 'mix') {
          ({
            junctionTableKey: [ 'follower_id', 'followee_id' ],
            junctionBatch: {
              thisKey: 'follower_id',
              parentKey: 'id',
              sqlJoin: (relationTable, followeeTable) => `${relationTable}.${q('followee_id', DB)} = ${followeeTable}.${q('id', DB)}`
            }
          })
        } else {
          ({
            sqlJoins: [
              (followerTable, relationTable) => `${followerTable}.${q('id', DB)} = ${relationTable}.${q('follower_id', DB)}`,
              (relationTable, followeeTable) => `${relationTable}.${q('followee_id', DB)} = ${followeeTable}.${q('id', DB)}`
            ]
          })
        }
      }
    },
    writtenMaterial: {
      type: AuthoredConnection,
      args: PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs,
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: 'id'
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'ASC',
              key: [ 'id', 'created_at' ]
            }
          })
        } else {
          ({
            orderBy: 'id',
            resolve: (user, args) => {
              return connectionFromArray(user.following, args)
            }
          })
        }
      },
      ...STRATEGY === 'batch' ?
        { sqlBatch:
          { thisKey: 'author_id',
            parentKey: 'id' } } :
        { sqlJoin: (userTable, unionTable) => `${userTable}.${q('id', DB)} = ${unionTable}.${q('author_id', DB)}` }
    },
    favNums: {
      type: new GraphQLList(GraphQLInt),<commit_msg>another thing for fixing sorting stability<commit_after>import {
  GraphQLObjectType,
  GraphQLList,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean
} from 'graphql'

import {
  globalIdField,
  connectionArgs,
  forwardConnectionArgs,
  connectionDefinitions,
  connectionFromArray
} from 'graphql-relay'

import { PostConnection } from './Post'
import { CommentConnection } from './Comment'
import { nodeInterface } from './Node'
import { AuthoredConnection } from './Authored/Interface'
import { q, bool } from '../shared'

const { PAGINATE, STRATEGY, DB } = process.env

const User = new GraphQLObjectType({
  description: 'a stem contract account',
  name: 'User',
  sqlTable: `(SELECT * FROM ${q('accounts', DB)})`,
  uniqueKey: 'id',
  interfaces: [ nodeInterface ],
  fields: () => ({
    id: {
      description: 'The global ID for the Relay spec',
      ...globalIdField('User'),
      sqlDeps: [ 'id' ]
    },
    email: {
      type: GraphQLString,
      sqlColumn: 'email_address'
    },
    fullName: {
      description: 'A user\'s first and last name',
      type: GraphQLString,
      sqlDeps: [ 'first_name', 'last_name' ],
      resolve: user => `${user.first_name} ${user.last_name}`
    },
    comments: {
      description: 'Comments the user has written on people\'s posts',
      type: CommentConnection,
      args: {
        active: { type: GraphQLBoolean },
        ...PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs
      },
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({ orderBy: 'id' })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'desc',
              key: 'id'
            }
          })
        } else {
          ({
            resolve: (user, args) => {
              user.comments.sort((a, b) => a.id - b.id)
              return connectionFromArray(user.comments, args)
            }
          })
        }
      },
      ... do {
        if (STRATEGY === 'batch' || STRATEGY === 'mix') {
          ({
            sqlBatch: {
              thisKey: 'author_id',
              parentKey: 'id'
            },
            where: (table, args) => args.active ? `${table}.${q('archived', DB)} = ${bool(false, DB)}` : null 
          })
        } else {
          ({
            sqlJoin: (userTable, commentTable, args) => `${commentTable}.${q('author_id', DB)} = ${userTable}.${q('id', DB)} ${args.active ? `AND ${commentTable}.${q('archived', DB)} = ${bool(false, DB)}` : ''}`
          })
        }
      }
    },
    posts: {
      description: 'A list of Posts the user has written',
      type: PostConnection, 
      args: {
        search: { type: GraphQLString },
        ...PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs
      },
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: args => ({ // eslint-disable-line no-unused-vars
              created_at: 'desc',
              id: 'asc'
            })
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: args => ({ // eslint-disable-line no-unused-vars
              order: 'desc',
              key: [ 'created_at', 'id' ]
            })
          })
        } else {
          ({
            resolve: (user, args) => {
              user.posts.sort((a, b) => a.id - b.id)
              return connectionFromArray(user.posts, args)
            }
          })
        }
      },
      where: (table, args) => {
        if (args.search) return `lower(${table}.${q('body', DB)}) LIKE lower('%${args.search}%')`
      },
      ... do {
        if (STRATEGY === 'batch') {
          ({
            sqlBatch: {
              thisKey: 'author_id',
              parentKey: 'id'
            }
          })
        } else {
          ({
            sqlJoin: (userTable, postTable) => `${postTable}.${q('author_id', DB)} = ${userTable}.${q('id', DB)}`
          })
        }
      }
    },
    following: {
      description: 'Users that this user is following',
      type: UserConnection,
      args: PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs,
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: {
              created_at: 'DESC',
              followee_id: 'ASC'
            }
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'ASC',
              key: [ 'created_at', 'followee_id' ]
            }
          })
        } else {
          ({
            resolve: (user, args) => {
              return connectionFromArray(user.following, args)
            }
          })
        }
      },
      //junctionTable: q('relationships', DB),
      junctionTable: `(SELECT * FROM ${q('relationships', DB)})`,
      ... do {
        if (STRATEGY === 'batch' || STRATEGY === 'mix') {
          ({
            junctionTableKey: [ 'follower_id', 'followee_id' ],
            junctionBatch: {
              thisKey: 'follower_id',
              parentKey: 'id',
              sqlJoin: (relationTable, followeeTable) => `${relationTable}.${q('followee_id', DB)} = ${followeeTable}.${q('id', DB)}`
            }
          })
        } else {
          ({
            sqlJoins: [
              (followerTable, relationTable) => `${followerTable}.${q('id', DB)} = ${relationTable}.${q('follower_id', DB)}`,
              (relationTable, followeeTable) => `${relationTable}.${q('followee_id', DB)} = ${followeeTable}.${q('id', DB)}`
            ]
          })
        }
      }
    },
    writtenMaterial: {
      type: AuthoredConnection,
      args: PAGINATE === 'offset' ? forwardConnectionArgs : connectionArgs,
      sqlPaginate: !!PAGINATE,
      ... do {
        if (PAGINATE === 'offset') {
          ({
            orderBy: {
              id: 'ASC',
              created_at: 'ASC'
            }
          })
        } else if (PAGINATE === 'keyset') {
          ({
            sortKey: {
              order: 'ASC',
              key: [ 'id', 'created_at' ]
            }
          })
        } else {
          ({
            orderBy: 'id',
            resolve: (user, args) => {
              return connectionFromArray(user.following, args)
            }
          })
        }
      },
      ...STRATEGY === 'batch' ?
        { sqlBatch:
          { thisKey: 'author_id',
            parentKey: 'id' } } :
        { sqlJoin: (userTable, unionTable) => `${userTable}.${q('id', DB)} = ${unionTable}.${q('author_id', DB)}` }
    },
    favNums: {
      type: new GraphQLList(GraphQLInt),
{'fraction_non_alphanumeric': 0.16413396839797384, 'fraction_numerical': 0.0, 'mean_word_length': 1.5760467380720544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '1344968', 'n_tokens_mistral': 3983, 'n_tokens_neox': 3956, 'n_words': 815}