A simple (yet effective) GraphQL to HTTP / REST router

ReGraphQL

GitHub go.mod Go version

Docker Image Version (latest semver) Docker Image Size (latest semver)

GitHub Workflow Status

A simple (yet effective) GraphQL to REST / HTTP router.

ReGraphQL helps you expose your GraphQL queries / mutations as REST / HTTP endpoints. Doing this has the following benefits:

  • Queries are stored and controlled server side. No queries on the frontend.
  • Can modify and optimise your queries on demand without redoploying your (frontend) clients
  • Can use GET (HTTP Method) instead of GraphQL's POST
  • It's an OSS alternative to Kong's DeGraphQL

It helps you going...

From this

query($person: StarWarsPeople!) {
	getPerson(person: $person) {
		birthYear
		eyeColors
		films {
			title
		}
		gender
		hairColors
		height
		homeworld {
			name
		}
		mass
		name
		skinColors
		species {
			name
		}
		starships {
			name
		}
		vehicles {
			name
		}
	}
}

To

GET /persons/{person}

Index

Requirements

  • Go 1.18

Features

  • Maps HTTP params to GraphQL Variables
  • Forwards HTTP headers to GraphQL request
  • Set HTTP Status Code different to 200 if GraphQL query fails.
  • Hide GraphQL Error's Locations
  • Hide GraphQL Error's Stacktrace
  • Hide GraphQL Error's Path
  • Reads configuration from .env file
  • Reads configuration from environment variables
  • Logs using Kubernetes' klog v2
  • Docker Image below 20MB
  • Exposes metrics using Prometheus
  • Exposes Liveness, Readiness and Startup Probes

Service Endpoints

Liveness

GET /health/liveness

{"hostname":"pod_67804","status":"up"}

Returns HTTP Status Code OK (200) with the following JSON as soon as the application starts

{"hostname":"<< hostname >>","status":"up"}

Readiness

GET /health/readiness

{"hostname":"pod_67804","status":"ready"}
  1. If the application is Ready to receive requests

Returns HTTP Status Code OK (200) with the following JSON:

{"hostname":"<< hostname >>","status":"ready"}
  1. If the application is Not Ready to receive requests

Returns HTTP Status Code Precondition Failed (412) with the following JSON:

{"hostname":"<< hostname >>","status":"waiting"}

Metrics

The service exposes a Prometheus metrics endpoint at /metrics

GET /metrics

# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0

Quick start

  1. Describe a route in a file using yaml, which matches your HTTP endpoint with your GraphQL endpoint and Query
routes:
    - http:
          uri: '/persons/{person}'
          method: GET
      graphql:
          endpoint: 'https://swapi.skyra.pw/'
          query: |
              query($person: StarWarsPeople!) {
                  getPerson(person: $person) {
                      birthYear
                      eyeColors
                      films {
                          title
                      }
                      gender
                      hairColors
                      height
                      homeworld {
                          name
                      }
                      mass
                      name
                      skinColors
                      species {
                          name
                      }
                      starships {
                          name
                      }
                      vehicles {
                          name
                      }
                  }
              }

File starwars.yml

  1. Copy starwars.yml into /tmp/config

  2. Run the service (using Docker Compose)

[sudo] docker-compose up
  1. Query your new HTTP endpoint!
curl 'http://127.0.0.1:8080/graphql/persons/lukeskywalker' --compressed

Docker Image

Docker image is based on Google's Distroless. The final image is around 11.2MB and packs only the necessary things to run the service.

docker pull eaceto/regraphql:latest

Contributing

Before contributing to ReGraphQL, please read the instructions detailed in our contribution guide.

License

ReGraphQL is released under the MIT license. See LICENSE for details.

Author

Created by Ezequiel (Kimi) Aceto.

Owner
Ezequiel Aceto
Bachelor in System Analysis (Faculty of Engineering UBA, where I also studied Engineering in Electronics and Computing). Embedded systems developer enthusiast.
Ezequiel Aceto
Similar Resources

go generate based graphql server library

go generate based graphql server library

gqlgen What is gqlgen? gqlgen is a Go library for building GraphQL servers without any fuss. gqlgen is based on a Schema first approach — You get to D

Dec 31, 2022

gqlanalysis makes easy to develop static analysis tools for GraphQL in Go.

gqlanalysis makes easy to develop static analysis tools for GraphQL in Go.

gqlanalysis gqlanalysis defines the interface between a modular static analysis for GraphQL in Go. gqlanalysis is inspired by go/analysis. gqlanalysis

Dec 14, 2022

Tools to write high performance GraphQL applications using Go/Golang.

graphql-go-tools Sponsors WunderGraph Are you looking for a GraphQL e2e data fetching solution? Supports frameworks like NextJS, type safety with gene

Dec 27, 2022

GraphQL implementation for click house in Go.

GraphQL implementation for click house in Go.

clickhouse-graphql-go GraphQL implementation for clickhouse in Go. This package stores real time streaming websocket data in clickhouse and uses Graph

Nov 20, 2022

GraphQL parser comparison in different languages

graphql-parser-bench Parsing a schema or document can be a critical part of the application, especially if you have to care about latency. This is the

Jul 10, 2022

A GraphQL complete example using Golang And PostgreSQL

GraphQL with Golang A GraphQL complete example using Golang & PostgreSQL Installation Install the dependencies go get github.com/graphql-go/graphql go

Dec 6, 2022

This app is an attempt towards using go lang with graphql data fetch in react front end.

go_movies _A React js + GraphQL supported with backend in GoLang. This app is an attempt towards using go lang with graphql data fetch in react front

Dec 7, 2021

proof-of-concept minimal GraphQL service for LTV

