mutations

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Connect = graphql.Field{
	Type: types.ConnectionType,
	Args: graphql.FieldConfigArgument{
		"connectionId": &graphql.ArgumentConfig{
			Type: graphql.ID,
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		bc := new(models.Connection)
		if err := db.DB.NewSelect().
			Model(bc).
			Where("id = ?", p.Args["connectionId"]).
			Scan(p.Context); err != nil {
			return nil, err
		}

		if _, err := client.NewClient(bc.DSN); err != nil {
			return nil, err
		}

		return types.ConvertBunModelToConnection(bc), nil
	},
}
View Source
var CreateConnection = graphql.Field{
	Type: types.ConnectionType,
	Args: graphql.FieldConfigArgument{
		"input": &graphql.ArgumentConfig{
			Type: graphql.NewNonNull(inputs.CreateConnectionInputObject),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		var input inputs.CreateConnectionInput
		if err := x.Bind(p.Args["input"], &input); err != nil {
			return nil, err
		}

		if c, err := client.NewClient(input.DSN); err != nil {
			return nil, err
		} else {
			if err = c.TestConnection(p.Context); err != nil {
				return nil, err
			}
		}

		bc := models.Connection{
			Name: input.Name,
			DSN:  input.DSN,
		}

		_, err := db.DB.NewInsert().
			Model(&bc).
			Exec(p.Context)

		return types.ConvertBunModelToConnection(&bc), err
	},
}
View Source
var CreateRedisRecord = graphql.Field{
	Type: types.RedisRecordType,
	Args: graphql.FieldConfigArgument{
		"input": &graphql.ArgumentConfig{
			Type: graphql.NewNonNull(inputs.RedisRecordInputObject),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		connection := p.Context.Value(constants.CurrentConnectionContextKey).(*types.Connection)
		client := connection.Client.(*client.ClientRedis)
		var input types.RedisRecord
		if err := x.Bind(p.Args["input"], &input); err != nil {
			return nil, err
		}

		if err := client.SetWithExpiration(p.Context, input.Type, input.Key, input.Value, input.ExpireAt); err != nil {
			return nil, err
		}

		result, e, err := client.GetWithExpiration(p.Context, input.Key)
		if err != nil {
			return nil, err
		}

		return types.RedisRecord{
			Key:      input.Key,
			Type:     input.Type,
			Value:    result,
			ExpireAt: e,
		}, nil
	},
}
View Source
var DeleteRedisRecords = graphql.Field{
	Type: graphql.String,
	Args: graphql.FieldConfigArgument{
		"keys": &graphql.ArgumentConfig{
			Type: graphql.NewList(graphql.ID),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		connection := p.Context.Value(constants.CurrentConnectionContextKey).(*types.Connection)
		client := connection.Client.(*client.ClientRedis)
		var keys []string
		for _, key := range p.Args["keys"].([]any) {
			keys = append(keys, key.(string))
		}
		if err := client.Client.Del(p.Context, keys...).Err(); err != nil {
			return nil, err
		}

		return "ok", nil
	},
}
View Source
var Disconnect = graphql.Field{
	Type: types.ConnectionType,
	Args: graphql.FieldConfigArgument{
		"connectionId": &graphql.ArgumentConfig{
			Type: graphql.ID,
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		bc := new(models.Connection)
		if err := db.DB.NewSelect().
			Model(bc).
			Where("id = ?", p.Args["connectionId"]).
			Scan(p.Context); err != nil {
			return nil, err
		}

		if err := client.CloseClient(bc.DSN); err != nil {
			return nil, err
		}

		return types.ConvertBunModelToConnection(bc), nil
	},
}
View Source
var ExecSQLQuery = graphql.Field{
	Type: scalars.JsonType,
	Args: graphql.FieldConfigArgument{
		"query": &graphql.ArgumentConfig{
			Type: graphql.NewNonNull(graphql.String),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		connection := p.Context.Value(constants.CurrentConnectionContextKey).(*types.Connection)
		db, err := connection.Client.GetDB()
		if err != nil {
			return nil, err
		}

		rows, err := db.QueryContext(p.Context, p.Args["query"].(string))
		if err != nil {
			return nil, err
		}

		var r []map[string]any
		err = db.ScanRows(p.Context, rows, &r)

		return r, err
	},
}
View Source
var UpdateRedisRecord = graphql.Field{
	Type: types.RedisRecordType,
	Args: graphql.FieldConfigArgument{
		"key": &graphql.ArgumentConfig{
			Type: graphql.ID,
		},
		"input": &graphql.ArgumentConfig{
			Type: graphql.NewNonNull(inputs.RedisRecordInputObject),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		connection := p.Context.Value(constants.CurrentConnectionContextKey).(*types.Connection)
		client := connection.Client.(*client.ClientRedis)
		var input types.RedisRecord
		if err := x.Bind(p.Args["input"], &input); err != nil {
			return nil, err
		}

		if err := client.Client.Del(p.Context, p.Args["key"].(string)).Err(); err != nil {
			return nil, err
		}

		if err := client.SetWithExpiration(p.Context, input.Type, input.Key, input.Value, input.ExpireAt); err != nil {
			return nil, err
		}

		result, e, err := client.GetWithExpiration(p.Context, input.Key)
		if err != nil {
			return nil, err
		}

		return types.RedisRecord{
			Key:      input.Key,
			Type:     input.Type,
			Value:    result,
			ExpireAt: e,
		}, nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL