Skip to content

Creating a Class

BD103 edited this page May 27, 2021 · 1 revision

If you want to create a class that isn't in the project but is in the Node.js version, then follow this tutorial. To clear up confusion, the tutorial walks through creating a User class. This class already exists, so don't create a PR including a new one. Thanks! ๐Ÿ˜

Check here to see what should be supported. Also, the BaseClass may receive some breaking changes in the future, so you might want to wait on developing right now.

Setup

First, create a new module under the classes subpackage named after the class name. Then, paste in the following:

# user.py
from .base import BaseClass


class User(BaseClass):
  pass

This created a new class that extends BaseClass. Now let's update classes/__init__.py:

# classes/__init__.py
from .base import BaseClass
from .config import config
from .user import User

Queries, GraphQL, and everything in between

GraphQL is confusing, and Replit's API is not well documented. When developing the BaseClass, I tried to make it as easy as possible to create new classes. All you should need to do is create an __init__() function. Hopefully.

Here's a sample User class:

class User(BaseClass):
  def __init__(self, username: str, **kwargs):
    self.vars = {
      **{"username": username},
      **kwargs
    }
    self._query_args = {"$username": "String!"}
    self._types = {"userByUsername(username: $username)": ["fullName", "karma"]

Time to explain. When sending a GraphQL request, two things are included. The body (or format) and the variables. The variables are inserted into the body, then a response is sent back. self.vars exposes a dictionary of variables to the request. This is a required element and is recommended to include keyword arguments just in case.

self._query_args defines what variables are read, as well as their type. In this example, it defines that $username is a variable and it is a required string. (The ! at the end marks it required. For more, read the GraphQL website.)

self._types references the types requested, as well as their fields. In the example, it requests the type userByUsername and gives the variable $username as well. It then defines the fields in a list, currently including fullName and karma.

I know its really confusing, but this is how it is for now. You can read the source code for BaseClass if you want.

And finally

Now, open up core.py and add the class in.

# core.py
from . import classes

defaultInitVars = {"username": None}

class ReplAPI(object):
  def __init__(self, initVars: dict={}):
    self.vars = {**defaultInitVars, **initVars}

    self.User = classes.User

Congrats! You've created your own class in the ReplAPI.it project! I must warn you, this tutorial may be out of date. I'm almost positive these classes are going to see major changes in the future. Even so, I thank you for taking your time to contribute. See you around,

~ BD103, maintainer of this project.

Clone this wiki locally