/// <summary> /// Adds a subscription server to this instance that will accept connected clients and /// process subscription requests from those clients. This extension will attempt to inject subscription related /// middleware into the primary query excution pipeline and replace it. Call this method before injecting or /// adding your own query execution middleware items. /// </summary> /// <typeparam name="TSchema">The type of the schema being built.</typeparam> /// <param name="schemaBuilder">The schema builder.</param> /// <param name="options">An action function to configure the subscription options.</param> /// <returns>ISchemaBuilder<TSchema>.</returns> public static ISchemaBuilder <TSchema> AddSubscriptionServer <TSchema>( this ISchemaBuilder <TSchema> schemaBuilder, Action <SubscriptionServerOptions <TSchema> > options = null) where TSchema : class, ISchema { var subscriptionsOptions = new SubscriptionServerOptions <TSchema>(); options?.Invoke(subscriptionsOptions); var extension = new ApolloSubscriptionServerSchemaExtension <TSchema>(schemaBuilder, subscriptionsOptions); // register the default router type to the service collection var defaultRouter = CreateDefaultSubscriptionRouterDescriptor(); extension.OptionalServices.Add(defaultRouter); schemaBuilder.Options.RegisterExtension(extension); return(schemaBuilder); }
public void GeneralPropertyCheck() { using var restorePoint = new GraphQLProviderRestorePoint(); GraphQLProviders.TemplateProvider = null; var primaryOptions = new SchemaOptions <GraphSchema>(); var subscriptionOptions = new SubscriptionServerOptions <GraphSchema>(); (var builder, var queryPipeline, var fieldPipeline) = CreateSchemaBuilderMock(); var extension = new ApolloSubscriptionServerSchemaExtension <GraphSchema>(builder.Object, subscriptionOptions); extension.Configure(primaryOptions); Assert.IsTrue(primaryOptions.DeclarationOptions.AllowedOperations.Contains(GraphCollection.Subscription)); Assert.AreEqual(2, extension.RequiredServices.Count); Assert.IsNotNull(extension.RequiredServices.SingleOrDefault(x => x.ServiceType == typeof(SubscriptionServerOptions <GraphSchema>))); Assert.IsNotNull(extension.RequiredServices.SingleOrDefault(x => x.ServiceType == typeof(ApolloMessageConverterFactory))); Assert.AreEqual(1, extension.OptionalServices.Count); Assert.IsNotNull(extension.OptionalServices.SingleOrDefault(x => x.ServiceType == typeof(ISubscriptionServer <GraphSchema>))); Assert.IsTrue(GraphQLProviders.TemplateProvider is SubscriptionEnabledTemplateProvider); // 9 middleware components in the subscription-swapped primary query pipeline registered by type // 1 middleware component registered by instance queryPipeline.Verify(x => x.Clear()); queryPipeline.Verify( x => x.AddMiddleware <IGraphMiddlewareComponent <GraphQueryExecutionContext> >( It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(9)); queryPipeline.Verify( x => x.AddMiddleware( It.IsAny <IGraphMiddlewareComponent <GraphQueryExecutionContext> >(), It.IsAny <string>()), Times.Exactly(1)); // ensur query level authorzation component was added queryPipeline.Verify( x => x.AddMiddleware <AuthorizeQueryOperationMiddleware <GraphSchema> >( It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(1)); // four components in the sub swaped field pipeline fieldPipeline.Verify(x => x.Clear()); fieldPipeline.Verify( x => x.AddMiddleware <IGraphMiddlewareComponent <GraphFieldExecutionContext> >(It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(4)); // ensure field authroization component was NOT added // to the field pipeline fieldPipeline.Verify( x => x.AddMiddleware <AuthorizeFieldMiddleware <GraphSchema> >(It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(0)); }