public void Register_SchemaType_ClrTypeExists() { // arrange var typeInterceptor = new AggregateTypeInterceptor(); typeInterceptor.SetInterceptors(new[] { new IntrospectionTypeInterceptor() }); IDescriptorContext context = DescriptorContext.Create( typeInterceptor: typeInterceptor); var typeRegistry = new TypeRegistry(context.TypeInterceptor); var typeInitializer = new TypeInitializer( context, typeRegistry, new List <ITypeReference> { context.TypeInspector.GetTypeRef(typeof(FooType), TypeContext.Output) }, null, t => t is FooType ? RootTypeKind.Query : RootTypeKind.None); // act typeInitializer.Initialize(() => null, new SchemaOptions()); // assert var exists = typeRegistry.TryGetType( context.TypeInspector.GetTypeRef(typeof(FooType), TypeContext.Output), out RegisteredType type); Assert.True(exists); var fooType = Assert.IsType <FooType>(type.Type).Fields.ToDictionary( t => t.Name.ToString(), t => t.Type.Print()); exists = typeRegistry.TryGetType( context.TypeInspector.GetTypeRef(typeof(BarType), TypeContext.Output), out type); Assert.True(exists); var barType = Assert.IsType <BarType>(type.Type).Fields.ToDictionary( t => t.Name.ToString(), t => t.Type.Print()); new { fooType, barType }.MatchSnapshot(); }
public void Register_ClrType_InferSchemaTypes() { // arrange IDescriptorContext context = DescriptorContext.Create( typeInterceptor: new AggregateTypeInterceptor(new IntrospectionTypeInterceptor())); var typeRegistry = new TypeRegistry(); var typeInitializer = new TypeInitializer( context, typeRegistry, new List <ITypeReference> { context.TypeInspector.GetTypeRef(typeof(Foo), TypeContext.Output) }, new List <Type>(), null, t => t is ObjectType <Foo>, t => t is FooType); // act typeInitializer.Initialize(() => null, new SchemaOptions()); // assert var exists = typeRegistry.TryGetType( context.TypeInspector.GetTypeRef(typeof(ObjectType <Foo>), TypeContext.Output), out RegisteredType type); Assert.True(exists); var fooType = Assert.IsType <ObjectType <Foo> >(type.Type).Fields.ToDictionary( t => t.Name.ToString(), t => t.Type.Print()); exists = typeRegistry.TryGetType( context.TypeInspector.GetTypeRef(typeof(ObjectType <Bar>), TypeContext.Output), out type); Assert.True(exists); var barType = Assert.IsType <ObjectType <Bar> >(type.Type).Fields.ToDictionary( t => t.Name.ToString(), t => t.Type.Print()); new { fooType, barType }.MatchSnapshot(); }
public bool TryPredictTypeKind(ITypeReference typeRef, out TypeKind kind) { if (_typeLookup.TryNormalizeReference(typeRef, out ITypeReference namedTypeRef) && _typeRegistry.TryGetType(namedTypeRef, out RegisteredType registeredType)) { switch (registeredType.Type) { case INamedType namedType: kind = namedType.Kind; return(true); case DirectiveType: kind = TypeKind.Directive; return(true); default: kind = default; return(false); } } namedTypeRef ??= typeRef; switch (namedTypeRef) { case ExtendedTypeReference r: if (Scalars.TryGetScalar(r.Type.Type, out _)) { kind = TypeKind.Scalar; return(true); } if (r.Type.IsSchemaType) { kind = GetTypeKindFromSchemaType(r.Type); return(true); } return(SchemaTypeResolver.TryInferSchemaTypeKind(r, out kind)); case SchemaTypeReference r: kind = GetTypeKindFromSchemaType(TypeInspector.GetType(r.Type.GetType())); return(true); default: kind = default; return(false); } }