def post_question(qtitle, qbody, tags_list, userId): if len(tags_list) != 0: tags = "{" + tags_list[0] for tag in tags_list[1:]: tags += ", " + tag tags += "}" qtitle = repr(qtitle) # for '\n' character, converting into raw string qbody = repr(qbody) insert_question = gql(''' mutation { insert_Questions( objects: { Title: "''' + qtitle + '''", Body: "''' + qbody + '''", Tags: "''' + tags + '''", UserId: ''' + str(userId) + ''' } ) { returning { Id } } } ''') print('new id = ') print(insert_question) pprint.pprint(insert_question) try: mutation_result = client.execute(insert_question) print("Question Id: ", mutation_result) except Exception as e: print(e)
def post_question(qtitle, qbody, tags_list, userId): if len(tags_list) != 0: tags = "{" + tags_list[0] for tag in tags_list[1:]: tags += ", " + tag tags += "}" insert_question = gql(''' mutation { insert_Questions( objects: { Title: "''' + qtitle + '''", Body: "''' + qbody + '''", Tags: "''' + tags + '''", UserId: ''' + str(userId) + ''' } ) { returning { Id } } } ''') try: mutation_result = client.execute(insert_question) print("Question Id: ", mutation_result) except Exception as e: print(e)
def query_question_for_page(Id): query_question = gql('''{ Questions_by_pk(Id: ''' + str(Id) + ''') { Id Title Body VoteCount created_at Question_Answers(order_by: {VoteCount: desc}) { Body VoteCount created_at Answer_User { Id EmailId FirstName LastName } } Question_User { Id EmailId FirstName LastName } } } ''') question = client.execute(query_question) question = question[ 'Questions_by_pk'] # "by_pk" means to query an entry by the private key pprint.pprint(question) return question
def questions_by_user(UserId): #select Title, Id from Questions where UserId = str(id) query_question = gql(''' # query { # Questions(_like: ''' + str(UserId) + ''') { # Id # Title # } # } query { Questions(where: {UserId: { _eq: ''' + str(UserId) + '''}}) { Id Title } } ''') questions = client.execute(query_question) questions = questions['Questions'] # pprint.pprint(questions) return questions
def print_users(): query_users = gql(''' query { Users { Id FirstName LastName EmailId } } ''') users = client.execute(query_users) for user in users["Users"]: print (user)
def user_exists(emailId): query_user = gql(''' query { Users (where: {EmailId: { _like: "''' + emailId + '''"}}) { EmailId } } ''') user = client.execute(query_user) if len(user["Users"]) == 0: return False if emailId != user["Users"][0]["EmailId"]: return False return True
def get_login_details(emailId): query_user = gql(''' query { Users (where: {EmailId: { _like: "''' + emailId + '''"}}) { EmailId, FirstName, LastName, Password, Id } } ''') user = client.execute(query_user) if len(user["Users"]) == 0: return None return user["Users"][0]
def query_question_for_list(Id): query_question = gql(''' query { Questions_by_pk(Id: ''' + str(Id) + ''') { Title Body VoteCount } } ''') question = client.execute(query_question) question = question[ 'Questions_by_pk'] # "by_pk" means to query an entry by the private key print("Title:", question['Title']) print("Body:", question['Body']) print("VoteCount:", question['VoteCount'])
def downvote_answer(aId): downvote = gql(''' mutation { update_Answers( where: { Id: { _eq: ''' + str(aId) + ''' } }, _inc: { VoteCount: -1 } ) { returning { VoteCount } } } ''') try: mutation_result = client.execute(downvote) print (mutation_result) except Exception as e: print (e)
def upvote_question(qId): upvote = gql(''' mutation { update_Questions( where: { Id: { _eq: ''' + str(qId) + ''' } }, _inc: { VoteCount: 1 } ) { returning { VoteCount } } } ''') try: mutation_result = client.execute(upvote) print(mutation_result) except Exception as e: print(e)
def register_user(emailId, firstName, lastName, password): insert_users = gql(''' mutation { insert_Users ( objects:{ EmailId: "''' + emailId + '''", FirstName: "''' + firstName + '''", LastName: "''' + lastName + '''", Password: "******" } ) { returning { Id } } } ''') mutation_result = client.execute(insert_users) print(mutation_result)
def query_question_for_list(Id): query_question = gql(''' query { Questions_by_pk(Id: ''' + str(Id) + ''') { Id Title Body VoteCount } } ''') question = client.execute(query_question) question = question['Questions_by_pk'] # "by_pk" means to query an entry by the private key return question # print ("Title:", question['Title']) # print ("Body:", question['Body']) # print ("VoteCount:", question['VoteCount']) # if __name__ == "__main__": # for id in id_list: # query_question_for_list(id)
def post_answer(qId, aBody, userId): insert_answer = gql(''' mutation { insert_Answers( objects: { QuestionId: ''' + str(qId) + ''', Body: "''' + aBody + '''", UserId: ''' + str(userId) + ''' } ) { returning { Id } } } ''') try: mutation_result = client.execute(insert_answer) print("Answer Id: ", mutation_result) except Exception as e: print(e)