Skip to content

Commit

Permalink
Order returned TypeInstances by path:revision and createdAt (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Feb 23, 2022
1 parent 041d69e commit f6f487e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 33 deletions.
3 changes: 3 additions & 0 deletions hub-js/graphql/local/examples.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ mutation DeleteLockedTypeInstance($typeInstanceID: ID!) {

fragment TypeInstance on TypeInstance {
id
createdAt {
formatted
}
typeRef {
path
revision
Expand Down
35 changes: 30 additions & 5 deletions hub-js/graphql/local/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ directive @cypher(statement: String) on FIELD_DEFINITION
directive @index on FIELD_DEFINITION
directive @id on FIELD_DEFINITION

"""
Represents date, time and time zone
"""
scalar DateTime

"""
Arbitrary data
"""
Expand All @@ -34,16 +39,30 @@ scalar LockOwnerID

type TypeInstance {
id: ID! @id

createdAt: DateTime
lockedBy: LockOwnerID

"""
Common properties for all TypeInstances which cannot be changed
"""
typeRef: TypeInstanceTypeReference!
@relation(name: "OF_TYPE", direction: "OUT")
uses: [TypeInstance!]! @relation(name: "USES", direction: "OUT")
usedBy: [TypeInstance!]! @relation(name: "USES", direction: "IN")
"""
Returns TypeInstances that are used. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
uses: [TypeInstance!]!
@cypher(
statement: "MATCH (this)-[:USES]->(ti:TypeInstance)-[:OF_TYPE]-(tr: TypeInstanceTypeReference) RETURN ti ORDER BY tr.path ASC, tr.revision DESC, ti.createdAt DESC"
)
"""
Returns TypeInstances that uses this TypeInstance. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
usedBy: [TypeInstance!]!
@cypher(
statement: "MATCH (this)<-[:USES]-(ti:TypeInstance)-[:OF_TYPE]-(tr: TypeInstanceTypeReference) RETURN ti ORDER BY tr.path ASC, tr.revision DESC, ti.createdAt DESC"
)
backend: TypeInstanceBackendReference! @relation(name: "STORED_IN", direction: "OUT")

latestResourceVersion: TypeInstanceResourceVersion
Expand Down Expand Up @@ -286,6 +305,10 @@ input UnlockTypeInstancesInput {
}

type Query {
"""
Returns all TypeInstances. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
typeInstances(filter: TypeInstanceFilter = {}): [TypeInstance!]!
@cypher(
statement: """
Expand Down Expand Up @@ -334,7 +357,9 @@ type Query {
)
)
RETURN DISTINCT ti
WITH DISTINCT ti, typeRef
ORDER BY typeRef.path ASC, typeRef.revision DESC, ti.createdAt DESC
RETURN ti
"""
)

Expand All @@ -356,7 +381,7 @@ type Mutation {
createTypeInstance(in: CreateTypeInstanceInput!): TypeInstance!
@cypher(
statement: """
CREATE (ti:TypeInstance {id: apoc.create.uuid()})
CREATE (ti:TypeInstance {id: apoc.create.uuid(), createdAt: datetime()})
// Backend
WITH *
Expand Down
3 changes: 2 additions & 1 deletion hub-js/src/schema/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const schema = makeAugmentedSchema({
// create TypeInstances
const createTypeInstanceResult = await tx.run(
`UNWIND $typeInstances AS typeInstance
CREATE (ti:TypeInstance {id: apoc.create.uuid()})
CREATE (ti:TypeInstance {id: apoc.create.uuid(), createdAt: datetime()})
// Backend
WITH *
Expand Down Expand Up @@ -500,6 +500,7 @@ export async function ensureCoreStorageTypeInstance(context: ContextWithDriver)
MERGE (ti)-[:CONTAINS]->(tir)
MERGE (tir)-[:DESCRIBED_BY]->(metadata:TypeInstanceResourceVersionMetadata)
MERGE (tir)-[:SPECIFIED_BY]->(spec)
SET ti.createdAt = CASE WHEN ti.createdAt IS NOT NULL THEN ti.createdAt ELSE datetime() END
RETURN ti
`, {value});
Expand Down
13 changes: 9 additions & 4 deletions pkg/hub/api/graphql/local/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f6f487e

Please sign in to comment.