Skip to content

Commit

Permalink
fix(hydra-cli): fix relationships IN query (#407)
Browse files Browse the repository at this point in the history
affects: @dzlzv/hydra-cli, hydra-e2e-tests

- parameters merging were causing errors generating typeorm; replaced with simple object merges
- simplified SQL query for OTO relationship filters
  • Loading branch information
dzhelezov authored May 27, 2021
1 parent f351fa2 commit a4019a3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 36 deletions.
1 change: 0 additions & 1 deletion packages/hydra-cli/src/codegen/WarthogWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ export default class WarthogWrapper {
'src/index.ts',
'src/server.ts',
'src/pubsub.ts',
'src/utils.ts',
'src/WarthogBaseService.ts',
'src/processor.resolver.ts',
'tsconfig.json',
Expand Down
3 changes: 2 additions & 1 deletion packages/hydra-cli/src/generate/ModelRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as utils from './utils'
import { GraphQLEnumType } from 'graphql'
import { AbstractRenderer } from './AbstractRenderer'
import { withEnum } from './enum-context'
import { camelCase } from 'lodash'
import { camelCase, snakeCase } from 'lodash'
import { getRelationType } from '../model/Relation'

const debug = Debug('qnode-cli:model-renderer')
Expand Down Expand Up @@ -141,6 +141,7 @@ export class ModelRenderer extends AbstractRenderer {
returnTypeFunc,
rootArgType: entityName,
fieldName: f.name,
fieldNameColumnName: snakeCase(f.name),
rootArgName: 'r', // disable utils.camelCase(entityName) could be a reverved ts/js keyword ie `class`
returnType: utils.generateResolverReturnType(returnTypeFunc, f.isList),
relatedTsProp: f.relation.relatedTsProp,
Expand Down
1 change: 1 addition & 0 deletions packages/hydra-cli/src/model/Relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Relation {
export interface FieldResolver {
returnTypeFunc: string
fieldName: string
fieldNameColumnName: string
rootArgName: string
rootArgType: string
returnType: string
Expand Down
17 changes: 4 additions & 13 deletions packages/hydra-cli/src/templates/entities/resolver.ts.mst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { Inject } from 'typedi';
import { Min } from 'class-validator'
import { Fields, StandardDeleteResponse, UserId, PageInfo, RawFields, NestedFields } from 'warthog';

import { mergeParameterKeys } from '../../utils';

import {
{{className}}CreateInput,
{{className}}CreateManyArgs,
Expand Down Expand Up @@ -131,7 +129,7 @@ export class {{className}}Resolver {
'main'
).take(undefined); // remove LIMIT

let parameters = mergeParameterKeys({}, 'main', mainQuery.getQueryAndParameters()[1]);
let parameters = mainQuery.getParameters();

{{#crossFilters}}

Expand All @@ -150,13 +148,9 @@ export class {{className}}Resolver {


mainQuery = mainQuery
.leftJoin('{{typeormAliasName}}.{{fieldName}}', '{{fieldName}}')
.andWhere(`{{fieldName}}.id IN (${ {{fieldName}}Query.getQuery() })`);
.andWhere(`"{{typeormAliasName}}"."{{fieldNameColumnName}}_id" IN (${ {{fieldName}}Query.getQuery() })`);

parameters = parameters = mergeParameterKeys(
parameters,
'{{fieldName}}',
{{fieldName}}Query.getQueryAndParameters()[1]);;
parameters = { ...parameters, ...{{fieldName}}Query.getParameters() };

}
{{/relationType.isTO}}
Expand All @@ -174,10 +168,7 @@ export class {{className}}Resolver {
'{{fieldName}}'
).take(undefined); //remove the default LIMIT

parameters = mergeParameterKeys(
parameters,
'{{fieldName}}',
{{fieldName}}Query.getQueryAndParameters()[1]);
parameters = { ...parameters, ...{{fieldName}}Query.getParameters() }

const subQueryFiltered = this.service
.getQueryBuilder()
Expand Down
21 changes: 0 additions & 21 deletions packages/hydra-cli/src/templates/graphql-server/src/utils.ts.mst

This file was deleted.

10 changes: 10 additions & 0 deletions packages/hydra-e2e-tests/test/e2e/api/graphql-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ export const HOOKS = gql`
}
}
`

export const TRANSFER_IN_QUERY = gql`
query {
transfers(
where: { id_in: ["xxxx", "yyyy"], fromAccount: { hex_endsWith: "abc" } }
) {
id
}
}
`
13 changes: 13 additions & 0 deletions packages/hydra-e2e-tests/test/e2e/transfer-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
queryInterface,
getProcessorStatus,
accountByOutgoingTxValue,
getGQLClient,
} from './api/processor-api'
import { transfer } from './api/substrate-api'
import pWaitFor from 'p-wait-for'
import {
ACCOUNTS_BY_VALUE_GT_EVERY,
ACCOUNTS_BY_VALUE_GT_NONE,
ACCOUNTS_BY_VALUE_GT_SOME,
TRANSFER_IN_QUERY,
} from './api/graphql-queries'
// You need to be connected to a development chain for this example to work.
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
Expand Down Expand Up @@ -135,6 +137,17 @@ describe('end-to-end transfer tests', () => {
expect(accs.length).to.be.equal(1, 'none tx vals > 200000: false') // ONLY BOB, it has no outgoing txs
})

it('correctly handles IN query', async () => {
const transfers = (
await getGQLClient().request<{
transfers: { id: string }[]
}>(TRANSFER_IN_QUERY)
).transfers

// simply check it executes normally
expect(transfers.length).to.be.equal(0, 'should execute IN query')
})

it('founds an account by incoming tx value (every)', async () => {
let accs = await accountByOutgoingTxValue(
ACCOUNTS_BY_VALUE_GT_EVERY,
Expand Down

0 comments on commit a4019a3

Please sign in to comment.