Skip to content

Conversation

@Maydayof
Copy link
Contributor

Create new GraphQLHttpWebSocket instance if the UseWebSocketForQueriesAndMutations is true

HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version.ToString()));

_graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
if (options.UseWebSocketForQueriesAndMutations)
Copy link
Collaborator

@rose-a rose-a Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subscriptions must always use the websocket connection (obviously they don't work with regular HTTP requests).

You need to make sure that an instance of GraphQLHttpWebSocket is present when a subscription is created, otherwise you will receive a null reference exception on creating a subscription.

@rose-a rose-a linked an issue Jun 17, 2020 that may be closed by this pull request
Copy link
Collaborator

@rose-a rose-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, one last thing: Would you please use Lazy<GraphQLHttpWebSocket> instead of GetGraphQLHttpWebSocket()?

This has thread-safety already builtin...

@Maydayof
Copy link
Contributor Author

To instantiate a GraphQLHttpWebSocket requires a call into an instance method and pass an instance reference (/* public GraphQLHttpWebSocket(Uri webSocketUri, GraphQLHttpClient client) */ )
But, a field initializer cannot reference non-static field, method, property. At the moment I just can't figure out, how should I use the Lazy variant? Any idea?

@rose-a
Copy link
Collaborator

rose-a commented Jun 22, 2020

I think its well described in the API Reference.

Create a private field for a Lazy<GraphQLHttpWebSocket> and assign it inside the constructor using the factory method () => new GraphQLHttpWebSocket(GetWebSocketUri(), this).

Then you access the actual instance of GraphQLHttpWebSocket using lazyWebsocket.Value.
The instance will then be created on fist access to lazyWebsocket.Value.

@Maydayof
Copy link
Contributor Author

Thx, I have moved the initialization into the ctor, that was the problem.

@rose-a rose-a merged commit 812a740 into graphql-dotnet:master Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create GraphQLHttpWebSocket only when needed

2 participants