Adaptic-ai / adaptic-backend

0 stars 0 forks source link

adaptic-backend

Adaptic Readme Banner

Description

The adaptic-backend repository provides a comprehensive collection of TypeScript type definitions that correspond to the various models and input types used in the Adaptic backend. These type definitions are crucial for ensuring type safety and consistency across backend operations, particularly when interacting with GraphQL queries, mutations, and Prisma models. By utilizing these type definitions, developers can build and maintain robust and scalable backend applications with confidence.

File Tree Structure

- fix-import-paths.js          # Automates the process of fixing import paths in generated TypeGraphQL Prisma resolver output files.
- package.json                 # Manages project configurations, scripts, and dependencies.
- schema.prisma                # Defines the data model for the application, establishing the structure of the database.
- src/
  - types/                     # Contains the TypeScript type definitions for the various models and input types.
  - auth.ts                    # Implements JWT authentication middleware to secure routes.
  - server.ts                  # Sets up the Express server integrated with Apollo Server for GraphQL requests and real-time updates.
- tsconfig.json                # Configures TypeScript compiler options for the project.

Purpose of Each File

Features

This repository offers a robust set of TypeScript type definitions for a comprehensive backend solution. Key features include:

Prerequisites

Before you begin, ensure you have the following prerequisites set up:

NPM Dependencies

The following NPM packages are required:

Environment Setup

  1. Ensure you have Node.js and npm installed. Download from Node.js official website.
  2. Clone the repository:
    git clone https://github.com/Adaptic-ai/adaptic-backend.git
  3. Navigate to the project directory:
    cd adaptic-backend

Installation

Follow these steps to install dependencies and set up the environment:

  1. Install NPM Dependencies:

    npm install
  2. Set Up Environment Variables: Create a .env file in the root of the project and add:

    DATABASE_URL=your_database_connection_string
    JWT_SECRET=your_jwt_secret
    PORT=4000
  3. Run the Application: Start the server:

    npm run start

Your backend server should now be up and running, ready to handle GraphQL requests and manage user authentication!

Usage

Importing Type Definitions

You can import and use the type definitions provided by the adaptic-backend package to ensure type safety in your application. Here’s how to do it:

For example, to use the Workspace type definition:

import { Workspace } from 'adaptic-backend/types';

// Example usage of the Workspace type
const myWorkspace: Workspace = {
  id: 'workspace-id',
  name: 'My Workspace',
  createdAt: new Date(),
  updatedAt: new Date(),
};

When performing a GraphQL query or mutation, you can use the imported type definitions to ensure your variables and data are correctly typed:

import { gql } from '@apollo/client';
import { WorkspaceInput } from 'adaptic-backend/types';

To test the backend, use the following sample data:

- **User Credentials**

  - Username: `testuser`
  - Password: `password123`

- **GraphQL Query Example**
  ```graphql
  query {
    user(id: "USER_ID") {
      id
      name
    }
  }
`;

// Example usage with the WorkspaceInput type
const newWorkspace: WorkspaceInput = {
  name: 'New Workspace',
};

const createWorkspace = async () => {
  const response = await apolloClient.mutate({
    mutation: CREATE_WORKSPACE,
    variables: { input: newWorkspace },
  });
  console.log(response.data);
};

Code Examples

Defining a Function with Type Safety

User Authentication

const authenticateUser = async (username, password) => {
  const response = await fetch('http://localhost:PORT/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      query: `
        mutation {
          login(username: "${username}", password: "${password}") {
            token
            user {
              id
              username
            }
          }
        }
      `
    })
  })
  const data = await response.json()
  return data
}

// Usage
authenticateUser('testuser', 'password123').then(console.log)

GraphQL Query

const fetchUserData = async userId => {
  const response = await fetch('http://localhost:PORT/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      query: `
        query {
          user(id: "${userId}") {
            id
            username
            email
          }
        }
      `
    })
  })
  const data = await response.json()
  return data
}

// Usage
fetchUserData('USER_ID').then(console.log)

Contributing

We welcome contributions to the adaptic-backend repository! To contribute, please follow these guidelines:

  1. Fork the Repository: Click the "Fork" button at the top right of the repository page.
  2. Clone Your Fork: Clone your forked repository to your local machine.
    git clone https://github.com/YOUR_USERNAME/adaptic-backend.git
  3. Create a Branch: Create a new branch for your feature or bug fix.
    git checkout -b feature/your-feature-name
  4. Make Changes: Implement your changes and ensure they are well-tested.
  5. Commit Your Changes: Commit your changes with a descriptive message.
    git commit -m "Add your message here"
  6. Push to Your Fork: Push your changes to your forked repository.
    git push origin feature/your-feature-name
  7. Create a Pull Request: Go to the original repository and create a pull request from your branch.

Please ensure that your code adheres to the existing style and includes appropriate tests.

License

This repository is licensed under the MIT License. See the LICENSE file for more details.

Acknowledgements

We would like to thank the open-source community for their contributions and support.


This project is a product of Adaptic.ai.

Thanks for reading this far! Why did the developer go broke? Because he used up all his cache!