Graphql: Access query/mutation/subscription as string in Resolve function

Created on 24 Oct 2017  路  1Comment  路  Source: graphql-go/graphql

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}}"
     },
}

Most helpful comment

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)
     },
}

>All comments

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)
     },
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rustysys-dev picture rustysys-dev  路  6Comments

yhc44 picture yhc44  路  5Comments

januszm picture januszm  路  6Comments

FrontMage picture FrontMage  路  5Comments

tobyjsullivan picture tobyjsullivan  路  5Comments