# Graphql

# Create a new user and invite account in a

Go to http://affine.domain.com/graphql

You need id of your workspace, you'll find it in the url : https://affine.example.com/workspace/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Create a new account

mutation {
     signUp(email: "newuser@example.com", name: "New User", password: "PASSWORD") {
         id
     }
}

# create invitation from your main account to your to the workspace you want to share

mutation {
  signIn(email: "admin@example.com", password:"PASSWORD") {
    id
    name
  },
  invite(
    email: "newuser@example.com"
    permission: Admin
    sendInviteMail: false
    workspaceId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  )
}

Get inviteId send in return : "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"

# Accept invitation

mutation {
  signIn(email: "NEWUSER@example.com", password:"PASSWORD") {
    id
    name
  },
  acceptInviteById(
    inviteId: "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
    sendAcceptMail: false
    workspaceId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  )
}

#Scripts python

import requests
import json

newuserName = "PSEUDO"
newuserEmail = "pseudo@example.com"
newuserPassword = "PASSWORD"
newuserId = ""

adminuserName = "PSEUDO"
adminuserEmail = "pseudo@example.com"
adminuserPassword = "PASSWORD"

adminuser_Workspace = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

url = 'https://affine.draak.fr/graphql'

# Create New Account

query = """
mutation {
     signUp(email: "%s", name: "%s", password: "%s") {
         id
     }
}
""" % (newuserEmail,newuserName,newuserPassword)

headers = {
    'Content-Type': 'application/json'
}
response = requests.post(url, json={'query': query}, headers=headers)
#print(json.loads(response.text))

# Get Invite ID
if response.status_code == 200:
    data = json.loads(response.text)
    #print(data["data"]["signUp"]["id"])
    newuserId = data["data"]["signUp"]["id"]
    #print(newuserId)
else:
    print(f"Error retrieving data, status code: {response.status_code}")


# Create invite to the workspace
query = """
mutation {
  signIn(email: "%s", password:"%s") {
    id
    name
  },
  invite(
    email: "%s"
    permission: Admin
    sendInviteMail: false
    workspaceId: "%s"
  )
}
""" % (adminuserEmail,adminuserPassword,newuserEmail,adminuser_Workspace)
headers = {
    'Content-Type': 'application/json'
}
response = requests.post(url, json={'query': query}, headers=headers)
print(json.loads(response.text))

# Get Invite ID
if response.status_code == 200:
    data = json.loads(response.text)
    #print(data["data"]["signUp"]["id"])
    inviteID = data["data"]["invite"]
    print(inviteID)
else:
    print(f"Error retrieving data, status code: {response.status_code}")

# Accept invitation

query = """
mutation {
  signIn(email: "%s", password:"%s") {
    id
    name
  },
  acceptInviteById(
    inviteId: "%s"
    sendAcceptMail: false
    workspaceId: "%s"
  )
}
""" % (newuserEmail,newuserPassword,inviteID,adminuser_Workspace)
headers = {
    'Content-Type': 'application/json'
}
response = requests.post(url, json={'query': query}, headers=headers)