Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions v2/pkg/engine/resolve/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"net/http"

"github.com/cespare/xxhash/v2"

"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/httpclient"
)

Expand All @@ -29,3 +31,14 @@ type HookableSubscriptionDataSource interface {
// If an error is returned, the error is propagated to the client.
SubscriptionOnStart(ctx StartupHookContext, input []byte) (err error)
}

// SubscriptionTriggerHasher is an optional interface for subscription datasources
// that need to control which fields contribute to the trigger ID hash.
// When implemented, it replaces the default behaviour of hashing the full raw input.
// The datasource writes only its stable, identity-relevant fields into xxh.
// The resolver still appends the subgraph headers hash afterward.
type SubscriptionTriggerHasher interface {
// ProvideTriggerHashInput generates hash input to be used to create a trigger id
// and stores it inside xxh.
ProvideTriggerHashInput(ctx *Context, input []byte, xxh *xxhash.Digest) error
}
27 changes: 23 additions & 4 deletions v2/pkg/engine/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,26 @@ func (r *Resolver) UnsubscribeClient(connectionID ConnectionID) error {
// prepareTrigger safely gets the headers for the trigger Subgraph and computes the hash across headers and input
// the generated hash is the unique triggerID
// the headers must be forwarded to the DataSource to create the trigger
func (r *Resolver) prepareTrigger(ctx *Context, sourceName string, input []byte) (headers http.Header, triggerID uint64) {
func (r *Resolver) prepareTrigger(ctx *Context, sourceName string, input []byte, source SubscriptionDataSource) (
headers http.Header, triggerID uint64) {
keyGen := pool.Hash64.Get()
_, _ = keyGen.Write(input)
var usedHasher bool

// use custom data as hash input, if source provides it
hasher, implementsHasher := source.(SubscriptionTriggerHasher)
if implementsHasher {
err := hasher.ProvideTriggerHashInput(ctx, input, keyGen)
if err == nil {
usedHasher = true
}
}

// Fallback: use input as hash source, if either the source does not provide custom data
// or if there was an error with it.
if !usedHasher {
_, _ = keyGen.Write(input)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if ctx.SubgraphHeadersBuilder != nil {
var headersHash uint64
headers, headersHash = ctx.SubgraphHeadersBuilder.HeadersForSubgraph(sourceName)
Expand All @@ -1286,8 +1303,10 @@ func (r *Resolver) prepareTrigger(ctx *Context, sourceName string, input []byte)
_, _ = keyGen.Write(b[:])
}
}

triggerID = keyGen.Sum64()
pool.Hash64.Put(keyGen)

return headers, triggerID
}

Expand Down Expand Up @@ -1328,7 +1347,7 @@ func (r *Resolver) ResolveGraphQLSubscription(ctx *Context, subscription *GraphQ
return nil
}

headers, triggerID := r.prepareTrigger(ctx, subscription.Trigger.SourceName, input)
headers, triggerID := r.prepareTrigger(ctx, subscription.Trigger.SourceName, input, subscription.Trigger.Source)
id := SubscriptionIdentifier{
ConnectionID: NewConnectionID(),
SubscriptionID: 0,
Expand Down Expand Up @@ -1422,7 +1441,7 @@ func (r *Resolver) AsyncResolveGraphQLSubscription(ctx *Context, subscription *G
return err
}

headers, triggerID := r.prepareTrigger(ctx, subscription.Trigger.SourceName, input)
headers, triggerID := r.prepareTrigger(ctx, subscription.Trigger.SourceName, input, subscription.Trigger.Source)

return r.addSubscription(triggerID, &addSubscription{
ctx: ctx,
Expand Down
Loading