From 01de3af805edd02c5e91f8848c64b326a29693f3 Mon Sep 17 00:00:00 2001 From: Chavdar Angelov Date: Wed, 10 Jun 2020 21:47:08 +0300 Subject: [PATCH] Fix for "Unexpected type" error (#197 #205) * Revert Make GraphTypeInfo.TypeParameter lazy (#8) * Revert Make GraphTypeInfo.TypeParameter lazy * Version bumps * Fix version bump for GraphQL * "Unexpected type: " error fix hacky fix for "Unexpected type: " error when return type of resolver method is INode * Restore 'GraphTypeInfo' * Added unique version suffix we needed a way to have unique name for out own nuget feed * commetns resolved * removed version suffix * set a proper csproj file version Co-authored-by: K-Pavlov Co-authored-by: Rob Wijkstra Co-authored-by: Rob Wijkstra --- .../Adapters/GraphTypeAdapter.cs | 15 +++-- src/GraphQL.Conventions/CommonAssemblyInfo.cs | 4 +- .../GraphQL.Conventions.csproj | 2 +- .../Adapters/BugUnexpectedTypeTests.cs | 56 +++++++++++++++++++ 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 test/GraphQL.Conventions.Tests/Adapters/BugUnexpectedTypeTests.cs diff --git a/src/GraphQL.Conventions/Adapters/GraphTypeAdapter.cs b/src/GraphQL.Conventions/Adapters/GraphTypeAdapter.cs index 097da151..2118ed36 100644 --- a/src/GraphQL.Conventions/Adapters/GraphTypeAdapter.cs +++ b/src/GraphQL.Conventions/Adapters/GraphTypeAdapter.cs @@ -33,18 +33,21 @@ public ISchema DeriveSchema(GraphSchemaInfo schemaInfo) var possibleTypes = interfaces .Where(t => !t.IsIgnored) .SelectMany(t => t.PossibleTypes) + .Select(typeInfo => (typeInfo.IsIgnored || !typeInfo.IsNullable || typeInfo.Interfaces.Any(x => x.IsIgnored == true)) + ? null + : DeriveType(typeInfo) + ) + .Where(x => x != null) .GroupBy(t => t.Name) - .Select(g => g.First()); + .Select(g => g.First()) + .ToArray(); var schema = new Schema(new FuncDependencyResolver(DeriveTypeFromTypeInfo)) { Query = DeriveOperationType(schemaInfo.Query), Mutation = DeriveOperationType(schemaInfo.Mutation), Subscription = DeriveOperationType(schemaInfo.Subscription), - }; - schema.RegisterTypes(possibleTypes - .Where(t => !t.IsIgnored && !t.Interfaces.Any(i => i.IsIgnored)) - .Select(t => DeriveType(t).GetType()) - .ToArray()); + }; + schema.RegisterTypes(possibleTypes); return schema; } diff --git a/src/GraphQL.Conventions/CommonAssemblyInfo.cs b/src/GraphQL.Conventions/CommonAssemblyInfo.cs index d69289ec..89938498 100644 --- a/src/GraphQL.Conventions/CommonAssemblyInfo.cs +++ b/src/GraphQL.Conventions/CommonAssemblyInfo.cs @@ -8,8 +8,8 @@ [assembly: AssemblyCopyright("Copyright 2016-2019 Tommy Lillehagen. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("2.5.3")] -[assembly: AssemblyInformationalVersion("2.5.3")] +[assembly: AssemblyFileVersion("2.5.4")] +[assembly: AssemblyInformationalVersion("2.5.4")] [assembly: CLSCompliant(false)] [assembly: InternalsVisibleTo("Tests")] diff --git a/src/GraphQL.Conventions/GraphQL.Conventions.csproj b/src/GraphQL.Conventions/GraphQL.Conventions.csproj index 8a4dacc4..bd87c8cb 100644 --- a/src/GraphQL.Conventions/GraphQL.Conventions.csproj +++ b/src/GraphQL.Conventions/GraphQL.Conventions.csproj @@ -2,7 +2,7 @@ GraphQL Conventions for .NET - 2.5.3 + 2.5.4 Tommy Lillehagen net45;netstandard1.6.1 old diff --git a/test/GraphQL.Conventions.Tests/Adapters/BugUnexpectedTypeTests.cs b/test/GraphQL.Conventions.Tests/Adapters/BugUnexpectedTypeTests.cs new file mode 100644 index 00000000..fca27212 --- /dev/null +++ b/test/GraphQL.Conventions.Tests/Adapters/BugUnexpectedTypeTests.cs @@ -0,0 +1,56 @@ +using GraphQL.Conventions; +using GraphQL.Conventions.Relay; +using GraphQL.Conventions.Tests; +using GraphQL.Conventions.Tests.Templates; +using GraphQL.Conventions.Tests.Templates.Extensions; +using System.Threading.Tasks; + +namespace Tests.Adapters.Engine.Bugs +{ + public class BugUnexpectedTypeTests : TestBase + { + [Test] + public async Task Can_Resolve_NonNull_Null_Query() + => await Can_Resolve_Query_Private(); + + [Test] + public async Task Can_Resolve_Null_NonNull_Query() + => await Can_Resolve_Query_Private(); + + private async Task Can_Resolve_Query_Private() + { + var engine = GraphQLEngine + .New(); + var result = await engine + .NewExecutor() + .WithQueryString("query { node { ... on ParentNode { id, nested { id } } } }") + .Execute(); + result.ShouldHaveNoErrors(); + result.Data.ShouldHaveFieldWithValue("node", "id", "UGFyZW50Tm9kZTox"); + result.Data.ShouldHaveFieldWithValue("node", "nested", "id", "Q2hpbGROb2RlOjE="); + } + + public class NonNull_Null_Query + { + public INode Node() => new ParentNode(); + public NonNull A() => null; + public ParentNode B() => null; + } + + public class Null_NonNull_Query + { + public INode Node() => new ParentNode(); + public ParentNode A() => null; + public NonNull B() => null; + } + public class ChildNode : INode + { + public Id Id => Id.New(1); + } + public class ParentNode : INode + { + public Id Id => Id.New(1); + public ChildNode Nested() => new ChildNode(); + } + } +}