public void Register_SchemaType_ClrTypeExists()
        {
            // arrange
            var initialTypes = new List <ITypeReference>();

            initialTypes.Add(new ClrTypeReference(
                                 typeof(FooType),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var typeInitializer = new TypeInitializer(
                serviceProvider,
                DescriptorContext.Create(),
                initialTypes,
                new List <Type>(),
                new Dictionary <string, object>(),
                null,
                t => t is FooType);

            // act
            typeInitializer.Initialize(() => null);

            // assert
            bool exists = typeInitializer.Types.TryGetValue(
                new ClrTypeReference(typeof(FooType), TypeContext.Output),
                out RegisteredType type);

            Assert.True(exists);
            Assert.IsType <FooType>(type.Type).Fields.ToDictionary(
                t => t.Name.ToString(),
                t => TypeVisualizer.Visualize(t.Type))
            .MatchSnapshot(new SnapshotNameExtension("FooType"));

            exists = typeInitializer.Types.TryGetValue(
                new ClrTypeReference(typeof(BarType), TypeContext.Output),
                out type);

            Assert.True(exists);
            Assert.IsType <BarType>(type.Type).Fields.ToDictionary(
                t => t.Name.ToString(),
                t => TypeVisualizer.Visualize(t.Type))
            .MatchSnapshot(new SnapshotNameExtension("BarType"));
        }
Example #2
0
        public void FilterProvider_Throws_Exception_When_NotInitializedByConvention()
        {
            // arrange
            var provider = new QueryableFilterProvider(
                descriptor => descriptor.AddFieldHandler <QueryableStringEqualsHandler>());
            var context = ConventionContext.Create(
                null,
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            // act
            provider.Initialize(context);

            // assert
            SchemaException exception =
                Assert.Throws <SchemaException>(() => provider.OnComplete(context));

            exception.Message.MatchSnapshot();
        }
Example #3
0
        public void Merge_Should_Merge_ArgumentName()
        {
            // arrange
            var convention = new MockSortConvention(x => x.ArgumentName("Foo"));
            var extension  = new SortConventionExtension(x => x.ArgumentName("Bar"));
            var context    = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.Equal("Bar", convention.DefinitionAccessor?.ArgumentName);
        }
Example #4
0
        public void Merge_Should_Merge_Provider()
        {
            // arrange
            var convention = new MockSortConvention(x => x.Provider <QueryableSortProvider>());
            var extension  = new SortConventionExtension(x => x.Provider <MockProvider>());
            var context    = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.Equal(typeof(MockProvider), convention.DefinitionAccessor?.Provider);
        }
        public Schema Create()
        {
            IServiceProvider services = _services
                                        ?? new EmptyServiceProvider();

            var descriptorContext = DescriptorContext.Create(_options, services);

            IBindingLookup bindingLookup =
                _bindingCompiler.Compile(descriptorContext);

            IReadOnlyCollection <ITypeReference> types =
                GetTypeReferences(services, bindingLookup);

            var lazy = new LazySchema();

            TypeInitializer initializer =
                InitializeTypes(
                    services,
                    descriptorContext,
                    bindingLookup,
                    types,
                    () => lazy.Schema);

            SchemaTypesDefinition definition =
                CreateSchemaDefinition(initializer);

            if (definition.QueryType == null && _options.StrictValidation)
            {
                throw new SchemaException(
                          SchemaErrorBuilder.New()
                          .SetMessage(TypeResources.SchemaBuilder_NoQueryType)
                          .Build());
            }

            Schema schema = initializer.Types.Values
                            .Select(t => t.Type)
                            .OfType <Schema>()
                            .First();

            schema.CompleteSchema(definition);
            lazy.Schema = schema;
            return(schema);
        }
        public void Upgrade_Type_From_GenericType()
        {
            // arrange
            var initialTypes = new HashSet <ITypeReference>();

            initialTypes.Add(new ClrTypeReference(
                                 typeof(ObjectType <Foo>),
                                 TypeContext.Output));
            initialTypes.Add(new ClrTypeReference(
                                 typeof(FooType),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var clrTypeReferences = new Dictionary <IClrTypeReference, ITypeReference>();

            var typeDiscoverer = new TypeDiscoverer(
                initialTypes,
                clrTypeReferences,
                DescriptorContext.Create(),
                new Dictionary <string, object>(),
                new AggregateTypeInitializationInterceptor(),
                serviceProvider);

            // act
            DiscoveredTypes result = typeDiscoverer.DiscoverTypes();

            // assert
            Assert.Empty(result.Errors);

            result.Types
            .Select(t => t.Type)
            .OfType <IHasClrType>()
            .ToDictionary(
                t => t.GetType().GetTypeName(),
                t => t.ClrType.GetTypeName())
            .MatchSnapshot(new SnapshotNameExtension("registered"));

            clrTypeReferences.ToDictionary(
                t => t.Key.ToString(),
                t => t.Value.ToString())
            .MatchSnapshot(new SnapshotNameExtension("clr"));
        }
Example #7
0
        private void RegisterExternalResolvers()
        {
            if (_externalResolverTypes.Count == 0)
            {
                return;
            }

            IDescriptorContext descriptorContext =
                DescriptorContext.Create(_services);

            Dictionary <NameString, ObjectType> types =
                _types.Select(t => t.Value.Type)
                .OfType <ObjectType>()
                .ToDictionary(t => t.Name);

            foreach (Type type in _externalResolverTypes)
            {
                GraphQLResolverOfAttribute attribute =
                    type.GetCustomAttribute <GraphQLResolverOfAttribute>();

                if (attribute.TypeNames != null)
                {
                    foreach (string typeName in attribute.TypeNames)
                    {
                        if (types.TryGetValue(typeName,
                                              out ObjectType objectType))
                        {
                        }
                    }
                }

                if (attribute.Types != null)
                {
                    foreach (Type sourceType in attribute.Types
                             .Where(t => !BaseTypes.IsNonGenericBaseType(t)))
                    {
                        ObjectType objectType = types.Values
                                                .FirstOrDefault(t => t.GetType() == sourceType);
                    }
                }
            }
        }
Example #8
0
            public static DescriptorContext CreateContext(
                SchemaBuilder builder,
                LazySchema lazySchema)
            {
                IServiceProvider services = builder._services ?? new EmptyServiceProvider();

                var schemaInterceptor = new AggregateSchemaInterceptor();
                var typeInterceptor   = new AggregateTypeInterceptor();

                DescriptorContext context = DescriptorContext.Create(
                    builder._options,
                    services,
                    builder._conventions,
                    builder._contextData,
                    lazySchema,
                    schemaInterceptor,
                    typeInterceptor);

                return(context);
            }
Example #9
0
            private static SchemaTypesDefinition CreateSchemaDefinition(
                SchemaBuilder builder,
                DescriptorContext context,
                TypeRegistry typeRegistry)
            {
                var definition = new SchemaTypesDefinition();

                RegisterOperationName(
                    builder,
                    OperationType.Query,
                    builder._options.QueryTypeName);
                RegisterOperationName(
                    builder,
                    OperationType.Mutation,
                    builder._options.MutationTypeName);
                RegisterOperationName(
                    builder,
                    OperationType.Subscription,
                    builder._options.SubscriptionTypeName);

                Dictionary <OperationType, ITypeReference> operations =
                    builder._operations.ToDictionary(
                        t => t.Key,
                        t => t.Value(context.TypeInspector));

                definition.QueryType = ResolveOperation(
                    OperationType.Query, operations, typeRegistry);
                definition.MutationType = ResolveOperation(
                    OperationType.Mutation, operations, typeRegistry);
                definition.SubscriptionType = ResolveOperation(
                    OperationType.Subscription, operations, typeRegistry);

                IReadOnlyCollection <TypeSystemObjectBase> types =
                    RemoveUnreachableTypes(builder, typeRegistry, definition);

                definition.Types          = types.OfType <INamedType>().Distinct().ToArray();
                definition.DirectiveTypes = types.OfType <DirectiveType>().Distinct().ToArray();

                return(definition);
            }
Example #10
0
        public void Merge_Should_Merge_EnumConfigurations()
        {
            // arrange
            var convention = new MockSortConvention(
                x => x.ConfigureEnum <DefaultSortEnumType>(d => d.Name("Foo")));
            var extension = new SortConventionExtension(
                x => x.ConfigureEnum <MockSortEnumType>(d => d.Name("Foo")));
            var context = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.NotNull(convention.DefinitionAccessor);
            Assert.Equal(2, convention.DefinitionAccessor !.EnumConfigurations.Count);
        }
Example #11
0
        public void Register_ClrType_InferSchemaTypes()
        {
            // arrange
            var initialTypes = new HashSet <ITypeReference>();

            initialTypes.Add(TypeReference.Create(
                                 typeof(Foo),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var clrTypeReferences = new Dictionary <ClrTypeReference, ITypeReference>();

            var typeDiscoverer = new TypeDiscoverer(
                initialTypes,
                clrTypeReferences,
                DescriptorContext.Create(),
                new AggregateTypeInitializationInterceptor(),
                serviceProvider);

            // act
            DiscoveredTypes result = typeDiscoverer.DiscoverTypes();

            // assert
            Assert.Empty(result.Errors);

            new
            {
                registered = result.Types
                             .Select(t => t.Type)
                             .OfType <IHasRuntimeType>()
                             .ToDictionary(
                    t => t.GetType().GetTypeName(),
                    t => t.RuntimeType.GetTypeName()),
                clr = clrTypeReferences.ToDictionary(
                    t => t.Key.ToString(),
                    t => t.Value.ToString())
            }.MatchSnapshot();
        }
Example #12
0
    public void Merge_Should_Merge_ProviderInstance()
    {
        // arrange
        var providerInstance = new MockProvider();
        var convention       = new MockProjectionConvention(
            x => x.Provider(new QueryableProjectionProvider()));
        var extension = new ProjectionConventionExtension(
            x => x.Provider(providerInstance));
        var context = new ConventionContext(
            "Scope",
            new ServiceCollection().BuildServiceProvider(),
            DescriptorContext.Create());

        convention.Initialize(context);
        extension.Initialize(context);

        // act
        extension.Merge(context, convention);

        // assert
        Assert.Equal(providerInstance, convention.DefinitionAccessor?.ProviderInstance);
    }
        public void Merge_Should_Merge_Configurations()
        {
            // arrange
            var convention = new MockFilterConvention(
                x => x.Configure <ComparableOperationFilterInput <int> >(d => d.Name("Foo")));
            var extension = new FilterConventionExtension(
                x => x.Configure <ComparableOperationFilterInput <double> >(d => d.Name("Bar")));
            var context = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.NotNull(convention.DefinitionAccessor);
            Assert.Equal(2, convention.DefinitionAccessor !.Configurations.Count);
        }
        protected override void DrawRowValueCellCore(CustomDrawRowValueCellEventArgs e, BaseEditPainter pb, BaseEditViewInfo bvi, BaseViewInfo vi)
        {
            MyPropertyGridControl grid       = (MyPropertyGridControl)vi.Grid;
            RowProperties         properties = e.Row.GetRowProperties(e.CellIndex);
            DescriptorContext     context    = grid.GetDescriptorContext(properties);
            UITypeEditor          editor     = grid.GetUITypeEditor(context);

            if (editor != null && editor.GetPaintValueSupported())
            {
                int                 indent = 1;
                Size                size   = new Size(e.Bounds.Size.Height - 2 * indent, e.Bounds.Size.Height - 2 * indent);
                Rectangle           bounds = new Rectangle(e.Bounds.X + indent, e.Bounds.Y + indent, size.Width, size.Height);
                PaintValueEventArgs args   = new PaintValueEventArgs(context, grid.GetCellValue(properties.Row, 0), e.Cache.Graphics, bounds);
                editor.PaintValue(args);
                e.Cache.DrawRectangle(Pens.DarkGray, bounds);
                Rectangle textBounds = new Rectangle(e.Bounds.X + bounds.Width + 2 * indent + indent, e.Bounds.Y, e.Bounds.Width - bounds.Width + 2 * indent, e.Bounds.Height);
                grid.Appearance.RecordValue.DrawString(e.Cache, grid.GetCellDisplayText(properties.Row, 0), textBounds);
            }
            else
            {
                base.DrawRowValueCellCore(e, pb, bvi, vi);
            }
        }
Example #15
0
    public void Merge_Should_Merge_ProviderExtensionsTypes()
    {
        // arrange
        var convention =
            new MockProjectionConvention(x => x.AddProviderExtension <MockProviderExtensions>());
        var extension =
            new ProjectionConventionExtension(
                x => x.AddProviderExtension <MockProviderExtensions>());
        var context = new ConventionContext(
            "Scope",
            new ServiceCollection().BuildServiceProvider(),
            DescriptorContext.Create());

        convention.Initialize(context);
        extension.Initialize(context);

        // act
        extension.Merge(context, convention);

        // assert
        Assert.NotNull(convention.DefinitionAccessor);
        Assert.Equal(2, convention.DefinitionAccessor !.ProviderExtensionsTypes.Count);
    }
Example #16
0
        public void Merge_Should_Merge_Bindings()
        {
            // arrange
            var convention = new MockSortConvention(
                x => x.BindRuntimeType <int, DefaultSortEnumType>());
            var extension = new SortConventionExtension(
                x => x.BindRuntimeType <double, DefaultSortEnumType>());
            var context = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.NotNull(convention.DefinitionAccessor);
            Assert.Contains(typeof(int), convention.DefinitionAccessor !.Bindings);
            Assert.Contains(typeof(double), convention.DefinitionAccessor !.Bindings);
        }
Example #17
0
        public void Merge_Should_Merge_Operations()
        {
            // arrange
            var convention = new MockSortConvention(x => x.Operation(1));
            var extension  = new SortConventionExtension(x => x.Operation(2));
            var context    = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.NotNull(convention.DefinitionAccessor);
            Assert.Collection(
                convention.DefinitionAccessor !.Operations,
                x => Assert.Equal(1, x.Id),
                x => Assert.Equal(2, x.Id));
        }
Example #18
0
        public void Merge_Should_Merge_DefaultBinding()
        {
            // arrange
            var convention = new MockSortConvention(
                x => x.DefaultBinding <DefaultSortEnumType>());
            var extension = new SortConventionExtension(
                x => x.DefaultBinding <MockSortEnumType>());
            var context = new ConventionContext(
                "Scope",
                new ServiceCollection().BuildServiceProvider(),
                DescriptorContext.Create());

            convention.Initialize(context);
            extension.Initialize(context);

            // act
            extension.Merge(context, convention);

            // assert
            Assert.Equal(
                typeof(MockSortEnumType),
                convention.DefinitionAccessor?.DefaultBinding);
        }
            private static Schema CompleteSchema(
                SchemaBuilder builder,
                DescriptorContext context,
                LazySchema lazySchema,
                TypeRegistry typeRegistry)
            {
                SchemaTypesDefinition definition =
                    CreateSchemaDefinition(builder, context, typeRegistry);

                if (definition.QueryType is null && builder._options.StrictValidation)
                {
                    throw new SchemaException(
                              SchemaErrorBuilder.New()
                              .SetMessage(TypeResources.SchemaBuilder_NoQueryType)
                              .Build());
                }

                Schema schema = typeRegistry.Types
                                .Select(t => t.Type).OfType <Schema>().First();

                schema.CompleteSchema(definition);
                lazySchema.Schema = schema;
                return(schema);
            }
Example #20
0
            public static Schema Create(SchemaBuilder builder)
            {
                var lazySchema            = new LazySchema();
                DescriptorContext context = CreateContext(builder, lazySchema);

                try
                {
                    IBindingLookup bindingLookup = builder._bindingCompiler.Compile(context);

                    IReadOnlyList <ITypeReference> typeReferences =
                        CreateTypeReferences(builder, context, bindingLookup);

                    TypeRegistry typeRegistry =
                        InitializeTypes(builder, context, bindingLookup, typeReferences,
                                        lazySchema);

                    return(CompleteSchema(builder, context, lazySchema, typeRegistry));
                }
                catch (Exception ex)
                {
                    context.SchemaInterceptor.OnError(context, ex);
                    throw;
                }
            }
Example #21
0
        public void Register_ClrType_InferSchemaTypes()
        {
            // arrange
            IDescriptorContext context = DescriptorContext.Create(
                typeInterceptor: new AggregateTypeInterceptor(new IntrospectionTypeInterceptor()));
            var typeRegistry = new TypeRegistry(context.TypeInterceptor);

            var typeInitializer = new TypeInitializer(
                context,
                typeRegistry,
                new List <ITypeReference>
            {
                context.TypeInspector.GetTypeRef(typeof(Foo), TypeContext.Output)
            },
                new List <Type>(),
                null,
                t =>
            {
                return(t switch
                {
                    ObjectType <Foo> => RootTypeKind.Query,
                    _ => RootTypeKind.None
                });
            });
        public void Initializer_SchemaOptions_Are_Null()
        {
            // 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>);

            // act
            void Action() => typeInitializer.Initialize(() => null, null !);

            // assert
            Assert.Throws <ArgumentNullException>(Action);
        }
Example #23
0
        public void Register_ClrType_InferSchemaTypes()
        {
            // arrange
            var initialTypes = new List <ITypeReference>();

            initialTypes.Add(new ClrTypeReference(
                                 typeof(Foo),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var typeRegistrar = new TypeRegistrar(
                serviceProvider,
                DescriptorContext.Create(),
                initialTypes,
                new Dictionary <ITypeReference, ITypeReference>(),
                new Dictionary <string, object>(),
                new AggregateTypeInitilizationInterceptor());

            // act
            typeRegistrar.Complete();

            // assert
            typeRegistrar.Registerd
            .Select(t => t.Value.Type)
            .OfType <IHasClrType>()
            .ToDictionary(
                t => t.GetType().GetTypeName(),
                t => t.ClrType.GetTypeName())
            .MatchSnapshot(new SnapshotNameExtension("registered"));

            typeRegistrar.ClrTypes.ToDictionary(
                t => t.Key.ToString(),
                t => t.Value.ToString())
            .MatchSnapshot(new SnapshotNameExtension("clr"));
        }
 public UITypeEditor GetUITypeEditor(DescriptorContext context)
 {
     return(context.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) as UITypeEditor);
 }
Example #25
0
 public ProductImageRepository(DescriptorContext context)
 {
     _context = context;
 }
 public ReviewerRepository(DescriptorContext context)
 {
     _context = context;
 }
Example #27
0
 public ItemRepository(DescriptorContext context)
 {
     _context = context;
 }
 public SortingFieldCollectionExtensionTest()
 {
     _property          = x => x.Bar;
     _propertyInfo      = (PropertyInfo)_property.ExtractMember();
     _descriptorContext = DescriptorContext.Create();
 }
Example #29
0
 public SellerRepository(DescriptorContext context)
 {
     _context = context;
 }