first issue

This commit is contained in:
2019-07-24 16:52:09 +02:00
commit af2391cf28
11 changed files with 241 additions and 0 deletions

12
mutations/mutations.go Normal file
View 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
View File

@ -0,0 +1,5 @@
package mutations
const (
ADD_USER = `INSERT INTO users (firstname, lastname) VALUES ($1, $2)`
)

35
mutations/user.go Normal file
View 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
},
}
}