Skip to content

Commit

Permalink
Refined Type Interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jun 8, 2022
1 parent 791b664 commit c06f37a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public DefaultTypeInspector(bool ignoreRequiredAttribute = false)
public bool IgnoreRequiredAttribute { get; protected set; }

/// <inheritdoc />
public virtual IEnumerable<MemberInfo> GetMembers(Type type) => GetMembers(type, false);
public virtual IEnumerable<MemberInfo> GetMembers(Type type)
=> GetMembers(type, false);

/// <inheritdoc />
public virtual IEnumerable<MemberInfo> GetMembers(Type type, bool includeIgnored)
Expand All @@ -65,9 +66,16 @@ public virtual bool IsMemberIgnored(MemberInfo member)
return member.IsDefined(typeof(GraphQLIgnoreAttribute));
}

private IEnumerable<MemberInfo> GetMembersInternal(Type type, bool includeIgnored) =>
type.GetMembers(Instance | Public)
.Where(m => CanBeHandled(m, includeIgnored));
private IEnumerable<MemberInfo> GetMembersInternal(Type type, bool includeIgnored)
{
foreach (MemberInfo member in type.GetMembers(Instance | Public))
{
if (CanBeHandled(member, includeIgnored))
{
yield return member;
}
}
}

/// <inheritdoc />
public virtual ITypeReference GetReturnTypeRef(
Expand Down Expand Up @@ -766,23 +774,18 @@ private static bool HasConfiguration(ICustomAttributeProvider element)
element.IsDefined(typeof(DescriptorAttribute), true);

private static bool IsSystemMember(MemberInfo member)
=> IsCloneMember(member) ||
IsToString(member) ||
IsGetHashCode(member) ||
IsEquals(member);

private static bool IsToString(MemberInfo member)
=> member is MethodInfo { Name: _toString };

private static bool IsGetHashCode(MemberInfo member)
=> member is MethodInfo { Name: _getHashCode } m &&
m.GetParameters().Length == 0;

private static bool IsEquals(MemberInfo member)
=> member is MethodInfo { Name: _equals };
{
if (member is MethodInfo m &&
(m.Name.EqualsOrdinal(_toString) ||
m.Name.EqualsOrdinal(_getHashCode) ||
m.Name.EqualsOrdinal(_equals) ||
m.Name.EqualsOrdinal(_clone)))
{
return true;
}

private static bool IsCloneMember(MemberInfo member)
=> member.Name.EqualsOrdinal(_clone);
return false;
}

private IEnumerable<T> GetCustomAttributes<T>(
ICustomAttributeProvider attributeProvider,
Expand Down
27 changes: 27 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/CodeFirstTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
Expand Down Expand Up @@ -138,6 +139,18 @@ public async Task Default_Type_Resolution_Shall_Be_Exact_Schema()
.MatchSnapshotAsync();
}

[Fact]
public async Task Structureal_Equality_Is_Ignored()
{
Snapshot.FullName();

await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryStructEquals>()
.BuildSchemaAsync()
.MatchSnapshotAsync();
}

public class Query
{
public string SayHello(string name) =>
Expand Down Expand Up @@ -257,5 +270,19 @@ public interface IBar
{
string GetBar();
}

public class QueryStructEquals
{
public Example Foo(Example example) => example;
}

public class Example : IStructuralEquatable
{
public string Some { get; set; }

public bool Equals(object? other, IEqualityComparer comparer) => throw new NotImplementedException();

public int GetHashCode(IEqualityComparer comparer) => throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
schema {
query: QueryStructEquals
}

type Example {
some: String!
}

type QueryStructEquals {
foo(example: ExampleInput!): Example!
}

input ExampleInput {
some: String!
}

"The `@defer` directive may be provided for fragment spreads and inline fragments to inform the executor to delay the execution of the current fragment to indicate deprioritization of the current fragment. A query with `@defer` directive will cause the request to potentially return multiple responses, where non-deferred data is delivered in the initial response and data deferred is delivered in a subsequent response. `@include` and `@skip` take precedence over `@defer`."
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD

0 comments on commit c06f37a

Please sign in to comment.