first issue

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

BIN
graphql Executable file

Binary file not shown.

45
main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"cadoles/graphql/mutations"
"cadoles/graphql/postgres"
"cadoles/graphql/queries"
"log"
"net/http"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
)
func main() {
schemaConfig := graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: queries.GetRootFields(),
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: mutations.GetRootFields(),
}),
}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("Failed to create new schema, error: %v", err)
}
httpHandler := handler.New(&handler.Config{
Schema: &schema,
})
postgres.DBConnect()
defer postgres.DBClose()
http.Handle("/", httpHandler)
log.Print("ready: listening...\n")
http.ListenAndServe(":8383", nil)
}

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
},
}
}

37
postgres/postgres.go Normal file
View File

@ -0,0 +1,37 @@
package postgres
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
DB_HOST = "localhost"
DB_PORT = "5432"
DB_USER = "graphql"
DB_PASSWORD = "graphql"
DB_NAME = "graphql"
)
var (
DB *sql.DB
)
func checkErr(err error, DB *sql.DB) {
if err != nil {
panic(err)
}
}
func DBConnect() {
dbinfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME)
var err error
DB, err = sql.Open("postgres", dbinfo)
checkErr(err, DB)
}
func DBClose() {
DB.Close()
}

12
queries/queries.go Normal file
View File

@ -0,0 +1,12 @@
package queries
import (
"github.com/graphql-go/graphql"
)
// GetRootFields returns all the available queries.
func GetRootFields() graphql.Fields {
return graphql.Fields{
"user": GetUserQuery(),
}
}

5
queries/requests.go Normal file
View File

@ -0,0 +1,5 @@
package queries
const (
GET_USERS = `SELECT * FROM users`
)

37
queries/user.go Normal file
View File

@ -0,0 +1,37 @@
package queries
import (
"cadoles/graphql/postgres"
"cadoles/graphql/types"
"log"
"github.com/graphql-go/graphql"
)
// GetUserQuery returns the queries available against user type.
func GetUserQuery() *graphql.Field {
log.Print("GetUserQuery")
return &graphql.Field{
Type: graphql.NewList(types.UserType),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
var user types.User
users := []types.User{}
sqlStatement := GET_USERS
rows, err := postgres.DB.Query(sqlStatement)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&user.ID, &user.Firstname, &user.Lastname)
users = append(users, user)
if err != nil {
log.Fatalln(err)
}
}
return users, nil
},
}
}

20
types/role.go Normal file
View File

@ -0,0 +1,20 @@
package types
import (
"github.com/graphql-go/graphql"
)
// Role type definition.
type Role struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}
// RoleType is the GraphQL schema for the user type.
var RoleType = graphql.NewObject(graphql.ObjectConfig{
Name: "Role",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int},
"name": &graphql.Field{Type: graphql.String},
},
})

33
types/user.go Normal file
View File

@ -0,0 +1,33 @@
package types
import (
"github.com/graphql-go/graphql"
)
// User type definition.
type User struct {
ID int `db:"id" json:"id"`
Firstname string `db:"firstname" json:"firstname"`
Lastname string `db:"lastname" json:"lastname"`
}
// UserType is the GraphQL schema for the user type.
var UserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int},
"firstname": &graphql.Field{Type: graphql.String},
"lastname": &graphql.Field{Type: graphql.String},
"roles": &graphql.Field{
Type: graphql.NewList(RoleType),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
var roles []Role
// userID := params.Source.(User).ID
// Implement logic to retrieve user associated roles from user id here.
return roles, nil
},
},
},
})