Skip to content

Commit

Permalink
fix(hydra-cli): support entity relations in interfaces (#275)
Browse files Browse the repository at this point in the history
* [hydra-cli] keep the relations on the implementer when interface has relations

* [hydra-cli] add type defination

* [hydra-cli] set "limit+offset" as limit and leave "offset" as it is

* [hydra-cli] add tests

* hydra-cli: update scaffold schema for e2e-test

* hydra-cli: set offset "0"
  • Loading branch information
metmirr committed Mar 5, 2021
1 parent 427dbc9 commit 122e593
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 15 deletions.
8 changes: 7 additions & 1 deletion packages/hydra-cli/src/generate/utils.ts
Expand Up @@ -57,8 +57,14 @@ export function ownFields(o: ObjectType): Field[] {
}

const intrFields = o.interfaces[0].fields || []
return _.differenceBy(o.fields, intrFields, 'name')
const fields = _.differenceBy(o.fields, intrFields, 'name')
// Add non-scalar fields back to the object
_.intersectionBy(o.fields, intrFields, 'name').forEach((f) => {
if (!f.isBuildinType && f.relation) fields.push(f)
})
return fields
}

export function generateJoinColumnName(name: string): string {
return snakeCase(name.concat('_id'))
}
Expand Down
4 changes: 2 additions & 2 deletions packages/hydra-cli/src/templates/interfaces/service.ts.mst
Expand Up @@ -39,13 +39,13 @@ export class {{className}}Service {
try {
// fetching all the fields to allow type-dependent field resolutions
{{#subclasses}}
{{camelNamePlural}} = await this.{{camelName}}Service.find(where, ob, 0, _limit + _offset);
{{camelNamePlural}} = await this.{{camelName}}Service.find(where, ob, _limit + _offset, 0);
{{/subclasses}}
} finally {
await queryRunner.commitTransaction();
}

let collect = [{{#subclasses}}...{{camelNamePlural}}, {{/subclasses}}];
let collect: any[] = [{{#subclasses}}...{{camelNamePlural}}, {{/subclasses}}];
if (ob) {
// NB: copied from warthog's BaseService
// TODO: allow multiple sorts
Expand Down
21 changes: 19 additions & 2 deletions packages/hydra-cli/src/templates/scaffold/schema.graphql
Expand Up @@ -6,10 +6,27 @@ type Transfer @entity {
comment: String @fulltext(query: "commentSearch")
block: Int!
insertedAt: DateTime!
}
}

" Tracks block timestamps "
type BlockTimestamp @entity {
blockNumber: BigInt!
timestamp: BigInt!
}
}

############## Test purpose only @hydra-e2e-tests #################
# To make sure graphql api is generated as expected
type Extrinsic @entity {
id: ID!
hash: String!
}
interface Event @entity {
indexInBlock: Int!
inExtrinsic: Extrinsic!
}
type BoughtMemberEvent implements Event @entity {
id: ID!
indexInBlock: Int!
inExtrinsic: Extrinsic!
}
###################################################
31 changes: 31 additions & 0 deletions packages/hydra-cli/test/helpers/ModelRenderer.test.ts
Expand Up @@ -444,6 +444,37 @@ describe('ModelRenderer', () => {
expect(rendered).to.include('@InterfaceType')
})

it('should render interface without relation fields', () => {
const model = fromStringSchema(`
type Extrinsic @entity {
hash: String!
}
interface Event @entity {
indexInBlock: Int!
inExtrinsic: Extrinsic!
}
type BoughtMemberEvent implements Event @entity {
memberId: Int!
indexInBlock: Int!
inExtrinsic: Extrinsic!
}`)
generator = new ModelRenderer(
model,
model.lookupInterface('Event'),
enumCtxProvider
)
let rendered = generator.render(modelTemplate)
expect(rendered).to.not.include('inExtrinsic')

generator = new ModelRenderer(
model,
model.lookupEntity('BoughtMemberEvent'),
enumCtxProvider
)
rendered = generator.render(modelTemplate)
expect(rendered).to.include('inExtrinsic')
})

it('should import unions', function () {
const model = fromStringSchema(`
union Poor = HappyPoor | Miserable
Expand Down
30 changes: 22 additions & 8 deletions packages/hydra-e2e-tests/test/e2e/api/graphql-queries.ts
Expand Up @@ -61,13 +61,27 @@ query {
}
`

export const PROCESSOR_SUBSCRIPTION = `
subscription {
stateSubscription {
indexerHead
chainHead
lastProcessedEvent
lastCompleteBlock
export const INTERFACE_TYPES_WITH_RELATIONSHIP = gql`
query InterfaceQuery {
events {
indexInBlock
... on BoughtMemberEvent {
inExtrinsic {
id
hash
}
}
}
}
`

export const PROCESSOR_SUBSCRIPTION = gql`
subscription {
stateSubscription {
indexerHead
chainHead
lastProcessedEvent
lastCompleteBlock
}
}
}
`
7 changes: 7 additions & 0 deletions packages/hydra-e2e-tests/test/e2e/api/processor-api.ts
Expand Up @@ -8,6 +8,7 @@ import {
FETCH_INSERTED_AT_FIELD_FROM_TRANSFER,
FTS_COMMENT_QUERY_WITH_WHERE_CONDITION,
LAST_BLOCK_TIMESTAMP,
INTERFACE_TYPES_WITH_RELATIONSHIP,
PROCESSOR_SUBSCRIPTION,
} from './graphql-queries'
import { SubscriptionClient } from 'graphql-subscriptions-client'
Expand Down Expand Up @@ -137,3 +138,9 @@ export async function getProcessorStatus(): Promise<ProcessorStatus> {
await pWaitFor(() => processorStatus !== undefined)
return processorStatus as ProcessorStatus
}

export async function queryInterface(): Promise<{ events: [] }> {
return await getGQLClient().request<{
events: []
}>(INTERFACE_TYPES_WITH_RELATIONSHIP)
}
7 changes: 7 additions & 0 deletions packages/hydra-e2e-tests/test/e2e/transfer-e2e.test.ts
Expand Up @@ -5,6 +5,7 @@ import {
findTransfersByComment,
findTransfersByCommentAndWhereCondition,
findTransfersByValue,
queryInterface,
getProcessorStatus,
} from './api/processor-api'
import { transfer } from './api/substrate-api'
Expand Down Expand Up @@ -84,4 +85,10 @@ describe('end-to-end transfer tests', () => {
'Full text search with filtering should find some comment'
)
})

it('performs query on interface types, expect implementer to hold relationship', async () => {
const { events } = await queryInterface()
// We don't expect any data only testing Graphql API types
expect(events.length).to.be.equal(0, 'should not find any event.')
})
})
21 changes: 19 additions & 2 deletions packages/sample/schema.graphql
Expand Up @@ -6,10 +6,27 @@ type Transfer @entity {
comment: String @fulltext(query: "commentSearch")
block: Int!
insertedAt: DateTime!
}
}

" Tracks block timestamps "
type BlockTimestamp @entity {
blockNumber: BigInt!
timestamp: BigInt!
}
}

############## Test purpose only @hydra-e2e-tests #################
# To make sure graphql api is generated as expected
type Extrinsic @entity {
id: ID!
hash: String!
}
interface Event @entity {
indexInBlock: Int!
inExtrinsic: Extrinsic!
}
type BoughtMemberEvent implements Event @entity {
id: ID!
indexInBlock: Int!
inExtrinsic: Extrinsic!
}
###################################################

0 comments on commit 122e593

Please sign in to comment.