From 75dfa2992b49048ae9331bea9fc3008ec8649db4 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Tue, 25 Jan 2022 14:32:24 -0500 Subject: [PATCH] valid: Add Errorf function Add an Errorf function to create GraphQL errors without a validation context. --- valid/valid.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/valid/valid.go b/valid/valid.go index dba77aeb4cb60f34e092c42eb41430945195c984..13466b80f22fbb583022e84e088ca50f1e0e358c 100644 --- a/valid/valid.go +++ b/valid/valid.go @@ -18,6 +18,17 @@ type ValidationError struct { err *gqlerror.Error } +// Returns a new GraphQL error attached to the given field. +func Errorf(ctx context.Context, field string, msg string, items ...interface{}) error { + return &gqlerror.Error{ + Message: fmt.Sprintf(msg, items...), + Path: graphql.GetPath(ctx), + Extensions: map[string]interface{}{ + "field": field, + }, + } +} + // Creates a new validation context. func New(ctx context.Context) *Validation { return &Validation{