graphql mutation query error using graph-gophers/graphql-go package - go

PROBLEM
the mutation below
mutation {
signUp(signUpInput: {email: "newuser#gmail.com", username: "newUser", password: "asdfasdfawerawer"}) {
email
username
}
}
errors out the following
{
"errors": [
{
"message": "Cannot query field \"email\" on type \"SignUpResponse\".",
"locations": [
{
"line": 3,
"column": 5
}
]
},
{
"message": "Cannot query field \"username\" on type \"SignUpResponse\".",
"locations": [
{
"line": 4,
"column": 5
}
]
}
]
}
EXPECTATION
{
"data": {
"signUp": {
"email": "newuser#gmail.com",
"username": "newUser"
}
}
}
snippets of code
schema.graphql snippet
...
input SignUpInput {
username: String!
email: String!
password: String!
}
type Mutation {
signUp(signUpInput: SignUpInput): SignUpResponse!
}
type SignUpResponse {
ok: Boolean!
error: String
addedUser: User
}
resolvers.go snippet
...
// UserResolver ingests properties from User
type UserResolver struct{ u *User }
// UserID returns the userId of the user
func (r *UserResolver) UserID() graphql.ID {
return r.u.UserID
}
// Username returns the username of the user
func (r *UserResolver) Username() string {
return r.u.Username
}
// Email returns the email of the user
func (r *UserResolver) Email() string {
return r.u.Email
}
// Password returns the password of the user
func (r *UserResolver) Password() string {
return r.u.Password
}
type SignUpArgs struct {
Username string
Email string
Password string
}
// SignUp returns a new User from Db and its responses
func (r *RootResolver) SignUp(args struct{ SignUpInput *SignUpArgs }) (*SignUpResolver, error) {
// Find user:
u, err := r.Db.CreateUser(args.SignUpInput)
// need to deal with this different, so sort of error if we can't create the user
// a. user already exists
// b. email already exists
if err != nil {
// error creating the user
msg := "already signed up"
return &SignUpResolver{
Status: false,
Msg: &msg,
User: nil,
}, err
}
return &SignUpResolver{
Status: true,
Msg: nil,
User: &UserResolver{&u},
}, nil
}
// SignUpResolver is the response type
type SignUpResolver struct {
Status bool
Msg *string
User *UserResolver
}
// Ok for SignUpResponse
func (r *SignUpResolver) Ok() bool {
return r.Status
}
// Error for SignUpResponse
func (r *SignUpResolver) Error() *string {
return r.Msg
}
// AddedUser for SignUpResponse
func (r *SignUpResolver) AddedUser() *UserResolver {
return r.User
}
postgres.go - db operations
// User returns a single user
func (d *Db) User(uid graphql.ID) (User, error) {
var (
sqlStatement = `SELECT * FROM users WHERE user_id=$1;`
row *sql.Row
err error
u User
)
row = d.QueryRow(sqlStatement, uid)
err = row.Scan(
&u.UserID,
&u.Username,
&u.Email,
&u.Password,
)
util.Check(err, "row.Scan")
return u, nil
}
// CreateUser - inserts a new user
func (d *Db) CreateUser(i *SignUpArgs) (User, error) {
var (
sqlStatement = `
INSERT INTO users (email, username, password)
VALUES ($1, $2, $3)
RETURNING user_id`
userID graphql.ID
row *sql.Row
err error
u User
)
/***************************************************************************
* retrieve the UserID of the newly inserted record
* db.Exec() requires the Result interface with the
LastInsertId() method which relies on a returned value from postgresQL
* lib/pq does not however return the last inserted record
****************************************************************************/
row = d.QueryRow(sqlStatement, i.Email, i.Username, i.Password)
if err = row.Scan(&userID); err != nil {
// err: username or email is not unqiue --> user already exsits
return u, err
}
u, _ = d.User(userID)
return u, nil
}
I've tried to change the CreateUser to this
// CreateUser - inserts a new user
func (d *Db) CreateUser(i *SignUpArgs) (User, error) {
var (
sqlStatement = `
INSERT INTO users (email, username, password)
VALUES ($1, $2, $3)
RETURNING user_id`
userID graphql.ID
row *sql.Row
err error
u User
)
/***************************************************************************
* retrieve the UserID of the newly inserted record
* db.Exec() requires the Result interface with the
LastInsertId() method which relies on a returned value from postgresQL
* lib/pq does not however return the last inserted record
****************************************************************************/
row = d.QueryRow(sqlStatement, i.Email, i.Username, i.Password)
if err = row.Scan(&userID); err != nil {
// err: username or email is not unqiue --> user already exsits
return u, err
}
err = row.Scan(
&u.UserID,
&u.Username,
&u.Email,
&u.Password,
)
util.Check(err, "row.Scan User")
return u, nil
}
didn't do it obviously. hence the question, why the query error? seems like UserResolver can't return the User data provided the row is being returned from db.

