Is there a possibility to access the string representation of the query/mutation/subscription performed, within the Resolve function?
Example:
Somebody performs the following mutation:
mutation {
someMutation {
result {
a
b
}
}
}
someMutation := &graphql.Field{
Type: graphql.NewNonNull(someMutationPayload),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
mutationQuery := convertToString(p) //then mutationQuery == "someMutation{result{a b}}"
},
}
Figured it out in the meantime:
func convertFieldsToString(fields []*ast.Field) string {
var queryAsString string
for _, field := range fields {
queryAsString = queryAsString + field.Name.Value
selectionsAsString := convertFieldSelectionsToString(field)
if selectionsAsString != "" {
queryAsString = queryAsString + "{" + selectionsAsString + "}"
}
}
return queryAsString
}
func convertFieldSelectionsToString(field *ast.Field) string {
var selectionsAsString string
if field.SelectionSet != nil {
selections := field.SelectionSet.Selections
for _, selection := range selections {
selectionAsField := selection.(*ast.Field)
if selectionAsField.SelectionSet != nil {
selectionsAsString = selectionsAsString + " " + convertFieldsToString([]*ast.Field{selectionAsField}) + " "
} else {
selectionsAsString = selectionsAsString + " " + selectionAsField.Name.Value + " "
}
}
return selectionsAsString
}
return ""
}
And just call it from the Resolve function
someMutation := &graphql.Field{
Type: graphql.NewNonNull(someMutationPayload),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
mutationQuery := convertFieldsToString(p.Info.FieldASTs)
},
}
Most helpful comment
Figured it out in the meantime:
And just call it from the Resolve function