graphql

package
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2020 License: BSD-3-Clause Imports: 14 Imported by: 0

README

GraphQL

The graphql service type accepts GraphQL request and applies policies and validates against the schema.

The service settings and available input for the request are as follows:

The available service settings are as follows:

Name Type Description
mode setting mode 'a': validate request against GraphQL schema and maximum allowed query depth. mode 'b': Throttle requests based on server time
limit setting Limit specified in the format maxLimit-fillLimit-fillRate in milli seconds. Example: 1000-200-2000 indicates - maximum server time is set to 1000ms and client gains 200ms of server time per 2000ms

The available input for the request are as follows:

Name Type Description
query input GraphQL request string
schemaFile input GraphQL schema file path
maxQueryDepth input Maximum allowed GraphQL query depth
token string Token for which rate limit has to be applied
operation string An operation to perform: startconsume - start consuming the server time. stopconsume - stop consuming server time

The available response outputs are as follows:

Name Type Description
valid boolean true if the GraphQL query is valid
error boolean true if any error occured while inspecting the GraphQL query
errorMessage string The error message

A sample service definition is:

{
    "name": "GraphQL",
    "description": "GraphQL policies service",
    "ref": "github.com/project-flogo/microgateway/activity/graphql"
}

An example step that invokes JQL service using a GraphQL request from a HTTP trigger is:

{
    "service": "GraphQL",
    "input": {
        "query": "=$.payload.content",
        "schemaFile": "schema.graphql",
        "maxQueryDepth": 2
    }
}

Utilizing and extracting the response values can be seen in a conditional evaluation:

{
    "if": "$.GraphQL.outputs.error == true",
    "error": true,
    "output": {
        "code": 200,
        "data": {
            "error": "=$.GraphQL.outputs.errorMessage"
        }
    }
}

Maximum Query Depth (mode: a)

This mode allows to prevent clients from abusing deep query depth, Knowing your schema might give you an idea of how deep a legitimate query can go. example bad query:

query badquery {            #depth 0
  author() {                #depth 1
    posts {                 #depth 2
      author {              #depth 3
        posts {             #depth 4
          author {          #depth 5
          }
        }
      }
    }
  }
}

gateway configured with maxQueryDepth to 3 would consider above query too deep and the query is invalid.

Throttle based on server time (mode: b)

This mode allows to set up a maximum server time a client can use over a certain time frame and how much server time is added to the client over time.

Example:

{
  "name": "GraphQL",
  "description": "GraphQL policies service",
  "ref": "github.com/project-flogo/microgateway/activity/graphql",
  "settings": {
    "mode": "b",
    "limit": "1000-200-2000"
    }
}

In the above graphql service maximum server time is set to 1000ms and client gains 200ms of server time per 2000ms

TODO

  • Policy based on GraphQL query complexity
  • Throttling Based on Query Complexity

Documentation

Index

Constants

View Source
const (
	// GqlModeA GraphQL policy based on input query depth
	GqlModeA = "a"
	// GqlModeB GraphQL policy based on utilized server time
	GqlModeB = "b"
)

Variables

This section is empty.

Functions

func New

New creates new Activity

Types

type Activity

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

Activity is an GraphQLActivity inputs : {message} outputs: none

func (*Activity) Eval

func (a *Activity) Eval(ctx activity.Context) (done bool, err error)

Eval implements api.Activity.Eval - TBD

func (*Activity) Metadata

func (a *Activity) Metadata() *activity.Metadata

Metadata returns the activity's metadata

type Context

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

Context graphql context

type Input

type Input struct {
	Query         string `md:"query"`
	SchemaFile    string `md:"schemaFile"`
	MaxQueryDepth int    `md:"maxQueryDepth"`
	Token         string `md:"token"`
	Operation     string `md:"operation,allowed(startconsume,stopconsume)"`
}

Input input meta data

func (*Input) FromMap

func (i *Input) FromMap(values map[string]interface{}) error

func (*Input) ToMap

func (i *Input) ToMap() map[string]interface{}

type Output

type Output struct {
	Valid             bool   `md:"valid"`
	ValidationMessage string `md:"validationMessage"`
	Error             bool   `md:"error"`
	ErrorMessage      string `md:"errorMessage"`
}

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

type Settings

type Settings struct {
	Mode  string `md:"mode,allowed(a,b)"`
	Limit string `md:"limit"`
}

Settings settings for the GraphQL policy service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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