Your type definitions include:
type Mutation {
signUp(signUpInput: SignUpInput): SignUpResponse!
}
type SignUpResponse {
ok: Boolean!
error: String
addedUser: User
}
It appears you're attempting to query the fields for the returned User, but signUp does not return a User object. Instead, signUp returns a SignUpResponse object, which, as the error states, does not have any fields named email or username.
The correct query would look something like this:
mutation {
signUp(signUpInput: {email: "newuser#gmail.com", username: "newUser", password: "asdfasdfawerawer"}) {
addedUser {
email
username
}
ok
error
}
}

Related

How to fix error with Query dynamodb request?

In DynamoDB I Have a table that contains:
- email (primary key)
- password (attribute)
- rname (attribute)
I'm using V1 of the AWS Go SDK, to implement to perform a query using just the primary key to my database:
My struct to unMarshal to is:
type Item struct {
Email string `json:"email"`
Password string `json:"password"`
Rname string `json:"rname"`
}
and the code:
result, err := client.Query(&dynamodb.QueryInput{
TableName: aws.String("accountsTable"),
KeyConditions: map[string]*dynamodb.Condition{
"email": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String(email),
},
},
},
},
})
if err != nil {
return false, err
}
item := []Item{}
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &item)
if err != nil {
return false, err
}
However, I get the issue that the key is invalid. I check the key in the database and it matches the one i print out to the console too.
Not sure how to get round this issue as example's i've seen seem to work for their's and look the same.
Any help in fixing this issue would be appreciated thanks :)
You need to set the values of password and rname to omitempty so that it's not set to empty values as they are not keys they should not be included on a Query as it throws an invalid key exception:
type Item struct {
Email string `json:"email" dynamodbav:"email,omitempty"`
Password string `json:"password" dynamodbav:"password,omitempty"`
Rname string `json:"rname" dynamodbav:"rname,omitempty"`
}
Update
I believe the issue is due to the fact you try to marshall the entire response in a single command, however, iterating works for me. (I do not use Go).
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"fmt"
)
func main() {
// Create Session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB Client with Logging
client := dynamodb.New(sess, aws.NewConfig())
type Item struct {
Email string `dynamodbav: "email"`
Password string `dynamodbav: "password,omitempty"`
Rname string `dynamodbav: "rname,omitempty"`
}
result, err := client.Query(&dynamodb.QueryInput{
TableName: aws.String("accountsTable"),
KeyConditions: map[string]*dynamodb.Condition{
"email": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String("lhnng#amazon.com"),
},
},
},
},
})
if err != nil {
fmt.Println("Query API call failed:")
fmt.Println((err.Error()))
}
for _, i := range result.Items {
item := Item{}
err = dynamodbattribute.UnmarshalMap(i, &item)
if err != nil {
fmt.Println("Got error unmarshalling: %s", err)
}
fmt.Println("Email: ", item.Email)
fmt.Println()
}
}
Moreover, as you use a single key of email, it means there is at most 1 item with the same email address, meaning you should use GetItem rather than Query:
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
func main() {
// Item to Get
type Item struct {
Email string `dynamodbav: "email"`
Password string `dynamodbav: "password,omitempty"`
Rname string `dynamodbav: "rname,omitempty"`
}
// Create Session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB Client
client := dynamodb.New(sess, aws.NewConfig())
// Get Item
result, err := client.GetItem(&dynamodb.GetItemInput{
TableName: aws.String("accountsTable"),
Key: map[string]*dynamodb.AttributeValue{
"email": {
S: aws.String("lhnng#amazon.com"),
},
},
})
// Catch Error
if err != nil {
fmt.Println("GetItem API call failed:")
fmt.Println((err.Error()))
}
item := Item{}
// Unmarhsall
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
if err != nil {
panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
}
// If Item Returns Empty
if item.Email == "" {
fmt.Println("Could not find Item")
return
}
// Print Result
fmt.Println("Found item:")
fmt.Println("Email: ", item.Email)
}

