Skip to content

Commit

Permalink
fix: load subscriptions relations
Browse files Browse the repository at this point in the history
  • Loading branch information
IcanDivideBy0 committed Dec 17, 2024
1 parent 4a6ccd7 commit cdb30b5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Updated send_notification PG function to include `_block height` and entity `_id` (#2626)

## [16.1.0] - 2024-12-11
### Changed
Expand Down
6 changes: 3 additions & 3 deletions packages/node-core/src/db/sync-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ BEGIN
'mutation_type', TG_OP,
'_entity', row);
IF payload -> '_entity' ? '_block_range' then
payload = payload #- '{"_entity","_id"}';
payload = payload #- '{"_entity","_block_range"}';
payload = payload #- '{"_entity","_block_range"}';
payload = payload || jsonb_build_object('_block_height', lower(row._block_range));
IF NOT upper_inf(row._block_range) then
-- Check if a newer version of the entity exists to determine operation
-- Check if a newer version of the entity exists to determine operation
EXECUTE FORMAT(
'SELECT EXISTS (SELECT 1 FROM "${schema}".%I WHERE id = $1 AND lower(_block_range) = upper($2))',
TG_TABLE_NAME
Expand Down
2 changes: 1 addition & 1 deletion packages/query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Fixed
- Subscriptions `_entity` field now includes camel cased properties (#2626)
- Subscriptions `_entity` field now returns all properties (#2626)

## [2.19.0] - 2024-12-11
### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/query/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
path: WS_ROUTE,
});

this.wsCleanup = useServer({schema}, wsServer);
this.wsCleanup = useServer({schema, context: {pgClient: this.pgPool}}, wsServer);
}

app.use(PinoLogger(PinoConfig));
Expand Down
19 changes: 13 additions & 6 deletions packages/query/src/graphql/plugins/PgSubscriptionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function makePayload(entityType: string): {type: DocumentNode; name: string} {
}

export const PgSubscriptionPlugin = makeExtendSchemaPlugin((build) => {
const {inflection, pgIntrospectionResultsByKind} = build;
const {inflection, pgIntrospectionResultsByKind, pgSql: sql} = build;

const typeDefs = [
gql`
Expand Down Expand Up @@ -68,11 +68,18 @@ export const PgSubscriptionPlugin = makeExtendSchemaPlugin((build) => {

resolvers[payloadName] = {
_entity: {
resolve: ({_entity}) => {
return Object.entries(_entity).reduce((acc, [key, value]) => {
const attr = table.attributes.find((attr) => attr.name === key);
return Object.assign(acc, {[inflection.column(attr)]: value});
}, _entity);
resolve: async ({_block_height, _entity}, args, context, resolveInfo) => {
const [row] = await resolveInfo.graphile.selectGraphQLResultFromTable(
sql.identifier(table.namespace.name, table.name),
(tableAlias, queryBuilder) => {
queryBuilder.context.args ??= {};
queryBuilder.context.args.blockHeight = sql.fragment`${sql.value(_block_height.toString())}::bigint`;
queryBuilder.where(sql.fragment`${tableAlias}._id = ${sql.value(_entity._id)}`);
queryBuilder.limit(1);
}
);

return row;
},
},
};
Expand Down

0 comments on commit cdb30b5

Please sign in to comment.