Skip to content

Commit

Permalink
fix: add fragment type name to query lookup and builder
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstnmcf committed Sep 26, 2024
1 parent 15f53e6 commit 6c56298
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function buildQuery (query, parentResult) {
}

// get keys to reuse on merge results
function buildQuerySelection (selection, parent, wrap = true) {
function buildQuerySelection (selection, parent, wrap = true, fragment = undefined) {
if (!(selection && selection.length > 0)) {
return { selection: '', keys: [] }
}
Expand All @@ -132,7 +132,7 @@ function buildQuerySelection (selection, parent, wrap = true) {
fields.add(toQuerySelection(keyField))
keys.set('key' + keyId(k), k)
} else if (selection[i].selection) {
fields.add(buildQuerySelection(selection[i].selection, null, selection[i].wrap).selection)
fields.add(buildQuerySelection(selection[i].selection, null, selection[i].wrap, selection[i].fragment).selection)
} else if (selection[i].nested) {
fields.add(selection[i].parentField)
for (const nested of selection[i].nested.values()) {
Expand Down Expand Up @@ -182,7 +182,7 @@ function buildQuerySelection (selection, parent, wrap = true) {
}
}

const qselection = wrap ? `{ ${Array.from(fields).join(' ')} }` : Array.from(fields).join(' ')
const qselection = wrap ? `${fragment ? `... on ${fragment} ` : ''}{ ${Array.from(fields).join(' ')} }` : Array.from(fields).join(' ')
return { selection: qselection, keys: Array.from(keys.values()) }
}

Expand Down
21 changes: 16 additions & 5 deletions lib/query-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const { mergeMaps, collectNodeArgs, collectPlainArgs, pathJoin } = require('./ut
*/
function collectQueries ({
subgraphName, queryFieldNode, path = '', fieldId, parent, args,
fragmentFieldTypeName,
// references
types, fields, aliases,
// root: collect root queries
Expand Down Expand Up @@ -85,6 +86,7 @@ function collectQueries ({
field,
fieldId,
queryFieldNode,

// TODO maybe parent and root are redundant
parent,
root,
Expand All @@ -104,8 +106,9 @@ function collectQueries ({
return { queries, deferreds, order }
}

const fieldTypeName = field?.typeName
const fieldTypeName = fragmentFieldTypeName ?? field?.typeName
// const fieldType = field && types[fieldTypeName]
const isUnionTypeType = field.type.src.kind === 'UNION'

for (let i = 0; i < queryFieldSelections.length; ++i) {
const querySelection = queryFieldSelections[i]
Expand All @@ -115,6 +118,7 @@ function collectQueries ({
? context.info.fragments[querySelection.name.value]
: querySelection

const fragmentFieldTypeName = isUnionTypeType ? fragment.typeCondition.name.value : undefined
// TODO if !field - shouldn't happen
const nested = collectNestedQueries({
context,
Expand All @@ -124,12 +128,17 @@ function collectQueries ({
queryNode,
querySelection: fragment,
types,
fragmentFieldTypeName,
fields
})

// unwrap fragment as selection
for (const n of nested.queries.values()) {
queryNode.query.selection.push({ selection: n.query.selection, wrap: false })
queryNode.query.selection.push({
selection: n.query.selection,
wrap: fragmentFieldTypeName !== undefined,
fragment: fragmentFieldTypeName
})
}
collectDeferredQueries(queryNode, nested, deferreds)

Expand Down Expand Up @@ -161,7 +170,7 @@ function collectQueries ({

context.logger.debug(`deferred query for ${cpath} > ${selectionFieldName} on ${subgraphName}`)

const alias = aliases[fieldId]
const alias = aliases && aliases[fieldId]
if (alias && nested) {
nesting({
selectionFieldName,
Expand Down Expand Up @@ -258,7 +267,8 @@ function collectNestedQueries ({
types,
fields,
aliases,
alias
alias,
fragmentFieldTypeName
}) {
context.logger.debug({ fieldId, path }, 'query lookup, nested')
const a = alias ? Object.values(alias)[0] : null
Expand All @@ -274,7 +284,8 @@ function collectNestedQueries ({
types,
fields,
aliases,
typeName: a?.type
typeName: a?.type,
fragmentFieldTypeName
})
}

Expand Down

0 comments on commit 6c56298

Please sign in to comment.