Gin - struct param is validated as null

I have a function here to create post requests and add a new user of type struct into a slice (the data for the API is just running in memory, so therefore no database):
type user struct {
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
}
var users = []user{
{ID: "1", FirstName: "John", LastName: "Doe", Email: "john.doe#email.com"},
}
func createUser(c *gin.Context) {
var newUser user
if len(newUser.ID) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"message": "user id is null"})
return
}
users = append(users, newUser)
c.JSON(http.StatusCreated, newUser)
}
Eveything worked fine, until i tried to make an 'if statement' that checks if an id for a user being sent with a post request is null.
The problem is that the if statement returns true, when it should return false. So if i for example try to make a post request with the following data:
{
"id": "2",
"first_name": "Jane",
"last_name": "Doe",
"email": "jane.doe#email.com"
}
The if statement that checks for an id with the length of 0 will be true, and therefore return a StatusBadRequest. If have also tried this way:
if newUser.ID == "" {
}
But this also returns true when it shouldn't.
If i remove this check and create the POST request it works just fine, and the new added data will appear when i make a new GET request.
Why do these if statements return true?
When you create a new user object with var newUser user statement, you are just creating an empty user object. You still have to bind the JSON string you are sending into that object. for that, what you need to do is: c.BindJSON(&newUser). full code will be like:
func createUser(c *gin.Context) {
var newUser user
err := c.BindJSON(&newUser)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
if len(newUser.ID) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"message": "user id is null"})
return
}
users = append(users, newUser)
c.JSON(http.StatusCreated, newUser)
}
you can check this link for an example provided by Gin: https://github.com/gin-gonic/examples/blob/master/basic/main.go#L61

How to write response with searched data - go-swagger & gorm

I used go-swagger and gorm for MySQL queries and one of my handlers is (retreiving one record)
api.UsersUserGetByIDHandler = users.UserGetByIDHandlerFunc(func(params users.UserGetByIDParams) middleware.Responder {
db := dbConn()
user := User{}
res := db.Table("users").Where("id = ?", params.UserID).Select("id, email, password, name").Scan(&user)
if res.RecordNotFound() {
message := "User not exists"
return users.NewUserGetByIDDefault(500).WithPayload(&models.Error{Message: &message})
}
log.Info(user) // {21 bxffcgb#emagggil.com 123456 Second}
return users.NewUserGetByIDOK() //How return right response there???
//.WriteResponse()
})
or retreive all data from table users
api.UsersUserListHandler = users.UserListHandlerFunc(func(params users.UserListParams) middleware.Responder {
db := dbConn()
var user []User
var count int
db.Table("users").Select("id, email, password, name").Scan(&user).Count(&count)
log.Info(db.RecordNotFound())
if count == 0 {
message := "User not exists"
return users.NewUserGetByIDDefault(500).WithPayload(&models.Error{Message: &message})
}
return users.NewUserGetByIDOK()
})
User Gorm struct is
type User struct { // user
ID int64 `gorm:"AUTO_INCREMENT"`
Email string `gorm:"type:varchar(200);unique_index"`
Password string `gorm:"size:200"`
Name string `gorm:"type:varchar(200)`
}
and same as models.Users
How properly return data there? I tried with WriteResponse and WithPayload but unsuccessful
There is an answer:
First change
user := User{}
to
user := new(models.Users)
and add at the end
ret := make([]*models.Users, 0)
ret = append(ret, user)
return users.NewUserGetByIDOK().WithPayload(ret)
WithPayload functions form file *_responses.go is defined as
// WithPayload adds the payload to the user get by Id o k response
func (o *UserGetByIDOK) WithPayload(payload []*models.Users) *UserGetByIDOK {
o.Payload = payload
return o
}

datastore doesn't put nested struct in Go

