gql

package
v0.0.0-...-e6b71c9 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthTokenType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "AuthToken",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"clientId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"userId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"accessToken": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"refreshToken": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"expiresIn": &graphql.Field{
				Type: graphql.DateTime,
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

AuthTokenType defines a graphql type for AuthToken

View Source
var BasicUserType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "BasicUser",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"email": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"firstName": &graphql.Field{
				Type: graphql.String,
			},
			"lastName": &graphql.Field{
				Type: graphql.String,
			},
		},
	},
)

BasicUserType is similar to UserType except it does not include fields for anything except basic user info. Used in sharableStore query

View Source
var DeviceType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "Device",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"userId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"token": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"deletedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

DeviceType defines a graphql type for Device

View Source
var GroceryTripCategoryType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "GroceryTripCategory",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeCategory": &graphql.Field{
				Type: StoreCategoryType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					storeCategoryID := p.Source.(models.GroceryTripCategory).StoreCategoryID
					storeCategory := models.StoreCategory{}
					if err := db.Manager.Select("id, name").Where("id = ?", storeCategoryID).First(&storeCategory).Error; err != nil {
						return nil, err
					}
					return storeCategory, nil
				},
			},
			"items": &graphql.Field{
				Type: graphql.NewList(ItemType),
				Args: graphql.FieldConfigArgument{
					"filter": &graphql.ArgumentConfig{
						Type: graphql.String,
					},
				},
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					tripID := p.Source.(models.GroceryTripCategory).GroceryTripID
					categoryID := p.Source.(models.GroceryTripCategory).ID
					items, err := trips.RetrieveItemsInCategory(tripID, categoryID)
					if err != nil {
						return nil, err
					}
					return items, nil
				},
			},
		},
	},
)
View Source
var GroceryTripType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "GroceryTrip",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeID": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeName": &graphql.Field{
				Type: graphql.String,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					storeID := p.Source.(models.GroceryTrip).StoreID
					store := models.Store{}
					if err := db.Manager.Select("name").Where("id = ?", storeID).First(&store).Error; err != nil {
						return nil, err
					}
					return store.Name, nil
				},
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"completed": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Boolean),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"categories": &graphql.Field{
				Type: graphql.NewList(GroceryTripCategoryType),
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					tripID := p.Source.(models.GroceryTrip).ID
					categories := []models.GroceryTripCategory{}
					query := db.Manager.
						Joins("INNER JOIN store_categories ON store_categories.id = grocery_trip_categories.store_category_id").
						Where("grocery_trip_categories.grocery_trip_id = ?", tripID).
						Order("grocery_trip_categories.created_at DESC").
						Find(&categories).
						Error
					if err := query; err != nil {
						return nil, err
					}
					return categories, nil
				},
			},
		},
	},
)
View Source
var ItemType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "Item",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"groceryTripId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"categoryId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"categoryName": &graphql.Field{
				Type: graphql.String,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					groceryTripCategoryID := p.Source.(*models.Item).CategoryID
					groceryTripCategory := &models.GroceryTripCategory{}
					if err := db.Manager.Select("store_category_id").Where("id = ?", groceryTripCategoryID).First(&groceryTripCategory).Error; err != nil {
						return nil, err
					}
					storeCategory := &models.StoreCategory{}
					if err := db.Manager.Select("name").Where("id = ?", groceryTripCategory.StoreCategoryID).First(&storeCategory).Error; err != nil {
						return nil, err
					}

					return storeCategory.Name, nil
				},
			},
			"userId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"quantity": &graphql.Field{
				Type: graphql.Int,
			},
			"notes": &graphql.Field{
				Type: graphql.String,
			},
			"position": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Int),
			},
			"completed": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Boolean),
			},
			"user": &graphql.Field{
				Type: UserType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					userID := p.Source.(models.Item).UserID
					var user models.User
					if err := db.Manager.Where("id = ?", userID).First(&user).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
						return nil, err
					}
					return user, nil
				},
			},
			"mealId": &graphql.Field{
				Type: graphql.ID,
			},
			"mealName": &graphql.Field{
				Type: graphql.String,
			},
			"stapleItemId": &graphql.Field{
				Type: graphql.ID,
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

ItemType defines a graphql type for Item

View Source
var MealType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "Meal",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"recipeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"mealType": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"servings": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Int),
			},
			"notes": &graphql.Field{
				Type: graphql.String,
			},
			"date": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"users": &graphql.Field{
				Type: graphql.NewList(UserType),
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					mealUsers := p.Source.(models.Meal).Users
					var userIDs []uuid.UUID
					for i := range mealUsers {
						userIDs = append(userIDs, mealUsers[i].UserID)
					}
					var users []models.User
					if err := db.Manager.Where("id IN (?)", userIDs).Find(&users).Error; err != nil {
						return nil, err
					}
					return users, nil
				},
			},
			"store": &graphql.Field{
				Type: StoreType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					var store models.Store
					storeID := p.Source.(models.Meal).StoreID
					if err := db.Manager.Where("id = ?", storeID).Last(&store).Error; err != nil {
						return nil, err
					}
					return store, nil
				},
			},
			"recipe": &graphql.Field{
				Type: RecipeType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					var recipe models.Recipe
					recipeID := p.Source.(models.Meal).RecipeID
					query := db.Manager.
						Unscoped().
						Preload("Ingredients").
						Where("id = ?", recipeID).
						Last(&recipe).
						Error
					if err := query; err != nil {
						return nil, err
					}
					return recipe, nil
				},
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

MealType defines the gql type for Meal

View Source
var RecipeIngredientType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "RecipeIngredient",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"recipeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"amount": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"unit": &graphql.Field{
				Type: graphql.String,
			},
			"notes": &graphql.Field{
				Type: graphql.String,
			},
		},
	},
)

