kosmo

package module
v0.0.0-...-1dac878 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

kosmo

go.dev reference Actions Status Go Report Card Coverage Status GitHub license

Kosmo is a microservice library whose goal is to offer GraphQL interfaces through native go types. As a GraphQL implementation it uses the "github.com/graphql-go/graphql" library. Because the GraphQL schema is created by native types and the resolver methods allow typed results to be returned, the service created can not only be used as a web interface, but can also be easily imported and used by other Go programs.

Installation

go get github.com/FelixWieland/kosmo

Usage

Here is a quick example to get you started:

package main

import (
	"errors"

	"github.com/FelixWieland/kosmo"
)

//Passenger holds the data of a passenger
type Passenger struct {
	ID   int
	Name string `description:"Forename and Surname"`
	Seat int
}

//Passengers holds multiple Passenger
type Passengers []Passenger

//ResolvePassengerArgs used to resolve a passenger
type ResolvePassengerArgs struct {
	ID int
}

var demoPassengers Passengers = Passengers{
	Passenger{
		ID:   0,
		Name: "Max",
		Seat: 1,
	},
	Passenger{
		ID:   1,
		Name: "Mia",
		Seat: 2,
	},
}

//GetPassenger returns a Passenger
func GetPassenger(args ResolvePassengerArgs) (Passenger, error) {
	for _, passenger := range demoPassengers {
		if passenger.ID == args.ID {
			return passenger, nil
		}
	}
	return Passenger{}, errors.New("Passenger not found")
}

//GetPassengers returns multiple Passengers
func GetPassengers() (Passengers, error) {
	return demoPassengers, nil
}

func main() {
	service := kosmo.Service{
		HTTPConfig: kosmo.HTTPConfig{
			Playground: true,
			Port:       ":8080",
		},
	}
	passenger := kosmo.Type(Passenger{}).Queries(GetPassenger)
	passengers := kosmo.Type(Passengers{}).Queries(GetPassengers)

	service.Schemas(passenger, passengers).Server().ListenAndServe()
}

Requests

Request your service by visiting "http://localhost:8080/" in your Browser and query:

query {
	GetPassengers {
		Name
	}
}

With cURL:

curl --location --request POST 'http://localhost:8080/' \
	--header 'Content-Type: application/json' \
	--data-raw '{"query":"\nquery{\GetPassengers{\nName\n}\n}","variables":{}}'

With JS-Fetch:

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var graphql = JSON.stringify({
  query: "\nquery{\GetPassengers {\nName\n}\n}",
  variables: {}
})
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: graphql,
  redirect: 'follow'
};

fetch("http://localhost:8080/", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Configurations

A kosmo Service can take the following configurations (the following values are default):

kosmo.Service{
		HTTPConfig: kosmo.HTTPConfig{
			APIBase: "/", 				// Root of the endpoint
			Port: ":8080",				// Port of the service
			Playground: false,			// GraphIQL Playground
		},
		GraphQLConfig: kosmo.GraphQLConfig{
			RemoveResolverPrefixes: false,			// Removes the given prefixes from the resolver names 
			ResolverPrefixes: []string{},			// Prefixes that should be removed
		},
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Read(string, func(SetCache)) interface{}
}

Cache interface

func NewCache

func NewCache() Cache

NewCache - Creates a new Cache

type Describer

type Describer struct {
	Value       interface{}
	Description string
}

Describer is a wrapper for any type that allows a description

type Field

type Field interface {
	// contains filtered or unexported methods
}

Field resolve the given type

type GraphQLConfig

type GraphQLConfig struct {
	RemoveResolverPrefixes bool
	ResolverPrefixes       []string
}

GraphQLConfig represents the graphql configuration options

type GraphQLSchema

type GraphQLSchema struct {
	// contains filtered or unexported fields
}

GraphQLSchema contains the type, the query and the mutations of the type

func Type

func Type(typedVar interface{}) *GraphQLSchema

Type creates a graphQL Schema

func (*GraphQLSchema) Mutations

func (t *GraphQLSchema) Mutations(resolverFunctions ...interface{}) *GraphQLSchema

Mutations adds the GraphQLMutation functions

func (*GraphQLSchema) Queries

func (t *GraphQLSchema) Queries(resolverFunctions ...interface{}) *GraphQLSchema

Queries adds the Query resolver to the Type

type HTTPConfig

type HTTPConfig struct {
	Port       string
	APIBase    string
	Playground bool
}

HTTPConfig represents the http configurations

type ResolveParams

type ResolveParams graphql.ResolveParams

ResolveParams Params for Field.resolve()

type Service

type Service struct {
	HTTPConfig    HTTPConfig
	GraphQLConfig GraphQLConfig
	// contains filtered or unexported fields
}

Service represents a kosmo-microservice

func (*Service) Handler

func (s *Service) Handler() http.Handler

Handler - returns the raw HTTP handler

func (*Service) Schemas

func (s *Service) Schemas(schemas ...*GraphQLSchema) *Service

Schemas adds the schemas to the service

func (*Service) Server

func (s *Service) Server() *http.Server

Server - returns the http server

type SetCache

type SetCache func(interface{})

SetCache used to set a cache

type TagConfig

type TagConfig struct {
	Require bool
	Ignore  bool
}

TagConfig holds all flag configurations that can be made on a struct field

Directories

Path Synopsis
examples
getstarted command
lab command
nested command

Jump to

Keyboard shortcuts

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