schema

package
v0.0.0-...-e127ab3 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommentType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Comment",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.NewNonNull(graphql.ID),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*comment.Comment); ok {
					return meta.ID, nil
				}
				return nil, nil
			},
		},
		"title": &graphql.Field{
			Type: graphql.String,
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*comment.Comment); ok {
					return meta.Title, nil
				}
				return nil, nil
			},
		},
		"body": &graphql.Field{
			Type: graphql.NewNonNull(graphql.String),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*comment.Comment); ok {
					return meta.Body, nil
				}
				return nil, nil
			},
		},
	},
})
View Source
var MutationType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Mutation",
	Fields: graphql.Fields{
		"createUser": &graphql.Field{
			Type: UserType,
			Args: graphql.FieldConfigArgument{
				"email": &graphql.ArgumentConfig{
					Description: "New User Email",
					Type:        graphql.NewNonNull(graphql.String),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				email := p.Args["email"].(string)
				user := &user.User{
					Email: email,
				}
				err := model.Object.InsertUser(user)
				return user, err
			},
		},
		"removeUser": &graphql.Field{
			Type: graphql.Boolean,
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Description: "User ID to remove",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["id"].(string)
				id, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				err = model.Object.RemoveUserByID(id)
				return (err == nil), err
			},
		},
		"follow": &graphql.Field{
			Type: graphql.Boolean,
			Args: graphql.FieldConfigArgument{
				"follower": &graphql.ArgumentConfig{
					Description: "ID of follower user",
					Type:        graphql.NewNonNull(graphql.ID),
				},
				"followee": &graphql.ArgumentConfig{
					Description: "ID of followee user",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["follower"].(string)
				followerID, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				j := p.Args["followee"].(string)
				followeeID, err := strconv.Atoi(j)
				if err != nil {
					return nil, err
				}
				err = model.Object.Follow(followerID, followeeID)
				return err == nil, err
			},
		},
		"unfollow": &graphql.Field{
			Type: graphql.Boolean,
			Args: graphql.FieldConfigArgument{
				"follower": &graphql.ArgumentConfig{
					Description: "ID of follower user",
					Type:        graphql.NewNonNull(graphql.ID),
				},
				"followee": &graphql.ArgumentConfig{
					Description: "ID of followee user",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["follower"].(string)
				followerID, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				j := p.Args["followee"].(string)
				followeeID, err := strconv.Atoi(j)
				if err != nil {
					return nil, err
				}
				err = model.Object.Unfollow(followerID, followeeID)
				return err == nil, err
			},
		},
		"createPost": &graphql.Field{
			Type: PostType,
			Args: graphql.FieldConfigArgument{
				"user": &graphql.ArgumentConfig{
					Description: "Id of user creating the new post",
					Type:        graphql.NewNonNull(graphql.ID),
				},
				"title": &graphql.ArgumentConfig{
					Description: "New post title",
					Type:        graphql.NewNonNull(graphql.String),
				},
				"body": &graphql.ArgumentConfig{
					Description: "New post body",
					Type:        graphql.NewNonNull(graphql.String),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["user"].(string)
				userID, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				title := p.Args["title"].(string)
				body := p.Args["body"].(string)
				post := &post.Post{
					UserID: userID,
					Title:  title,
					Body:   body,
				}
				err = model.Object.InsertPost(post)
				return post, err
			},
		},
		"removePost": &graphql.Field{
			Type: graphql.Boolean,
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Description: "Post ID to remove",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["id"].(string)
				id, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				err = model.Object.RemovePostByID(id)
				return err == nil, err
			},
		},
		"createComment": &graphql.Field{
			Type: CommentType,
			Args: graphql.FieldConfigArgument{
				"user": &graphql.ArgumentConfig{
					Description: "Id of user creating the new comment",
					Type:        graphql.NewNonNull(graphql.ID),
				},
				"post": &graphql.ArgumentConfig{
					Description: "Id of post to attach the comment to",
					Type:        graphql.NewNonNull(graphql.ID),
				},
				"title": &graphql.ArgumentConfig{
					Description: "New comment title",
					Type:        graphql.NewNonNull(graphql.String),
				},
				"body": &graphql.ArgumentConfig{
					Description: "New comment body",
					Type:        graphql.NewNonNull(graphql.String),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["user"].(string)
				userID, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				j := p.Args["post"].(string)
				postID, err := strconv.Atoi(j)
				if err != nil {
					return nil, err
				}
				title := p.Args["title"].(string)
				body := p.Args["body"].(string)
				comment := &comment.Comment{
					UserID: userID,
					PostID: postID,
					Title:  title,
					Body:   body,
				}
				err = model.Object.InsertComment(comment)
				return comment, err
			},
		},
		"removeComment": &graphql.Field{
			Type: graphql.Boolean,
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Description: "Comment ID to remove",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["id"].(string)
				id, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				err = model.Object.RemoveCommentByID(id)
				return err == nil, err
			},
		},
	},
})
View Source
var PostType = graphql.NewObject(graphql.ObjectConfig{
	Name:       "Post",
	Interfaces: nil,
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.NewNonNull(graphql.ID),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*post.Post); ok {
					return meta.ID, nil
				}
				return nil, nil
			},
		},
		"title": &graphql.Field{
			Type: graphql.NewNonNull(graphql.String),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*post.Post); ok {
					return meta.Title, nil
				}
				return nil, nil
			},
		},
		"body": &graphql.Field{
			Type: graphql.NewNonNull(graphql.String),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*post.Post); ok {
					return meta.Body, nil
				}
				return nil, nil
			},
		},
	},
})
View Source
var QueryType = graphql.NewObject(graphql.ObjectConfig{
	Name: "Query",
	Fields: graphql.Fields{
		"user": &graphql.Field{
			Type: UserType,
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Description: "User ID",
					Type:        graphql.NewNonNull(graphql.ID),
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				i := p.Args["id"].(string)
				id, err := strconv.Atoi(i)
				if err != nil {
					return nil, err
				}
				return model.Object.GetUserByID(id)
			},
		},
	},
})
View Source
var UserType = graphql.NewObject(graphql.ObjectConfig{
	Name: "User",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.NewNonNull(graphql.ID),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*user.User); ok {
					return meta.ID, nil
				}
				return nil, nil
			},
		},
		"email": &graphql.Field{
			Type: graphql.NewNonNull(graphql.String),
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				if meta, ok := p.Source.(*user.User); ok {
					return meta.Email, nil
				}
				return nil, nil
			}},
	},
})

Functions

func InitSchema

func InitSchema() error

Types

This section is empty.

Jump to

Keyboard shortcuts

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