public async Task ReceiveEvent_WithARegisteredClient_ClientRecievesEvent() { (var server, var socketClient, var apolloClient) = await this.CreateConnection(); // start the sub on the client var startMessage = new ApolloClientStartMessage() { Id = "abc", Payload = new GraphQueryData() { Query = "subscription { apolloSubscription { watchForPropObject { property1 } } }", }, }; await apolloClient.DispatchMessage(startMessage); var evt = new SubscriptionEvent() { Data = new TwoPropertyObject(), DataTypeName = SchemaExtensions.RetrieveFullyQualifiedDataObjectTypeName(typeof(TwoPropertyObject)), SchemaTypeName = SchemaExtensions.RetrieveFullyQualifiedSchemaTypeName(typeof(GraphSchema)), EventName = "[subscription]/ApolloSubscription/WatchForPropObject", }; var count = await server.ReceiveEvent(evt); Assert.AreEqual(1, count); socketClient.AssertApolloResponse(ApolloMessageType.DATA, "abc"); }
/// <summary> /// Invokes this middleware component allowing it to perform its work against the supplied context. /// </summary> /// <param name="context">The context containing the request passed through the pipeline.</param> /// <param name="next">The delegate pointing to the next piece of middleware to be invoked.</param> /// <param name="cancelToken">The cancel token.</param> /// <returns>Task.</returns> public Task InvokeAsync( GraphQueryExecutionContext context, GraphMiddlewareInvocationDelegate <GraphQueryExecutionContext> next, CancellationToken cancelToken) { if (context?.Items != null && context.IsValid && !context.IsCancelled) { // if a context item for the subscription event key was added by one of the extension methods // inspect it to try and find the events that were registered if (context.Items.ContainsKey(SubscriptionConstants.RAISED_EVENTS_COLLECTION_KEY)) { var collection = context.Items[SubscriptionConstants.RAISED_EVENTS_COLLECTION_KEY] as IList <SubscriptionEventProxy>; if (collection == null) { throw new GraphExecutionException( $"Unable to cast the context item '{SubscriptionConstants.RAISED_EVENTS_COLLECTION_KEY}' into " + $"{typeof(IList<SubscriptionEventProxy>).FriendlyName()}. Published subscription events could not be raised.", SourceOrigin.None); } else { foreach (var proxy in collection) { var eventData = new SubscriptionEvent() { Id = Guid.NewGuid().ToString(), SchemaTypeName = SchemaExtensions.RetrieveFullyQualifiedSchemaTypeName(typeof(TSchema)), Data = proxy.DataObject, DataTypeName = SchemaExtensions.RetrieveFullyQualifiedDataObjectTypeName(proxy.DataObject?.GetType()), EventName = proxy.EventName?.Trim(), }; _eventQueue.Enqueue(eventData); } } } } return(next(context, cancelToken)); }
/// <summary> /// Initializes a new instance of the <see cref="SubscriptionEventName" /> class. /// </summary> /// <param name="schemaType">Type of the schema which owns this name.</param> /// <param name="eventName">Name the event.</param> public SubscriptionEventName(Type schemaType, string eventName) : this(SchemaExtensions.RetrieveFullyQualifiedSchemaTypeName(schemaType), eventName) { }