api

package
v0.0.0-...-7544200 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LoginType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Login",
	Fields: graphql.Fields{
		"token": &graphql.Field{
			Type: graphql.String,
		},
	},
})
View Source
var RoleType = graphql.NewObject(graphql.ObjectConfig{
	Name: "RoleType",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.Int,
		},
		"userId": &graphql.Field{
			Type: graphql.Int,
		},
		"name": &graphql.Field{
			Type: graphql.String,
		},
		"isAdmin": &graphql.Field{
			Type: graphql.Boolean,
		},
		"isGod": &graphql.Field{
			Type: graphql.Boolean,
		},
	},
})
View Source
var RootMutation = graphql.NewObject(graphql.ObjectConfig{
	Name: "RootMutation",
	Fields: graphql.Fields{
		"registerUser": &graphql.Field{
			Type:        LoginType,
			Description: "Register user",
			Args: graphql.FieldConfigArgument{
				"name": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
				"email": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
				"password": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
			},
			Resolve: func(params graphql.ResolveParams) (interface{}, error) {
				name := params.Args["name"].(string)
				email := params.Args["email"].(string)
				password := params.Args["password"].(string)
				token, err := authentication.RegisterUser(name, email, password, false)

				if err != nil {
					return nil, err
				}

				return token, nil
			},
		},
		"loginUser": &graphql.Field{
			Type:        LoginType,
			Description: "Login user",
			Args: graphql.FieldConfigArgument{
				"email": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
				"password": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
			},
			Resolve: func(params graphql.ResolveParams) (interface{}, error) {
				email := params.Args["email"].(string)
				password := params.Args["password"].(string)
				token, err := authentication.LoginUser(email, password)

				if err != nil {
					return nil, err
				}

				return token, nil
			},
		},
	},
})
View Source
var RootQuery = graphql.NewObject(graphql.ObjectConfig{
	Name: "RootQuery",
	Fields: graphql.Fields{
		"getUser": &graphql.Field{
			Type:        UserType,
			Description: "Query for getting user logged in",
			Resolve: func(params graphql.ResolveParams) (interface{}, error) {
				jwt := params.Context.Value("jwt").(string)
				_, err := server.ValidateJWT(jwt)

				if err != nil {
					return nil, err
				}

				claims, err := server.ParseToken(jwt)
				userID := int(claims["id"].(float64))
				res, err := users.QueryUserById(userID)

				if err != nil {
					return nil, err
				}

				return res, nil
			},
		},
	},
})
View Source
var SuccessType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Success",
	Fields: graphql.Fields{
		"ok": &graphql.Field{
			Type: graphql.Boolean,
		},
	},
})
View Source
var UserType = graphql.NewObject(graphql.ObjectConfig{
	Name: "User",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.Int,
		},
		"name": &graphql.Field{
			Type: graphql.String,
		},
		"email": &graphql.Field{
			Type: graphql.String,
		},
		"role": &graphql.Field{
			Type:        RoleType,
			Description: "query user role",
			Resolve: func(params graphql.ResolveParams) (interface{}, error) {
				userId := params.Source.(model.User).Id
				res, err := users.QueryRole(userId)

				if err != nil {
					return nil, err
				}

				return res, nil
			},
		},
	},
})

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL