To facilitate better answering of questions, if you have a question, please fill in the following info. Otherwise, please delete the template.
What I want to do
I want to sort a union
field by a field that exists on all types in the union
. Pretty sure this isn’t possible, but I want to order the union
field (Paragraph.text
) not by just one field on one of union
types (Word | Phrase
), but by the .start
field on both types. Phrase
currently doesn’t have a .start
field, was hoping I could use a var in place of the .start
field on Phrase
so that the value is calculated dynamically.
Schema
type Paragraph @auth(
query: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
add: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
update: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
delete: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
) {
id: ID!
start: Float
end: Float
speaker: Speaker!
text: [Text]
words: [Word!] @hasInverse(field: paragraph)
phrases: [Phrase] @hasInverse(field: paragraph)
transcript: Transcript!
}
type Phrase @auth(
query: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
add: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
update: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
delete: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
) {
id: ID!
paragraph: Paragraph!
words: [Word!]! @hasInverse(field: phrase)
start: Float # ideally should be a virtual field, phrase.words[0].start
end: Float # ideally should be a virtual field, phrase.words[words.length - 1].end
}
type Word @auth(
query: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
add: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
update: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
delete: { rule: "{ $ROLE: { eq: \"ADMIN\" } }" }
) {
id: ID!
xid: String! @id
start: Float!
end: Float!
text: String! @search(by: [regexp])
paragraph: Paragraph!
transcript: Transcript!
phrase: Phrase
}
What I did
This is my query:
query paginateParagraphsWithPhrases() {
var(func: type(Transcript)) @filter(eq(Transcript.slug, "hp")) {
paragraphs as Transcript.paragraphs {
Paragraph.text {
wordStart as Word.start
wordEnd as Word.end
Phrase.words (orderasc: Word.start) {
phraseWordStart as Word.start
}
phraseStart as min(val(phraseWordStart))
}
paragraphStart as min(val(wordStart))
paragraphEnd as max(val(wordEnd))
}
}
paginateParagraphsWithPhrases(func: uid(paragraphs), orderasc: val(paragraphStart), first: 100, offset: 0) {
id: uid
transcript: Paragraph.transcript {
id: uid
}
start: val(paragraphStart)
end: val(paragraphEnd)
speaker: Paragraph.speaker {
name: Speaker.name
}
text: Paragraph.text, (orderasc: Word.start) { # want to order this by Word.start, or min(val(phraseWordStart)), i.e. first word in the phrase
dgraph.type
id: uid
phraseStart: val(phraseStart)
start: Word.start
end: Word.end
text: Word.text
xid: Word.xid
words: Phrase.words (orderasc: Word.start) {
xid: Word.xid
start: Word.start
text: Word.text
end: Word.end
}
}
}
}
Dgraph metadata
dgraph version
21.03