Skip to content

Commit

Permalink
fix(query-graphql): Fixed union types crashing @GraphQLResultInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Oct 30, 2023
1 parent 1c21c59 commit 51cfe27
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
ASTNode,
DirectiveNode,
FieldNode,
getArgumentValues,
getNamedType,
GraphQLField,
Expand All @@ -15,6 +14,10 @@ import type { RelationDescriptor } from './relation.decorator'
import type { QueryResolveFields, QueryResolveTree, SelectRelation } from '@ptc-org/nestjs-query-core'
import type { GraphQLCompositeType, GraphQLResolveInfo as ResolveInfo, SelectionNode } from 'graphql'

/**
* Parts based of https://github.com/graphile/graphile-engine/blob/master/packages/graphql-parse-resolve-info/src/index.ts
*/

function getFieldFromAST<TContext>(
fieldNode: ASTNode,
parentType: GraphQLCompositeType
Expand All @@ -39,7 +42,7 @@ function getDirectiveValue(directive: DirectiveNode, info: ResolveInfo) {
return info.variableValues[arg.value.name.value]
}

function getDirectiveResults(fieldNode: FieldNode, info: ResolveInfo) {
function getDirectiveResults(fieldNode: SelectionNode, info: ResolveInfo) {
const directiveResult = {
shouldInclude: true,
shouldSkip: false
Expand All @@ -63,10 +66,21 @@ function parseFieldNodes<DTO>(
initTree: QueryResolveFields<DTO> | null,
parentType: GraphQLCompositeType
): QueryResolveTree<DTO> | QueryResolveFields<DTO> {
const asts: ReadonlyArray<FieldNode> = Array.isArray(inASTs) ? inASTs : [inASTs]
const asts: ReadonlyArray<SelectionNode> = Array.isArray(inASTs) ? inASTs : [inASTs]

return asts.reduce((tree, fieldNode) => {
const alias: string = fieldNode?.alias?.value ?? fieldNode.name.value
let name: string
let alias: string

if (fieldNode.kind === Kind.INLINE_FRAGMENT) {
name = fieldNode?.typeCondition?.name.value
} else {
name = fieldNode.name.value
}

if (fieldNode.kind === Kind.FIELD) {
alias = fieldNode?.alias?.value ?? name
}

const field = getFieldFromAST(fieldNode, parentType)
if (field == null) {
Expand All @@ -87,12 +101,12 @@ function parseFieldNodes<DTO>(
}

const parsedField = {
name: fieldNode.name.value,
name,
alias,
args: getArgumentValues(field, fieldNode, resolveInfo.variableValues),
args: fieldNode.kind === Kind.FIELD ? getArgumentValues(field, fieldNode, resolveInfo.variableValues) : {},

fields:
fieldNode.selectionSet && isCompositeType(fieldGqlTypeOrUndefined)
fieldNode.kind !== Kind.FRAGMENT_SPREAD && fieldNode.selectionSet && isCompositeType(fieldGqlTypeOrUndefined)
? parseFieldNodes(
fieldNode.selectionSet.selections,
resolveInfo,
Expand Down

0 comments on commit 51cfe27

Please sign in to comment.