RecipeIngredientType defines the gql type for RecipeIngredient

View Source
var RecipeType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "Recipe",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.String,
			},
			"description": &graphql.Field{
				Type: graphql.String,
			},
			"mealType": &graphql.Field{
				Type: graphql.String,
			},
			"url": &graphql.Field{
				Type: graphql.String,
			},
			"imageUrl": &graphql.Field{
				Type: graphql.String,
			},
			"ingredients": &graphql.Field{
				Type: graphql.NewList(RecipeIngredientType),
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					return p.Source.(models.Recipe).Ingredients, nil
				},
			},
			"instructions": &graphql.Field{
				Type: graphql.String,
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

RecipeType defines the gql type for Recipe

View Source
var StoreCategoryType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "StoreCategory",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"deletedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)
View Source
var StoreInviteType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "StoreInvite",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"invitingUser": &graphql.Field{
				Type:        UserType,
				Description: "Returns the user who sent the invite",
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {

					storeID := p.Source.(models.Store).ID
					user, err := stores.RetrieveStoreCreator(storeID)
					if err != nil {
						return nil, err
					}
					return user, nil
				},
			},
		},
	},
)

StoreInviteType defines a graphql type for Store

View Source
var StoreStapleItemType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "StoreStapleItem",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

StoreStapleItemType defines a graphql type for Item

View Source
var StoreType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "Store",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"userId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"name": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"shareCode": &graphql.Field{
				Type: graphql.String,
			},
			"creator": &graphql.Field{
				Type:    BasicUserType,
				Resolve: resolvers.BasicUserResolver,
			},
			"trip": &graphql.Field{
				Type: GroceryTripType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					header := p.Info.RootValue.(map[string]interface{})["Authorization"]
					user, err := auth.FetchAuthenticatedUser(header.(string))
					if err != nil {
						return nil, err
					}

					storeID := p.Source.(models.Store).ID
					trip, err := trips.RetrieveCurrentStoreTripForUser(storeID, user)
					if err != nil {
						return nil, err
					}
					return trip, nil
				},
			},
			"users": &graphql.Field{
				Type: graphql.NewList(StoreUserType),
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					storeID := p.Source.(models.Store).ID
					storeUsers, err := stores.RetrieveStoreUsers(storeID)
					if err != nil {
						return nil, err
					}
					return storeUsers, nil
				},
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

StoreType defines a graphql type for Store

View Source
var StoreUserPreferenceType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "StoreUserPreference",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeUserId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"defaultStore": &graphql.Field{
				Type: graphql.Boolean,
			},
			"notifications": &graphql.Field{
				Type: graphql.Boolean,
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"deleteAt": &graphql.Field{
				Type: graphql.DateTime,
			},
		},
	},
)

StoreUserPreferenceType defines a graphql type for StoreUserPreference

View Source
var StoreUserType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "StoreUser",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"storeId": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"userId": &graphql.Field{
				Type: graphql.ID,
			},
			"email": &graphql.Field{
				Type: graphql.String,
			},
			"creator": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Boolean),
			},
			"active": &graphql.Field{
				Type: graphql.NewNonNull(graphql.Boolean),
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"user": &graphql.Field{
				Type: UserType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					userID := p.Source.(models.StoreUser).UserID
					user := &models.User{}
					if err := db.Manager.Where("id = ?", userID).First(&user).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
						return nil, err
					}
					return user, nil
				},
			},
		},
	},
)

StoreUserType defines a graphql type for StoreUser

View Source
var UserType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "User",
		Fields: graphql.Fields{
			"id": &graphql.Field{
				Type: graphql.NewNonNull(graphql.ID),
			},
			"email": &graphql.Field{
				Type: graphql.NewNonNull(graphql.String),
			},
			"name": &graphql.Field{
				Type: graphql.String,
			},
			"passwordResetToken": &graphql.Field{
				Type: graphql.String,
			},
			"passwordResetTokenExpiry": &graphql.Field{
				Type: graphql.DateTime,
			},
			"createdAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"updatedAt": &graphql.Field{
				Type: graphql.DateTime,
			},
			"defaultStoreId": &graphql.Field{
				Type: graphql.ID,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					userID := p.Source.(models.User).ID
					//TODO add index for this query?
					var storeIDs []uuid.UUID
					query := db.Manager.
						Model(&models.Store{}).
						Select("stores.id").
						Joins("INNER JOIN store_users ON store_users.store_id = stores.id").
						Joins("INNER JOIN store_user_preferences ON store_user_preferences.store_user_id = store_users.id").
						Where("store_users.user_id = ?", userID).
						Where("store_user_preferences.default_store = ?", true).
						Pluck("stores.id", &storeIDs).
						Error
					if err := query; err != nil {
						return nil, errors.New("default store id not found for user")
					}
					if len(storeIDs) > 0 {
						return storeIDs[0], nil
					}
					return nil, nil
				},
			},
			"accessToken": &graphql.Field{
				Type: graphql.String,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					userID := p.Source.(*models.User).ID
					var authToken models.AuthToken
					query := db.Manager.
						Select("access_token").
						Where("user_id = ?", userID).
						Order("created_at DESC").
						Last(&authToken).
						Error
					if err := query; err != nil {
						return nil, errors.New("token not found for user")
					}
					return authToken.AccessToken, nil
				},
			},

			"token": &graphql.Field{
				Type: AuthTokenType,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					userID := p.Source.(*models.User).ID
					authToken := &models.AuthToken{}
					query := db.Manager.
						Where("user_id = ?", userID).
						Order("created_at DESC").
						Last(&authToken).
						Error
					if err := query; err != nil {
						return nil, errors.New("token not found for user")
					}
					return authToken, nil
				},
			},
		},
	},
)

UserType defines a graphql type for User

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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