-
Notifications
You must be signed in to change notification settings - Fork 348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add distinct query plugin #1274
Merged
+132
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7106181
Add distinct query plugin
stwiname a9ce714
Clean up log
stwiname f6327e9
Fix distinct not being provided to query
stwiname c151d92
Uppercase enum to be consistent with other enums
stwiname 5172842
Update dictionary queries to try distinct argument
stwiname File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,107 @@ | ||
// Copyright 2022 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {Build, Plugin} from 'graphile-build'; | ||
import {PgClass, QueryBuilder} from 'graphile-build-pg'; | ||
import type {GraphQLEnumType} from 'graphql'; | ||
import * as PgSql from 'pg-sql2'; | ||
|
||
type Extend = <T1, T2>(base: T1, extra: T2, hint?: string) => T1 & T2; | ||
|
||
const getEnumName = (entityName: string): string => { | ||
return `${entityName}_distinct_enum`; | ||
}; | ||
|
||
export const PgDistinctPlugin: Plugin = (builder) => { | ||
// Creates enums for each entity based on their fields | ||
builder.hook( | ||
'init', | ||
(args, build) => { | ||
const { | ||
graphql: {GraphQLEnumType}, | ||
newWithHooks, | ||
pgIntrospectionResultsByKind, | ||
} = build; | ||
|
||
pgIntrospectionResultsByKind.class.forEach((cls: PgClass) => { | ||
if (!cls.isSelectable || build.pgOmit(cls, 'order')) return; | ||
if (!cls.namespace) return; | ||
|
||
const enumTypeName = getEnumName(cls.name); | ||
|
||
const entityEnumValues: Record<string, {value: number}> = {}; | ||
cls?.attributes?.forEach((attr, index) => { | ||
if (attr.name.indexOf('_') !== 0) { | ||
entityEnumValues[attr.name.toUpperCase()] = {value: index}; | ||
} | ||
}); | ||
|
||
newWithHooks( | ||
GraphQLEnumType, | ||
{ | ||
name: enumTypeName, | ||
values: entityEnumValues, | ||
}, | ||
{ | ||
__origin: `Adding connection "distinct" enum type for ${cls.name}.`, | ||
pgIntrospection: cls, | ||
}, | ||
true | ||
); | ||
}); | ||
|
||
return args; | ||
}, | ||
['AddDistinctEnumsPlugin'] | ||
); | ||
|
||
// Extends schema and modifies the query | ||
builder.hook( | ||
'GraphQLObjectType:fields:field:args', | ||
(args, build, {addArgDataGenerator, scope: {pgFieldIntrospection}}) => { | ||
const { | ||
extend, | ||
graphql: {GraphQLList}, | ||
pgSql: sql, | ||
} = build as Build & {extend: Extend; pgSql: typeof PgSql}; | ||
|
||
const enumTypeName = getEnumName(pgFieldIntrospection?.name); | ||
const enumType = build.getTypeByName(enumTypeName) as GraphQLEnumType; | ||
|
||
if (!enumType) { | ||
return args; | ||
} | ||
|
||
addArgDataGenerator(({distinct}) => ({ | ||
pgQuery: (queryBuilder: QueryBuilder) => { | ||
distinct?.map((field: number) => { | ||
const {name} = enumType.getValues()[field]; | ||
const fieldName = name.toLowerCase(); | ||
if (!pgFieldIntrospection?.attributes?.map((a) => a.name).includes(fieldName)) { | ||
console.warn(`Distinct field ${fieldName} doesn't exist on entity ${pgFieldIntrospection?.name}`); | ||
|
||
return; | ||
} | ||
|
||
const id = sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(fieldName)}`; | ||
|
||
// Dependent on https://github.com/graphile/graphile-engine/pull/805 | ||
(queryBuilder as any).distinctOn(id); | ||
}); | ||
}, | ||
})); | ||
|
||
return extend( | ||
args, | ||
{ | ||
distinct: { | ||
description: 'Fields to be distinct', | ||
defaultValue: null, | ||
type: new GraphQLList(enumType), | ||
}, | ||
}, | ||
'DistinctPlugin' | ||
); | ||
} | ||
); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we useDistinct, we no longer need to have
if the distinct blocks in between difference is large than queryEndBlock threshold 10000, this will become inefficient.
For example expected distinct return 1999, 8999, 28999... (total number of batch size blocks)
but due to query limit
we will only have 2 blocks in return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to retain
lessThan
for the work that @bz888 has done. But thinking about that, thequeryEndBlock
could be set to the height when a next dataSource is introduced.Maybe we should save this for another task?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's do it in another task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1381