Skip to content

This is Go code for a GraphQL API that interacts with a database. The code includes functions for querying and retrieving data from the database. The code uses the Go programming language and the GraphQL Go library to handle GraphQL queries. It also uses the database/sql library to interact with the database.

Notifications You must be signed in to change notification settings

JubaerHossain/gosql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GoSQL

GoDoc Go Report Card

GoSQL is a package that provides a simple way to query and manipulate SQL databases from GraphQL resolvers in Go.

Features - Query models by ID or with a where clause. - Specify which fields to select from the database using GraphQL queries. - Automatically convert database rows into Go struct instances. - Easily build a where clause with a map of column names and values. - Supports PostgreSQL, MySQL, and SQLite databases.

Installation

To use this package in your Go project, you can install it using the go get command:

go get github.com/JubaerHossain/gosql

Getting Started

To use GoSQL, first, you need to import the package:

import "github.com/JubaerHossain/gosql"

Then, define your model struct and make sure that the field names match the column names in your database table:

type User struct {
    ID        int
    FirstName string
    LastName  string
    Email     string
}

You can then use GoSQL to query the database and get the results as a slice of struct instances:

func GetUser(params graphql.ResolveParams) (interface{}, error) {
    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", params)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

You can also query a single record by ID:

func GetUserByID(params graphql.ResolveParams) (interface{}, error) {
    // Query the database for a user by ID
    user, err := gosql.FindByID(reflect.TypeOf(User{}), "users", params)
    if err != nil {
        return nil, err
    }

    // Return the result
    return user, nil
}

Where Clauses You can also build a where clause using a map of column names and values:

func GetUsersWithLastName(params graphql.ResolveParams) (interface{}, error) {
    // Build the where clause
    where := make(map[string]interface{})
    where["last_name"] = params.Args["lastName"].(string)

    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", params, where)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

Selecting Fields You can specify which fields to select from the database using GraphQL queries:

{
    users {
        firstName
        email
    }
}
func GetUser(params graphql.ResolveParams) (interface{}, error) {
    // Get the columns to select from the query
    selectColumn := gosql.GetColumns(params)

    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", selectColumn, params)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

Supported Databases GoSQL supports PostgreSQL, MySQL, and SQLite databases. To connect to your database, you need to set the DB variable in the database package to a database handle.

import "database/sql"
import _ "github.com/lib/pq"
import "lms/database"

func init() {
    db, err := sql.Open("postgres", "user=postgres dbname=lms sslmode=disable")
    if err != nil {
        panic(err)
    }

    database.DB = db
}

Contributing Contributions are welcome! If you find a bug or want to add a feature, please open an issue or submit a pull request.

About

This is Go code for a GraphQL API that interacts with a database. The code includes functions for querying and retrieving data from the database. The code uses the Go programming language and the GraphQL Go library to handle GraphQL queries. It also uses the database/sql library to interact with the database.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages