From 2212a0a7b971d31d094e3a052bd3eaa30331587f Mon Sep 17 00:00:00 2001
From: Simon Meyffret <meyffret@amazon.com>
Date: Tue, 19 Apr 2022 10:53:56 -0400
Subject: [PATCH] Handle recursive union types

checkAmbiguousMemberTypes might trigger calls to createOrReuseUnion when
fetching the fields of a given type. However, if graphQLType is not
defined yet, it will try to create it instead of returning the
previously created one. In order to reuse graphQLType without creating a
new one, we need to call checkAmbiguousMemberTypes **after** assigning
the graphQLType to the current definition.

Signed-off-by: Simon Meyffret <smeyffret@gmail.com>
---
 packages/openapi-to-graphql/src/schema_builder.ts | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/packages/openapi-to-graphql/src/schema_builder.ts b/packages/openapi-to-graphql/src/schema_builder.ts
index 0475f456..ff32df92 100644
--- a/packages/openapi-to-graphql/src/schema_builder.ts
+++ b/packages/openapi-to-graphql/src/schema_builder.ts
@@ -396,13 +396,6 @@ function createOrReuseUnion<TSource, TContext, TArgs>({
       }
     )
 
-    /**
-     * Check for ambiguous member types
-     *
-     * i.e. member types that can be confused with each other.
-     */
-    checkAmbiguousMemberTypes(def, types, data)
-
     def.graphQLType = new GraphQLUnionType({
       name: def.graphQLTypeName,
       description,
@@ -436,6 +429,13 @@ function createOrReuseUnion<TSource, TContext, TArgs>({
       }
     })
 
+    /**
+     * Check for ambiguous member types
+     *
+     * i.e. member types that can be confused with each other.
+     */
+    checkAmbiguousMemberTypes(def, types, data)
+
     return def.graphQLType
   }
 }