The structs look like this:
type Account struct {
Username string // NameKey
Password []byte `datastore:",noindex"`
RegistrationTime time.Time `datastore:",noindex"`
AppUser
}
type AppUser struct {
LoginEntries []LoginEntry `datastore:",noindex"`
}
type LoginEntry struct {
Timestamp time.Time `datastore:",noindex"`
UserAgent string `datastore:",noindex"`
IP string `datastore:",noindex"`
}
I'm also sure I put the data correctly, because other data has no problem being updated, and I tried to fmt.Println the content of account Account right before saving it in datastore (Put(ctx, key, &account) and when I print it then I can see all the AppUser information.. but when I later Get the user then the AppUser info doesn't exist (just shows up as {[]}).
I'm quite certain I have stored nested struct slices before in datastore without any problems, so I'm quite confused as to what might be causing it..
The Put func:
func PutAccount(ctx context.Context, acc Account) (*datastore.Key, error) {
if isValidUsername(acc.Username) != true {
return nil, errors.New("Invalid username.")
}
var hashedPassword []byte
if acc.RegistrationTime.IsZero() {
var err error
hashedPassword, err = bcrypt.GenerateFromPassword(acc.Password, 12)
if err != nil {
return nil, err
}
} else {
hashedPassword = acc.Password
}
account := Account{
Username: strings.ToLower(acc.Username),
Password: hashedPassword,
RegistrationTime: time.Now(),
AppUser: acc.AppUser}
fmt.Println("PutAccount, account:", account) // before saving it prints the AppUser without problems
key := datastore.NameKey("Account", account.Username, nil)
return database.DatastoreClient().Put(ctx, key, &account)
}
the Get func:
func GetAccount(ctx context.Context, key *datastore.Key) (Account, error) {
var account Account
err := database.DatastoreClient().Get(ctx, key, &account)
if err != nil {
return account, err
}
return account, nil
}
Using named struct works. eg:
type Account struct {
Username string // NameKey
Password []byte `datastore:",noindex"`
RegistrationTime time.Time `datastore:",noindex"`
AppUser AppUser
}
As to why anonymous embedded struct do not, this is probably worthy of an issue.

Querying the database for basic auth using go-http-auth with martini-go

I am attempting to use go-http-auth with martini-go. In the example given here - https://github.com/abbot/go-http-auth
package main
import (
auth "github.com/abbot/go-http-auth"
"fmt"
"net/http"
)
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
}
return ""
}
func handle(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
fmt.Fprintf(w, "<html><body><h1>Hello, %s!</h1></body></html>", r.Username)
}
func main() {
db, err := sql.Open("postgres", "postgres://blabla:blabla#localhost/my_db")
authenticator := auth.NewBasicAuthenticator("example.com", Secret)
m := martini.Classic()
m.Map(db)
m.Get("/users", authenticator.Wrap(MyUserHandler))
m.Run()
}
The Secret function is using a hardcoded user "john".
The authentication is successful when I execute
curl --user john:hello localhost:3000/users
Obviously, this is a trivial example with hardcoded username and password.
I am now changing the Secret function to this
func Secret(user, realm string) string {
fmt.Println("Executing Secret")
var db *sql.DB
var (
username string
password string
)
err := db.QueryRow("select username, password from users where username = ?", user).Scan(&username, &password)
if err == sql.ErrNoRows {
return ""
}
if err != nil {
log.Fatal(err)
}
//if user == "john" {
//// password is "hello"
//return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
//}
//return ""
return ""
}
But it fails with PANIC: runtime error: invalid memory address or nil pointer dereference. Which is obviously because I am attempting to instantiate var db *sql.DB in the Secret function. I cannot pass db *sql.DB into the Secret function either because auth.BasicNewAuthentication is expecting a Secret argument that conforms to type func (string, string) string.
How can I implement my database query correctly and return the password for comparison?
You can use a simple closure to pass in a reference to your DB to the authenticator function:
authenticator := auth.NewBasicAuthenticator("example.com", func(user, realm string) string {
return Secret(db, user, realm)
})
…and then change your Secret to accept the database as the first argument:
func Secret(db *sql.DB, user, realm string) string {
// do your db lookup here…
}
Alternative approach to Attilas answer. You can define a struct, define the Secret() handler on it and pass only the referenced function (go keeps the reference to the "owner") into the authhandler.
type SecretDb struct {
db *DB
}
func (db *SecretDb) Secret(user, realm string) string {
// .. use db.db here
}
func main() {
secretdb = SecretDb{db}
...
auth.NewBasicAuthenticator("example.com", secretdb.Secret)
...
}

Resources