LTV GraphQL Proof-of-Concept This is a barebones proof-of-concept of a possible GraphQL implementation that interacts with Core. It includes a few ver

Jan 4, 2022

Learn GraphQL with THE IDOLM@STER SHINY COLORS.

faaaar Learn GraphQL with THE IDOLM@STER SHINY COLORS. Getting Started The following is a simple example which get information about 20-year-old idols

Dec 11, 2022
Comments
  • Hide errors stacktrace

    Hide errors stacktrace

    Is your feature request related to a problem? Please describe. When a GraphQL error appears a complete description of the error is sent, which include stack trace information in most cases.

    Describe the solution you'd like

    Example error

    {
       "errors":[
          {
             "message":"Variable \"$person\" got invalid value \"lukesk\"; Value \"lukesk\" does not exist in \"StarWarsPeople\" enum.",
             "extensions":{
                "code":"BAD_USER_INPUT",
                "exception":{
                   "stacktrace":[
                      "GraphQLError: Variable \"$person\" got invalid value \"lukesk\"; Value \"lukesk\" does not exist in \"StarWarsPeople\" enum.",
                      "    at /usr/src/app/node_modules/graphql/execution/values.js:116:15",
                      "    at coerceInputValueImpl (/usr/src/app/node_modules/graphql/utilities/coerceInputValue.js:131:9)",
                      "    at coerceInputValueImpl (/usr/src/app/node_modules/graphql/utilities/coerceInputValue.js:54:14)",
                      "    at coerceInputValue (/usr/src/app/node_modules/graphql/utilities/coerceInputValue.js:37:10)",
                      "    at _loop (/usr/src/app/node_modules/graphql/execution/values.js:109:69)",
                      "    at coerceVariableValues (/usr/src/app/node_modules/graphql/execution/values.js:121:16)",
                      "    at getVariableValues (/usr/src/app/node_modules/graphql/execution/values.js:50:19)",
                      "    at buildExecutionContext (/usr/src/app/node_modules/graphql/execution/execute.js:203:61)",
                      "    at executeImpl (/usr/src/app/node_modules/graphql/execution/execute.js:101:20)",
                      "    at execute (/usr/src/app/node_modules/graphql/execution/execute.js:60:35)"
                   ]
                }
             }
          }
       ]
    }
    

    We should give the possibility to hide (stop propagating) errors[].extensions.exception , for example, by setting the following properties in the configuration yaml:

    routes:
        - http:
              uri: '/films/{film}'
              method: GET
          graphql:
              endpoint: 'https://swapi.skyra.pw/'
              query: |
                  {
                      getFilm(film: $film) {
                          title
                          releaseDate
                          producers
                          episodeId
                      }
                  }
              types:
                film: Float
              errors:
                hidePath: true # defaults to false
                hideLocations: true # defaults to false
                extensions:
                   hide: true     # defaults to false. True hides the complete **extensions** dictionary
                   codeMapping:
                      BAD_USER_INPUT:  400     # Maps **extensions.code** to HTTP Status. Error "BAD_USER_INPUT" maps to Status Code 400 (Bad Request)
    
  • Return (http status code) errors when query fails

    Return (http status code) errors when query fails

    Is your feature request related to a problem? Please describe. When a GraphQL query fails, it returns a response with Status Code 200.

    Describe the solution you'd like Capture and analyse the GraphQL response, determine if it's an error, and add a (failure) status code to the HTTP Response.

Go monolith with embedded microservices including GRPC,REST,GraphQL and The Clean Architecture.
Go monolith with embedded microservices including GRPC,REST,GraphQL and The Clean Architecture.

GoArcc - Go monolith with embedded microservices including GRPC,REST, graphQL and The Clean Architecture. Description When you start writing a Go proj

Dec 21, 2022
A simple Go, GraphQL, and PostgreSQL starter template

Simple Go/GraphQL/PostgreSQL template Purpose Have a good starting point for any project that needs a graphql, go, and postgres backend. It's a very l

Jan 8, 2022
A collection of Go packages for creating robust GraphQL APIs

api-fu api-fu (noun) (informal) Mastery of APIs. ?? Packages The top level apifu package is an opinionated library that aims to make it as easy as pos

Dec 28, 2022
graphql parser + utilities

graphql utilities for dealing with GraphQL queries in Go. This package focuses on actually creating GraphQL servers and expects you to describe your s

Dec 20, 2022
GraphQL server with a focus on ease of use
GraphQL server with a focus on ease of use

graphql-go The goal of this project is to provide full support of the GraphQL draft specification with a set of idiomatic, easy to use Go packages. Wh

Dec 31, 2022
An implementation of GraphQL for Go / Golang

graphql An implementation of GraphQL in Go. Follows the official reference implementation graphql-js. Supports: queries, mutations & subscriptions. Do

Dec 26, 2022
Convert Golang Struct To GraphQL Object On The Fly

Straf Convert Golang Struct To GraphQL Object On The Fly Easily Create GraphQL Schemas Example Converting struct to GraphQL Object type UserExtra stru

Oct 26, 2022
GraphQL server with a focus on ease of use
GraphQL server with a focus on ease of use

graphql-go The goal of this project is to provide full support of the GraphQL draft specification with a set of idiomatic, easy to use Go packages. Wh

Dec 25, 2022
GQLEngine is the best productive solution for implementing a GraphQL server 🚀

GQLEngine is the best productive solution for implementing a graphql server for highest formance examples starwars: https://github.com/gqlengine/starw

Apr 24, 2022
⚡️ A Go framework for rapidly building powerful graphql services

Thunder is a Go framework for rapidly building powerful graphql servers. Thunder has support for schemas automatically generated from Go types, live q

Dec 24, 2022