@@ -9,6 +9,19 @@ import (
99// GraphQlErrors contains one or more GraphQlError instances.
1010type GraphQlErrors []* GraphQlError
1111
12+ // NewGraphQlErrors wraps raw GraphQL error payloads in GraphQlError values.
13+ func NewGraphQlErrors (graphQLErrorPayloads []json.RawMessage ) GraphQlErrors {
14+ errs := make (GraphQlErrors , 0 , len (graphQLErrorPayloads ))
15+ for _ , graphQLErrorPayload := range graphQLErrorPayloads {
16+ var value any
17+ if err := json .Unmarshal (graphQLErrorPayload , & value ); err != nil {
18+ value = map [string ]any {"message" : string (graphQLErrorPayload )}
19+ }
20+ errs = append (errs , & GraphQlError {v : value })
21+ }
22+ return errs
23+ }
24+
1225func (gg GraphQlErrors ) Error () string {
1326 // This slightly convoluted implementation is used to ensure that output
1427 // remains stable with earlier versions of src-cli, which returned a wrapped
@@ -48,6 +61,23 @@ func (g *GraphQlError) Code() (string, error) {
4861 return "" , nil
4962}
5063
64+ // Path returns the GraphQL error path, if one was set on the error.
65+ func (g * GraphQlError ) Path () ([]any , error ) {
66+ e , ok := g .v .(map [string ]any )
67+ if ! ok {
68+ return nil , errors .Errorf ("unexpected GraphQL error of type %T" , g .v )
69+ }
70+
71+ if e ["path" ] == nil {
72+ return nil , nil
73+ }
74+ path , ok := e ["path" ].([]any )
75+ if ! ok {
76+ return nil , errors .Errorf ("unexpected path of type %T" , e ["path" ])
77+ }
78+ return path , nil
79+ }
80+
5181func (g * GraphQlError ) Error () string {
5282 j , _ := json .MarshalIndent (g .v , "" , " " )
5383 return string (j )
0 commit comments