2020-04-10 08:27:43 +02:00
|
|
|
// Package core provides the primary API to include and use Super Graph with your own code.
|
|
|
|
// For detailed documentation visit https://supergraph.dev
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
/*
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"github.com/dosco/super-graph/core"
|
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
db, err := sql.Open("pgx", "postgres://postgrs:@localhost:5432/example_db")
|
|
|
|
if err != nil {
|
2020-04-24 03:24:41 +02:00
|
|
|
log.Fatal(err)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 03:21:45 +02:00
|
|
|
sg, err := core.NewSuperGraph(nil, db)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
2020-04-24 03:24:41 +02:00
|
|
|
log.Fatal(err)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
query {
|
|
|
|
posts {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
ctx = context.WithValue(ctx, core.UserIDKey, 1)
|
|
|
|
|
|
|
|
res, err := sg.GraphQL(ctx, query, nil)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
2020-04-24 03:24:41 +02:00
|
|
|
log.Fatal(err)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(res.Data))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2020-06-03 07:03:12 +02:00
|
|
|
"hash/maphash"
|
2020-04-11 08:45:06 +02:00
|
|
|
_log "log"
|
|
|
|
"os"
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-20 05:48:49 +02:00
|
|
|
"github.com/chirino/graphql"
|
2020-04-10 08:27:43 +02:00
|
|
|
"github.com/dosco/super-graph/core/internal/allow"
|
|
|
|
"github.com/dosco/super-graph/core/internal/crypto"
|
|
|
|
"github.com/dosco/super-graph/core/internal/psql"
|
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextkey int
|
|
|
|
|
|
|
|
// Constants to set values on the context passed to the NewSuperGraph function
|
|
|
|
const (
|
|
|
|
// Name of the authentication provider. Eg. google, github, etc
|
|
|
|
UserIDProviderKey contextkey = iota
|
|
|
|
|
|
|
|
// User ID value for authenticated users
|
|
|
|
UserIDKey
|
|
|
|
|
|
|
|
// User role if pre-defined
|
|
|
|
UserRoleKey
|
|
|
|
)
|
|
|
|
|
|
|
|
// SuperGraph struct is an instance of the Super Graph engine it holds all the required information like
|
|
|
|
// datase schemas, relationships, etc that the GraphQL to SQL compiler would need to do it's job.
|
|
|
|
type SuperGraph struct {
|
2020-04-11 08:45:06 +02:00
|
|
|
conf *Config
|
|
|
|
db *sql.DB
|
|
|
|
log *_log.Logger
|
2020-04-23 07:42:16 +02:00
|
|
|
dbinfo *psql.DBInfo
|
2020-04-11 08:45:06 +02:00
|
|
|
schema *psql.DBSchema
|
|
|
|
allowList *allow.List
|
|
|
|
encKey [32]byte
|
2020-06-03 07:03:12 +02:00
|
|
|
hashSeed maphash.Seed
|
|
|
|
queries map[uint64]*query
|
2020-04-11 08:45:06 +02:00
|
|
|
roles map[string]*Role
|
|
|
|
getRole *sql.Stmt
|
2020-04-13 02:35:31 +02:00
|
|
|
rmap map[uint64]*resolvFn
|
2020-04-11 08:45:06 +02:00
|
|
|
abacEnabled bool
|
|
|
|
anonExists bool
|
|
|
|
qc *qcode.Compiler
|
|
|
|
pc *psql.Compiler
|
2020-04-23 02:51:14 +02:00
|
|
|
ge *graphql.Engine
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSuperGraph creates the SuperGraph struct, this involves querying the database to learn its
|
|
|
|
// schemas and relationships
|
2020-04-11 08:45:06 +02:00
|
|
|
func NewSuperGraph(conf *Config, db *sql.DB) (*SuperGraph, error) {
|
2020-04-23 07:42:16 +02:00
|
|
|
return newSuperGraph(conf, db, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSuperGraph helps with writing tests and benchmarks
|
|
|
|
func newSuperGraph(conf *Config, db *sql.DB, dbinfo *psql.DBInfo) (*SuperGraph, error) {
|
|
|
|
if conf == nil {
|
|
|
|
conf = &Config{}
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
sg := &SuperGraph{
|
2020-06-03 07:03:12 +02:00
|
|
|
conf: conf,
|
|
|
|
db: db,
|
|
|
|
dbinfo: dbinfo,
|
|
|
|
log: _log.New(os.Stdout, "", 0),
|
|
|
|
hashSeed: maphash.MakeSeed(),
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := sg.initConfig(); err != nil {
|
|
|
|
return nil, err
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := sg.initCompilers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sg.initAllowList(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sg.initPrepared(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:35:31 +02:00
|
|
|
if err := sg.initResolvers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-23 02:51:14 +02:00
|
|
|
if err := sg.initGraphQLEgine(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
if len(conf.SecretKey) != 0 {
|
|
|
|
sk := sha256.Sum256([]byte(conf.SecretKey))
|
|
|
|
conf.SecretKey = ""
|
|
|
|
sg.encKey = sk
|
|
|
|
} else {
|
|
|
|
sg.encKey = crypto.NewEncryptionKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result struct contains the output of the GraphQL function this includes resulting json from the
|
|
|
|
// database query and any error information
|
|
|
|
type Result struct {
|
|
|
|
op qcode.QType
|
|
|
|
name string
|
|
|
|
sql string
|
|
|
|
role string
|
|
|
|
|
|
|
|
Error string `json:"message,omitempty"`
|
|
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
|
|
Extensions *extensions `json:"extensions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphQL function is called on the SuperGraph struct to convert the provided GraphQL query into an
|
|
|
|
// SQL query and execute it on the database. In production mode prepared statements are directly used
|
|
|
|
// and no query compiling takes places.
|
|
|
|
//
|
|
|
|
// In developer mode all names queries are saved into a file `allow.list` and in production mode only
|
|
|
|
// queries from this file can be run.
|
|
|
|
func (sg *SuperGraph) GraphQL(c context.Context, query string, vars json.RawMessage) (*Result, error) {
|
2020-04-20 08:06:58 +02:00
|
|
|
var res Result
|
|
|
|
|
|
|
|
res.op = qcode.GetQType(query)
|
|
|
|
res.name = allow.QueryName(query)
|
|
|
|
|
|
|
|
// use the chirino/graphql library for introspection queries
|
|
|
|
// disabled when allow list is enforced
|
2020-04-21 16:03:05 +02:00
|
|
|
if !sg.conf.UseAllowList && res.name == "IntrospectionQuery" {
|
2020-05-01 08:20:13 +02:00
|
|
|
r := sg.ge.ServeGraphQL(&graphql.Request{Query: query})
|
2020-04-20 08:06:58 +02:00
|
|
|
res.Data = r.Data
|
2020-04-23 02:51:14 +02:00
|
|
|
|
2020-04-21 16:03:05 +02:00
|
|
|
if r.Error() != nil {
|
|
|
|
res.Error = r.Error().Error()
|
|
|
|
}
|
2020-04-20 08:06:58 +02:00
|
|
|
return &res, r.Error()
|
2020-04-20 05:48:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 08:06:58 +02:00
|
|
|
ct := scontext{Context: c, sg: sg, query: query, vars: vars, res: res}
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
if len(vars) <= 2 {
|
|
|
|
ct.vars = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyExists(c, UserIDKey) {
|
|
|
|
ct.role = "user"
|
|
|
|
} else {
|
|
|
|
ct.role = "anon"
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ct.execQuery()
|
|
|
|
if err != nil {
|
|
|
|
return &ct.res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ct.res.Data = json.RawMessage(data)
|
|
|
|
|
|
|
|
return &ct.res, nil
|
|
|
|
}
|
2020-04-21 16:03:05 +02:00
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
// GraphQLSchema function return the GraphQL schema for the underlying database connected
|
|
|
|
// to this instance of Super Graph
|
2020-04-21 16:03:05 +02:00
|
|
|
func (sg *SuperGraph) GraphQLSchema() (string, error) {
|
2020-04-23 02:51:14 +02:00
|
|
|
return sg.ge.Schema.String(), nil
|
2020-04-21 16:03:05 +02:00
|
|
|
}
|
2020-05-20 06:03:05 +02:00
|
|
|
|
|
|
|
// Operation function return the operation type from the query. It uses a very fast algorithm to
|
|
|
|
// extract the operation without having to parse the query.
|
|
|
|
func Operation(query string) OpType {
|
|
|
|
return OpType(qcode.GetQType(query))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name function return the operation name from the query. It uses a very fast algorithm to
|
|
|
|
// extract the operation name without having to parse the query.
|
|
|
|
func Name(query string) string {
|
|
|
|
return allow.QueryName(query)
|
|
|
|
}
|