Graphql: HELP: how to get arguments from GraphQL payload (string)

Created on 29 Dec 2018  路  2Comments  路  Source: graphql-go/graphql

Hi,

I need help trying to get arguments from a graphql payload, which is essential a string. I have a query named emails that get's all the emails associated with a particular email address. This is how it's defined:

rootQuery := graphql.NewObject(graphql.ObjectConfig{
        Name: "Query",
        Fields: graphql.Fields{
            "emails": &graphql.Field{
                Type: graphql.NewList(emailType),
                Args: graphql.FieldConfigArgument{
                    "address": &graphql.ArgumentConfig{
                        Type: graphql.NewNonNull(graphql.String),
                    },
                },
                Resolve: func(params graphql.ResolveParams) (interface{}, error) {
                    // return results
                },
            },
        },
    })

So suppose a user makes the following query:

{
  emails(address: "[email protected]"){
    subject,
    timestamp
  }
}

I want to take the above payload as a string and essential get the args address. I don't want to do this in resolve. I know I can define address := params.Args["address"].(string) in resolve, but I need the functionality outside a resolve function. I'm trying to create a subscription, so a user can subscribe to a particular inbox and get new incoming emails and I can use the address argument to put them in a particular web socket channel. However, I'm being passed the graphql payload as a pure string and need a way to extract the arguments. Is there any way to do this outside of creating a resolve?

question

All 2 comments

Hi @mtusman, thanks for using the lib and reaching us for a question.

A simple solution for your use case would be to parse the query and use the visitor component, I've put together a simple working example:

package main

import (
    "fmt"
    "log"

    sourceAST "github.com/graphql-go/graphql/language/ast"
    "github.com/graphql-go/graphql/language/parser"
    "github.com/graphql-go/graphql/language/visitor"
)

func main() {
    query := `
        query {
            emails(address: "[email protected]") {
                subject
                timestamp
            }
        }
    `
    fields, err := VisitFields(query)
    if err != nil {
        log.Fatal(err)
    }
    for _, f := range fields {
        if f.Name.Value == "emails" {
            for _, arg := range f.Arguments {
                if arg.Name.Value == "address" {
                    fmt.Printf("address: %v \n", arg.Value.GetValue())
                }
            }
        }
    }
}

func VisitFields(query string) ([]*sourceAST.Field, error) {
    var fields []*sourceAST.Field
    ast, err := parser.Parse(parser.ParseParams{Source: query})
    if err != nil {
        return fields, err
    }
    v := &visitor.VisitorOptions{
        Enter: func(p visitor.VisitFuncParams) (string, interface{}) {
            if node, ok := p.Node.(*sourceAST.Field); ok {
                if node.SelectionSet == nil {
                    return visitor.ActionNoChange, nil
                }

                fields = append(fields, node)
            }
            return visitor.ActionNoChange, nil
        },
    }
    visitor.Visit(ast, v, nil)
    return fields, nil
}
(issue-430)-> go run main.go
address: [email protected]

Thanks @chris-ramon, that worked perfectly for my use case!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zbintliff picture zbintliff  路  3Comments

Fruchtgummi picture Fruchtgummi  路  4Comments

davejohnston picture davejohnston  路  3Comments

yookoala picture yookoala  路  4Comments

michelalbers picture michelalbers  路  3Comments