Skip to content

Instantly share code, notes, and snippets.

@koblas
Created August 3, 2018 15:43
Show Gist options
  • Save koblas/e2d4228b41bc0816c3df6122ffab1800 to your computer and use it in GitHub Desktop.
Save koblas/e2d4228b41bc0816c3df6122ffab1800 to your computer and use it in GitHub Desktop.
func init() {
mutationConfig.AddFieldConfig("sentimentAnalysis", &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "SentimentAnalysisResult",
Description: "Sentiment Analysis Response",
Fields: graphql.Fields{
"score": &graphql.Field{
Type: graphql.Int,
},
},
}),
Args: graphql.FieldConfigArgument{
"text": &graphql.ArgumentConfig{
Type: &graphql.NonNull{
OfType: graphql.String,
},
},
"lang": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: wrapIsAuth(wrapGraphQL(func(ctx context.Context, input struct {
Text string
Lang *string
}, _ []*ValidationError) (interface{}, error) {
logger := service.LoggerFromContext(ctx)
analyizer := service.GetSentimentAnalysis(ctx)
logger.Info("Calling SentimentAnalysis")
// https://godoc.org/github.com/cdipaolo/sentiment#Models.SentimentAnalysis
result := analyizer.SentimentAnalysis(input.Text, input.Lang)
return &result, nil
})),
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment