first issue
This commit is contained in:
12
mutations/mutations.go
Normal file
12
mutations/mutations.go
Normal file
@ -0,0 +1,12 @@
|
||||
package mutations
|
||||
|
||||
import (
|
||||
"github.com/graphql-go/graphql"
|
||||
)
|
||||
|
||||
// GetRootFields returns all the available mutations.
|
||||
func GetRootFields() graphql.Fields {
|
||||
return graphql.Fields{
|
||||
"createUser": GetCreateUserMutation(),
|
||||
}
|
||||
}
|
5
mutations/requests.go
Normal file
5
mutations/requests.go
Normal file
@ -0,0 +1,5 @@
|
||||
package mutations
|
||||
|
||||
const (
|
||||
ADD_USER = `INSERT INTO users (firstname, lastname) VALUES ($1, $2)`
|
||||
)
|
35
mutations/user.go
Normal file
35
mutations/user.go
Normal file
@ -0,0 +1,35 @@
|
||||
package mutations
|
||||
|
||||
import (
|
||||
"cadoles/graphql/postgres"
|
||||
"cadoles/graphql/types"
|
||||
|
||||
"github.com/graphql-go/graphql"
|
||||
)
|
||||
|
||||
// GetCreateUserMutation creates a new user and returns it.
|
||||
func GetCreateUserMutation() *graphql.Field {
|
||||
return &graphql.Field{
|
||||
Type: types.UserType,
|
||||
Args: graphql.FieldConfigArgument{
|
||||
"firstname": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
"lastname": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
},
|
||||
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
|
||||
user := &types.User{
|
||||
Firstname: params.Args["firstname"].(string),
|
||||
Lastname: params.Args["lastname"].(string),
|
||||
}
|
||||
|
||||
_, err := postgres.DB.Exec(ADD_USER, user.Firstname, user.Lastname)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return user, nil
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user