forked from Joystream/hydra
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hydra-cli): support variant relations v3 (Joystream#380)
* chore(docs): [skip ci] doc update (Joystream#357) * GitBook: [master] 2 pages modified * GitBook: [master] one page modified * chore(docs): small interfaces docs fix * feat(hydra-cli): add variant relations affects: @dzlzv/hydra-cli * fix(hydra-cli): add support for otm relations affects: @dzlzv/hydra-cli * docs(hydra-cli): add docs for variant relations * fix(hydra-cli): duplicate function name affects: @dzlzv/hydra-cli * test(hydra-cli): variant relation tests affects: @dzlzv/hydra-cli Co-authored-by: dzhelezov <dzhelezov@gmail.com>
- Loading branch information
Showing
10 changed files
with
292 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
# Table of contents | ||
|
||
* [Hydra](README.md) | ||
* [Hydra CLI](packages/hydra-cli/README.md) | ||
* [Hydra Indexer](packages/hydra-indexer/README.md) | ||
* [Hydra Indexer Gateway](packages/hydra-indexer-gateway/README.md) | ||
* [Hydra Processor](packages/hydra-processor/README.md) | ||
* [Hydra Typegen](packages/hydra-typegen/README.md) | ||
* [Overview](docs/README.md) | ||
* [Query Node Manifest](docs/manifest-spec.md) | ||
* [Query Node Queries](docs/queries.md) | ||
* [Pagination](docs/paginate-query-results.md) | ||
* [Sorting](docs/sort-query-results.md) | ||
* [Mappings](docs/mappings/README.md) | ||
* [DatabaseManager](docs/mappings/databasemanager.md) | ||
* [SubstrateEvent](docs/mappings/substrateevent.md) | ||
* [Schema](docs/schema-spec/README.md) | ||
* [The Goodies](docs/schema-spec/the-query-goodies.md) | ||
* [Entities](docs/schema-spec/entities.md) | ||
* [Enums](docs/schema-spec/enums.md) | ||
* [Interfaces](docs/schema-spec/interfaces.md) | ||
* [Algebraic types](docs/schema-spec/variant-types.md) | ||
* [Full-text queries](docs/schema-spec/full-text-queries.md) | ||
* [Entity Relationships](docs/schema-spec/entity-relationship.md) | ||
* [Install Hydra](docs/install-hydra.md) | ||
* [Tutorial](docs/quick-start.md) | ||
* [GraphQL Entity Relationships](docs/graphql-entity-relationships.md) | ||
* [Architecture](docs/architecture.md) | ||
* [Migration to Hydra v2](migration-to-hydra-v2.md) | ||
* [What's new in Hydra v3](announcing-hydra-v3.md) | ||
|
||
- [Hydra](README.md) | ||
- [Hydra CLI](packages/hydra-cli/README.md) | ||
- [Hydra Indexer](packages/hydra-indexer/README.md) | ||
- [Hydra Indexer Gateway](packages/hydra-indexer-gateway/README.md) | ||
- [Hydra Processor](packages/hydra-processor/README.md) | ||
- [Hydra Typegen](packages/hydra-typegen/README.md) | ||
- [Overview](docs/README.md) | ||
- [Query Node Manifest](docs/manifest-spec.md) | ||
- [Graphql Queries](docs/queries.md) | ||
- [Pagination](docs/paginate-query-results.md) | ||
- [Sorting](docs/sort-query-results.md) | ||
- [Mappings](docs/mappings/README.md) | ||
- [DatabaseManager](docs/mappings/databasemanager.md) | ||
- [SubstrateEvent](docs/mappings/substrateevent.md) | ||
- [Schema](docs/schema-spec/README.md) | ||
- [The Goodies](docs/schema-spec/the-query-goodies.md) | ||
- [Entities](docs/schema-spec/entities.md) | ||
- [Enums](docs/schema-spec/enums.md) | ||
- [Interfaces](docs/schema-spec/interfaces.md) | ||
- [Algebraic types](docs/schema-spec/variant-types.md) | ||
- [Full-text queries](docs/schema-spec/full-text-queries.md) | ||
- [Entity Relationships](docs/schema-spec/entity-relationship.md) | ||
- [Cross filtering](docs/schema-spec/cross-filters.md) | ||
- [Variant relations](docs/schema-spec/variant-relations.md) | ||
- [Install Hydra](docs/install-hydra.md) | ||
- [Tutorial](docs/quick-start.md) | ||
- [GraphQL Entity Relationships](docs/graphql-entity-relationships.md) | ||
- [Architecture](docs/architecture.md) | ||
- [Migration to Hydra v2](migration-to-hydra-v2.md) | ||
- [What's new in Hydra v3](announcing-hydra-v3.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
description: Define variant types with relations | ||
--- | ||
|
||
# Variant Relations | ||
|
||
Variant types support entity relationship in a different way unlike the normal entity relationship. There are some limitations with variant relations: | ||
|
||
- Only one-to-many and many-to-one relations are supported | ||
- Reverse lookup is not supported | ||
|
||
Let's take a look an example: | ||
|
||
1. Schema: in the schema below there are two variant types with the relations | ||
|
||
```graphql | ||
type BoughtMemberEvent @entity { | ||
id: ID! | ||
name: String | ||
handle: String! | ||
} | ||
|
||
type InvitedMemberEvent @entity { | ||
id: ID! | ||
name: String | ||
handle: String! | ||
} | ||
|
||
type MemberInvitation @variant { | ||
event: InvitedMemberEvent! | ||
} | ||
|
||
type MemberPurchase @variant { | ||
event: BoughtMemberEvent! | ||
} | ||
|
||
union MemberSource = MemberInvitation | MemberPurchase | ||
|
||
type Member @entity { | ||
id: ID! | ||
isVerified: Boolean! | ||
handle: String! | ||
source: MemberSource! | ||
} | ||
``` | ||
|
||
2. Mappings: insert data into database | ||
|
||
For variant relations to work an additional field is added to variant type which is db only field (which means it is not visible in the graphql API). This field is will be generated from relation field name + 'id' ie. in the schema above relation name is `event` so the auto generated field name is `eventId`. This field is not optional and mapping author must set it properly. | ||
|
||
```ts | ||
async function handle_Member(db: DB, event: SubstrateEvent) { | ||
// Create an event from BoughtMemberEvent or MemberInvitation and save to db | ||
let event = new InvitedMemberEvent({ handle: 'joy' }) | ||
event = await db.save<InvitedMemberEvent>(event) | ||
|
||
// Create variant instance and set eventId property | ||
const invitation = new MemberInvitation() | ||
// Auto generated property, it holds primary key of the related entity | ||
invitation.eventId = event.id | ||
|
||
// Create new member and set the source property | ||
const member = new Member({ handle: 'hydra', isVerified: true }) | ||
member.source = invitation | ||
await db.save<Member>(member) | ||
} | ||
``` | ||
|
||
3. Query: fetch all members' `source`: | ||
|
||
```graphql | ||
query { | ||
members { | ||
source { | ||
__typename | ||
... on MemberInvitation { | ||
event { | ||
id | ||
name | ||
handle | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.