From 4851da8d199265e386f4e356281c90e8334f1b6f Mon Sep 17 00:00:00 2001 From: gilex-dev Date: Fri, 17 Nov 2023 19:09:01 +0100 Subject: [PATCH] update example queries. fix error on todo creation --- database/main.go | 2 +- database/todo.go | 4 +-- example.graphql | 73 ++++++++++++++++++++++++++++++++++++++- graph/schema.resolvers.go | 2 +- 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/database/main.go b/database/main.go index ef0880d..54a2d4a 100644 --- a/database/main.go +++ b/database/main.go @@ -78,7 +78,7 @@ func (db CustomDB) createSQLite3Tables() error { sql string }{ {"User", "userId INTEGER PRIMARY KEY NOT NULL, userName VARCHAR NOT NULL UNIQUE CHECK(length(userName)!=0), passwordHash VARCHAR NOT NULL CHECK(length(passwordHash)!=0), fullName VARCHAR CHECK(length(fullName)!=0)"}, - {"Todo", "todoId INTEGER PRIMARY KEY NOT NULL, text VARCHAR NOT NULL CHECK(length(text)!=0), IS_done BOOL NOT NULL, FK_User_userId INTEGER NOT NULL, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, + {"Todo", "todoId INTEGER PRIMARY KEY NOT NULL, text VARCHAR NOT NULL CHECK(length(text)!=0), IS_done BOOL NOT NULL DEFAULT false, FK_User_userId INTEGER NOT NULL, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"R_User_Role", "relationId INTEGER PRIMARY KEY NOT NULL, FK_Role_roleId INTEGER NOT NULL, FK_User_userId INTEGER NOT NULL, UNIQUE(FK_Role_roleId, FK_User_userId), FOREIGN KEY(FK_Role_roleId) REFERENCES Role(roleId) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"Role", "roleId INTEGER PRIMARY KEY NOT NULL, roleName VARCHAR NOT NULL UNIQUE CHECK(length(roleName)!=0), IS_admin BOOL NOT NULL, IS_userCreator BOOL NOT NULL"}, {"RefreshToken", "tokenId INTEGER PRIMARY KEY NOT NULL, FK_User_userId INTEGER NOT NULL, selector VARCHAR NOT NULL CHECK(length(selector)!=0) UNIQUE, tokenHash VARCHAR NOT NULL CHECK(length(tokenHash)!=0), expiryDate INTEGER NOT NULL, tokenName VARCHAR CHECK(length(tokenName)!=0), FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, diff --git a/database/todo.go b/database/todo.go index 9fed4a7..a53bce6 100644 --- a/database/todo.go +++ b/database/todo.go @@ -113,8 +113,8 @@ func (db CustomDB) GetAllTodos() ([]*model.Todo, error) { return todos, nil } -// AddTodo adds a Todo to passed UserID. Check if the source has the rights to call this. -func (db CustomDB) AddTodo(newTodo model.NewTodo) (*model.Todo, error) { +// CreateTodo creates a Todo for the passed newTodo.UserID. Check if the source has the rights to call this. +func (db CustomDB) CreateTodo(newTodo model.NewTodo) (*model.Todo, error) { statement, err := db.connection.Prepare("INSERT INTO Todo (text, FK_User_userID) VALUES (?, ?)") if err != nil { return nil, err diff --git a/example.graphql b/example.graphql index 3e03d52..c9fc075 100644 --- a/example.graphql +++ b/example.graphql @@ -10,6 +10,12 @@ query getUser { text done } + roles { + id + roleName + isAdmin + isUserCreator + } } } @@ -65,7 +71,13 @@ mutation updateTodo { } mutation createUser { - createUser(input: { userName: "1234Lorem", fullName: "Lorem I." }) { + createUser( + input: { + userName: "1234Lorem" + fullName: "Lorem I." + password: "t1meToDoSometh1ng" + } + ) { id } } @@ -75,3 +87,62 @@ mutation updateUser { fullName } } + +query getRefreshTokens { + refreshTokens { + id + userId + tokenName + } +} + +mutation createRefreshToken { + createRefreshToken(input: { tokenName: "myNewDevice" }) { + userId + tokenName + selector + token + } +} + +mutation deleteRefreshToken { + deleteRefreshToken(id: 1) +} + +query getRoles { + roles { + id + roleName + } +} + +query getRole { + role(id: 2) { + id + roleName + } +} + +mutation createRole { + createRole( + input: { roleName: "moderator", isAdmin: false, isUserCreator: true } + ) { + id + } +} + +mutation deleteRole { + deleteRole(id: 2) +} + +mutation addRole { + addRole(userId: 1, roleId: 2) { + roleName + } +} + +mutation removeRole { + removeRole(userId: 2, roleId: 1) { + roleName + } +} diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index 7449598..cdb8206 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -25,7 +25,7 @@ func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) // CreateTodo is the resolver for the createTodo field. func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) { - todo, err := globals.DB.AddTodo(input) + todo, err := globals.DB.CreateTodo(input) if err != nil { globals.Logger.Println("Failed to add new todo:", err) return nil, errors.New("failed to add new todo")