<commit_before>package com.apollographql.apollo3.network.ws

import com.apollographql.apollo3.api.AnyAdapter
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.http.DefaultHttpRequestComposer
import com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader
import okio.Buffer


/**
 * An [WsProtocol] that uses https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
 * It can carry queries in addition to subscriptions over the websocket
 */
class GraphQLWsProtocol(
    override val frameType: WsFrameType = WsFrameType.Binary
) : WsProtocol {
  override val name: String
    get() = "graphql-transport-ws"

  override fun connectionInit(): Map<String, Any?> {
    val map = mutableMapOf<String, Any?>(
        "type" to "connection_init",
    )
    return map
  }

  override fun connectionTerminate(): Map<String, Any?>? = null

  override fun <D : Operation.Data> operationStart(request: ApolloRequest<D>): Map<String, Any?> {
    return mapOf(
        "type" to "subscribe",
        "id" to request.requestUuid.toString(),
        "payload" to DefaultHttpRequestComposer.composePayload(request)
    )
  }

  override fun <D : Operation.Data> operationStop(request: ApolloRequest<D>): Map<String, Any?> {
    return mapOf(
        "type" to "complete",
        "id" to request.requestUuid.toString()
    )
  }

  @Suppress("UNCHECKED_CAST")
  override fun parseMessage(string: String): WsMessage {<commit_msg>add connectionPayload to GraphQLWsProtocol (#3110)<commit_after>package com.apollographql.apollo3.network.ws

import com.apollographql.apollo3.api.AnyAdapter
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.http.DefaultHttpRequestComposer
import com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader
import okio.Buffer


/**
 * An [WsProtocol] that uses https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
 * It can carry queries in addition to subscriptions over the websocket
 *
 * @param connectionPayload a map of additional parameters to send in the "connection_init" message
 */
class GraphQLWsProtocol(
    private val connectionPayload: Map<String, Any?>? = null,
    override val frameType: WsFrameType = WsFrameType.Binary,
) : WsProtocol {
  override val name: String
    get() = "graphql-transport-ws"

  override fun connectionInit(): Map<String, Any?> {
    val map = mutableMapOf<String, Any?>(
        "type" to "connection_init",
    )
    if (connectionPayload != null) {
      map.put("payload", connectionPayload)
    }
    return map
  }

  override fun connectionTerminate(): Map<String, Any?>? = null

  override fun <D : Operation.Data> operationStart(request: ApolloRequest<D>): Map<String, Any?> {
    return mapOf(
        "type" to "subscribe",
        "id" to request.requestUuid.toString(),
        "payload" to DefaultHttpRequestComposer.composePayload(request)
    )
  }

  override fun <D : Operation.Data> operationStop(request: ApolloRequest<D>): Map<String, Any?> {
    return mapOf(
        "type" to "complete",
        "id" to request.requestUuid.toString()
    )
  }

  @Suppress("UNCHECKED_CAST")
  override fun parseMessage(string: String): WsMessage {
{'fraction_non_alphanumeric': 0.11101083032490974, 'fraction_numerical': 0.0048134777376654635, 'mean_word_length': 4.1953125, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7397133', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1021, 'n_words': 238}