Skip to content

Commit

Permalink
companiesCount query
Browse files Browse the repository at this point in the history
  • Loading branch information
Fi1osof committed Mar 13, 2021
1 parent 01de7aa commit 8e94df8
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 33 deletions.
2 changes: 2 additions & 0 deletions server/nexus/generated/nexus.ts
Expand Up @@ -899,6 +899,7 @@ export interface NexusGenFieldTypes {
comments: NexusGenRootTypes['Comment'][] // [Comment!]!
commentsCount: number // Int!
companies: NexusGenRootTypes['Company'][] // [Company!]!
companiesCount: number // Int!
ratings: NexusGenRootTypes['Rating'][] // [Rating!]!
resources: NexusGenRootTypes['ResourceInterface'][] // [ResourceInterface!]!
resourcesCount: number // Int!
Expand Down Expand Up @@ -1141,6 +1142,7 @@ export interface NexusGenFieldTypeNames {
comments: 'Comment'
commentsCount: 'Int'
companies: 'Company'
companiesCount: 'Int'
ratings: 'Rating'
resources: 'ResourceInterface'
resourcesCount: 'Int'
Expand Down
5 changes: 5 additions & 0 deletions server/nexus/generated/schema.graphql
Expand Up @@ -346,6 +346,11 @@ type Query {
"""
companies(orderBy: CompaniesOrderByInput, skip: Int, take: Int): [Company!]!

"""
Подсчет количества компаний
"""
companiesCount: Int!

"""
Рейтинги заведений
"""
Expand Down
86 changes: 57 additions & 29 deletions server/nexus/types/Query/definitions/Resource/Company.ts
Expand Up @@ -10,7 +10,11 @@ import {
intArg,
ObjectDefinitionBlock,
} from 'nexus/dist/core'
import { NexusGenObjects } from 'server/nexus/generated/nexus'
import { PrismaContext } from 'server/nexus/context'
import {
// NexusGenArgTypes,
NexusGenObjects,
} from 'server/nexus/generated/nexus'
import { TemplateVarIDs } from '../../../../constants'
import { coordsResolver } from '../../resolvers/coords'

Expand Down Expand Up @@ -118,11 +122,27 @@ export type CompaniesResult = {
company_coords: string | undefined
}

function companiesQuery(this: PrismaContext['knex']) {
const query = this.from<bani684_site_content>(
'bani684_site_content as company'
)
.where({
template: 27,
deleted: false,
published: true,
hidemenu: false,
})

return query
}

const companiesResolver: FieldResolver<'Query', 'companies'> = (
_,
args,
{ knex }
ctx
) => {
const { knex } = ctx

const { take: limit, skip } = args

const orderByCoords = args.orderBy?.coords
Expand All @@ -138,23 +158,9 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = (
/**
* Получаем данные компаний
*/
const query = knex.from<CompaniesResult>(function (this: typeof knex) {
const companiesQuery = this.from<bani684_site_content>(
'bani684_site_content as company'
)
.select({
company_id: 'company.id',
company_uri: 'company.uri',
company_pagetitle: 'company.pagetitle',
company_createdby: 'company.createdby',
company_createdon: 'company.createdon',
company_description: 'company.description',
company_longtitle: 'company.longtitle',
company_published: 'company.published',
company_searchable: 'company.searchable',
company_template: 'company.template',
})

const query = knex.from<CompaniesResult>(function (this: typeof ctx.knex) {
const q2 = companiesQuery
.call(this)
/**
* Картинка компании
*/
Expand Down Expand Up @@ -194,23 +200,28 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = (
.select('tvs_coords.id as company_coords_id')
.select('tvs_coords.value as company_coords')

.where({
template: 27,
deleted: false,
published: true,
hidemenu: false,
.select({
company_id: 'company.id',
company_uri: 'company.uri',
company_pagetitle: 'company.pagetitle',
company_createdby: 'company.createdby',
company_createdon: 'company.createdon',
company_description: 'company.description',
company_longtitle: 'company.longtitle',
company_published: 'company.published',
company_searchable: 'company.searchable',
company_template: 'company.template',
})

.as('t')

/**
* Если сортировка по координатам, то берем компании только с координатами
*/
if (orderByCoords) {
companiesQuery.whereNotNull('tvs_coords.id')
}
// if (args.orderBy?.coords) {
// query.whereNotNull('tvs_coords.id')
// }

return companiesQuery
return q2
})

// if (limit) {
Expand Down Expand Up @@ -387,4 +398,21 @@ export const companies = (t: ObjectDefinitionBlock<'Query'>) => {
},
resolve: companiesResolver,
})

t.nonNull.int('companiesCount', {
description: 'Подсчет количества компаний',
resolve: async (_, _args, ctx) => {
const query = ctx.knex.from(function (this: typeof ctx.knex) {
return companiesQuery.call(this).count({ count: '*' }).as('t')
})

return await query.then((r) => {
const count = r[0]?.count

return typeof count === 'number'
? count
: (count && parseInt(count)) || 0
})
},
})
}
3 changes: 3 additions & 0 deletions src/gql/Query/company.graphql
Expand Up @@ -27,6 +27,9 @@ query companies(
# ...ListCompany
# }
# }

companiesCount

companies(skip: $skip, take: $take, orderBy: $orderBy) {
...Company_
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/gql/generated/companies.ts
Expand Up @@ -22,14 +22,15 @@ export type CompaniesQueryVariables = Types.Exact<{
}>;


export type CompaniesQuery = { __typename?: 'Query', companies: Array<(
export type CompaniesQuery = { __typename?: 'Query', companiesCount: number, companies: Array<(
{ __typename?: 'Company' }
& CompanyFragment
)> };


export const CompaniesDocument = gql`
query companies($skip: Int, $take: Int, $orderBy: CompaniesOrderByInput, $withContent: Boolean = false) {
companiesCount
companies(skip: $skip, take: $take, orderBy: $orderBy) {
...Company_
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/gql/generated/helpers/apollo-helpers.ts
Expand Up @@ -67,12 +67,13 @@ export type GalleryImageFieldPolicy = {
image?: FieldPolicy<any> | FieldReadFunction<any>,
title?: FieldPolicy<any> | FieldReadFunction<any>
};
export type QueryKeySpecifier = ('cities' | 'comments' | 'commentsCount' | 'companies' | 'ratings' | 'resources' | 'resourcesCount' | 'reviews' | 'topics' | 'user' | 'users' | 'usersCount' | 'votes' | 'votesByRating' | QueryKeySpecifier)[];
export type QueryKeySpecifier = ('cities' | 'comments' | 'commentsCount' | 'companies' | 'companiesCount' | 'ratings' | 'resources' | 'resourcesCount' | 'reviews' | 'topics' | 'user' | 'users' | 'usersCount' | 'votes' | 'votesByRating' | QueryKeySpecifier)[];
export type QueryFieldPolicy = {
cities?: FieldPolicy<any> | FieldReadFunction<any>,
comments?: FieldPolicy<any> | FieldReadFunction<any>,
commentsCount?: FieldPolicy<any> | FieldReadFunction<any>,
companies?: FieldPolicy<any> | FieldReadFunction<any>,
companiesCount?: FieldPolicy<any> | FieldReadFunction<any>,
ratings?: FieldPolicy<any> | FieldReadFunction<any>,
resources?: FieldPolicy<any> | FieldReadFunction<any>,
resourcesCount?: FieldPolicy<any> | FieldReadFunction<any>,
Expand Down
16 changes: 16 additions & 0 deletions src/modules/gql/generated/schema.json
Expand Up @@ -3064,6 +3064,22 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "companiesCount",
"description": "Подсчет количества компаний",
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "ratings",
"description": "Рейтинги заведений",
Expand Down
2 changes: 2 additions & 0 deletions src/modules/gql/generated/types.ts
Expand Up @@ -303,6 +303,8 @@ export interface Query {
commentsCount: Scalars['Int'];
/** Компании */
companies: Array<Company>;
/** Подсчет количества компаний */
companiesCount: Scalars['Int'];
/** Рейтинги заведений */
ratings: Array<Rating>;
/** Ресурсы */
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Cities/City/index.tsx
Expand Up @@ -81,7 +81,7 @@ const CityPage: Page<CityPageProps> = ({ city, ...other }) => {
pagination={{
page,
limit: companiesResponse.variables?.take || 0,
total: 100,
total: companiesResponse.data?.companiesCount || 0,
}}
{...other}
/>
Expand All @@ -90,6 +90,7 @@ const CityPage: Page<CityPageProps> = ({ city, ...other }) => {
}, [
city,
companiesResponse.data?.companies,
companiesResponse.data?.companiesCount,
companiesResponse.variables?.take,
other,
page,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/MainPage/index.tsx
Expand Up @@ -54,7 +54,7 @@ export const MainPage: Page<MainPageProps> = ({ city }): JSX.Element => {
pagination={{
page,
limit: companiesResponse.variables?.take || 0,
total: 100,
total: companiesResponse.data?.companiesCount || 0,
}}
/>
</>
Expand Down

0 comments on commit 8e94df8

Please sign in to comment.