Created
July 29, 2024 23:00
-
-
Save steven-tey/efb18503e3125b2d1524fd2579ff92e9 to your computer and use it in GitHub Desktop.
Retrieving `dclid` cookie in newsletter subscription and tracking a lead event with `dub`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/graphql_controller.rb | |
class GraphqlController < ApplicationController | |
def execute | |
# Reading the `dclid` cookie | |
dclid = cookies[:dclid] | |
# Handle the GraphQL query | |
variables = ensure_hash(params[:variables]) | |
context = { | |
dclid: dclid, # pass dclid to the context | |
# other context variables | |
} | |
result = MySchema.execute(params[:query], variables: variables, context: context, operation_name: params[:operationName]) | |
render json: result | |
end | |
private | |
def ensure_hash(ambiguous_param) | |
case ambiguous_param | |
when String | |
if ambiguous_param.present? | |
ensure_hash(JSON.parse(ambiguous_param)) | |
else | |
{} | |
end | |
when Hash, ActionController::Parameters | |
ambiguous_param | |
when nil | |
{} | |
else | |
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment