remove extra queries

This commit is contained in:
keyan 2023-02-03 18:08:08 -06:00
parent 3a9994a9a6
commit 36ddb25b46
5 changed files with 46 additions and 47 deletions

View File

@ -12,7 +12,7 @@ import { msatsToSats } from '../../lib/format'
import { parse } from 'tldts' import { parse } from 'tldts'
import uu from 'url-unshort' import uu from 'url-unshort'
async function comments (me, models, id, sort) { async function comments (me, models, id, sort, root) {
let orderBy let orderBy
switch (sort) { switch (sort) {
case 'top': case 'top':
@ -39,7 +39,7 @@ async function comments (me, models, id, sort) {
WHERE true WHERE true
${await filterClause(me, models)}) ${await filterClause(me, models)})
SELECT * FROM base ORDER BY sort_path`, Number(id)) SELECT * FROM base ORDER BY sort_path`, Number(id))
return nestComments(flat, id)[0] return nestComments(flat, id, root)[0]
} }
export async function getItem (parent, { id }, { me, models }) { export async function getItem (parent, { id }, { me, models }) {
@ -871,7 +871,7 @@ export default {
if (item.comments) { if (item.comments) {
return item.comments return item.comments
} }
return comments(me, models, item.id, 'hot') return comments(me, models, item.id, 'hot', item)
}, },
upvotes: async (item, args, { models }) => { upvotes: async (item, args, { models }) => {
const [{ count }] = await models.$queryRaw(` const [{ count }] = await models.$queryRaw(`
@ -946,6 +946,9 @@ export default {
if (!item.rootId) { if (!item.rootId) {
return null return null
} }
if (item.root) {
return item.root
}
return await models.item.findUnique({ where: { id: item.rootId } }) return await models.item.findUnique({ where: { id: item.rootId } })
}, },
parent: async (item, args, { models }) => { parent: async (item, args, { models }) => {
@ -1082,10 +1085,11 @@ const createItem = async (parent, { title, url, text, boost, forward, bounty, pa
return item return item
} }
function nestComments (flat, parentId) { function nestComments (flat, parentId, root) {
const result = [] const result = []
let added = 0 let added = 0
for (let i = 0; i < flat.length;) { for (let i = 0; i < flat.length;) {
flat[i].root = root
if (!flat[i].comments) flat[i].comments = [] if (!flat[i].comments) flat[i].comments = []
if (Number(flat[i].parentId) === Number(parentId)) { if (Number(flat[i].parentId) === Number(parentId)) {
result.push(flat[i]) result.push(flat[i])
@ -1093,7 +1097,7 @@ function nestComments (flat, parentId) {
i++ i++
} else if (result.length > 0) { } else if (result.length > 0) {
const item = result[result.length - 1] const item = result[result.length - 1]
const [nested, newAdded] = nestComments(flat.slice(i), item.id) const [nested, newAdded] = nestComments(flat.slice(i), item.id, root)
if (newAdded === 0) { if (newAdded === 0) {
break break
} }

View File

@ -14,11 +14,7 @@ export const ITEM_FIELDS = gql`
streak streak
id id
} }
fwdUser { position
name
streak
id
}
sats sats
upvotes upvotes
boost boost
@ -45,9 +41,23 @@ export const ITEM_FIELDS = gql`
status status
uploadId uploadId
mine mine
}`
export const ITEM_FULL_FIELDS = gql`
${ITEM_FIELDS}
fragment ItemFullFields on Item {
...ItemFields
text
fwdUser {
name
streak
id
}
root { root {
id id
title title
bounty
bountyPaidTo
sub { sub {
name name
} }
@ -87,11 +97,9 @@ export const ITEMS = gql`
cursor cursor
items { items {
...ItemFields ...ItemFields
position
}, },
pins { pins {
...ItemFields ...ItemFields
position
} }
} }
}` }`
@ -104,50 +112,45 @@ export const TOP_ITEMS = gql`
cursor cursor
items { items {
...ItemFields ...ItemFields
position
}, },
pins { pins {
...ItemFields ...ItemFields
position
} }
} }
}` }`
export const OUTLAWED_ITEMS = gql` export const OUTLAWED_ITEMS = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
query outlawedItems($cursor: String) { query outlawedItems($cursor: String) {
outlawedItems(cursor: $cursor) { outlawedItems(cursor: $cursor) {
cursor cursor
items { items {
...ItemFields ...ItemFullFields
text
} }
} }
}` }`
export const BORDERLAND_ITEMS = gql` export const BORDERLAND_ITEMS = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
query borderlandItems($cursor: String) { query borderlandItems($cursor: String) {
borderlandItems(cursor: $cursor) { borderlandItems(cursor: $cursor) {
cursor cursor
items { items {
...ItemFields ...ItemFullFields
text
} }
} }
}` }`
export const FREEBIE_ITEMS = gql` export const FREEBIE_ITEMS = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
query freebieItems($cursor: String) { query freebieItems($cursor: String) {
freebieItems(cursor: $cursor) { freebieItems(cursor: $cursor) {
cursor cursor
items { items {
...ItemFields ...ItemFullFields
text
} }
} }
}` }`
@ -167,14 +170,13 @@ export const POLL_FIELDS = gql`
}` }`
export const ITEM = gql` export const ITEM = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
${POLL_FIELDS} ${POLL_FIELDS}
query Item($id: ID!) { query Item($id: ID!) {
item(id: $id) { item(id: $id) {
...ItemFields ...ItemFullFields
...PollFields ...PollFields
text
} }
}` }`
@ -189,15 +191,13 @@ export const COMMENTS_QUERY = gql`
` `
export const ITEM_FULL = gql` export const ITEM_FULL = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
${POLL_FIELDS} ${POLL_FIELDS}
${COMMENTS} ${COMMENTS}
query Item($id: ID!) { query Item($id: ID!) {
item(id: $id) { item(id: $id) {
...ItemFields ...ItemFullFields
prior prior
position
text
...PollFields ...PollFields
comments { comments {
...CommentsRecursive ...CommentsRecursive
@ -206,11 +206,10 @@ export const ITEM_FULL = gql`
}` }`
export const ITEM_WITH_COMMENTS = gql` export const ITEM_WITH_COMMENTS = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
${COMMENTS} ${COMMENTS}
fragment ItemWithComments on Item { fragment ItemWithComments on Item {
...ItemFields ...ItemFullFields
text
comments { comments {
...CommentsRecursive ...CommentsRecursive
} }
@ -228,13 +227,12 @@ export const BOUNTY_ITEMS_BY_USER_NAME = gql`
}` }`
export const ITEM_SEARCH = gql` export const ITEM_SEARCH = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
query Search($q: String, $cursor: String, $sort: String, $what: String, $when: String) { query Search($q: String, $cursor: String, $sort: String, $what: String, $when: String) {
search(q: $q, cursor: $cursor, sort: $sort, what: $what, when: $when) { search(q: $q, cursor: $cursor, sort: $sort, what: $what, when: $when) {
cursor cursor
items { items {
...ItemFields ...ItemFullFields
text
searchTitle searchTitle
searchText searchText
} }

View File

@ -1,9 +1,9 @@
import { gql } from '@apollo/client' import { gql } from '@apollo/client'
import { ITEM_FIELDS } from './items' import { ITEM_FULL_FIELDS } from './items'
import { INVITE_FIELDS } from './invites' import { INVITE_FIELDS } from './invites'
export const NOTIFICATIONS = gql` export const NOTIFICATIONS = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
${INVITE_FIELDS} ${INVITE_FIELDS}
query Notifications($cursor: String, $inc: String) { query Notifications($cursor: String, $inc: String) {
@ -16,7 +16,7 @@ export const NOTIFICATIONS = gql`
sortTime sortTime
mention mention
item { item {
...ItemFields ...ItemFullFields
text text
} }
} }
@ -24,7 +24,7 @@ export const NOTIFICATIONS = gql`
sortTime sortTime
earnedSats earnedSats
item { item {
...ItemFields ...ItemFullFields
text text
} }
} }
@ -48,7 +48,7 @@ export const NOTIFICATIONS = gql`
... on Reply { ... on Reply {
sortTime sortTime
item { item {
...ItemFields ...ItemFullFields
text text
} }
} }

View File

@ -189,11 +189,9 @@ export const USER_WITH_POSTS = gql`
cursor cursor
items { items {
...ItemFields ...ItemFields
position
} }
pins { pins {
...ItemFields ...ItemFields
position
} }
} }
}` }`

View File

@ -1,5 +1,5 @@
import { gql } from '@apollo/client' import { gql } from '@apollo/client'
import { ITEM_FIELDS } from './items' import { ITEM_FULL_FIELDS } from './items'
import { USER_FIELDS } from './users' import { USER_FIELDS } from './users'
export const INVOICE = gql` export const INVOICE = gql`
@ -27,7 +27,7 @@ export const WITHDRAWL = gql`
}` }`
export const WALLET_HISTORY = gql` export const WALLET_HISTORY = gql`
${ITEM_FIELDS} ${ITEM_FULL_FIELDS}
${USER_FIELDS} ${USER_FIELDS}
query WalletHistory($cursor: String, $inc: String) { query WalletHistory($cursor: String, $inc: String) {
@ -46,8 +46,7 @@ export const WALLET_HISTORY = gql`
type type
description description
item { item {
...ItemFields ...ItemFullFields
text
} }
} }
cursor cursor