Skip to content

Commit

Permalink
Merge pull request wundergraph#578 from wundergraph/v2-alpha
Browse files Browse the repository at this point in the history
V2 alpha
  • Loading branch information
pvormste committed Oct 9, 2023
1 parent 2593582 commit 17d5e22
Show file tree
Hide file tree
Showing 735 changed files with 352,241 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: ci
on:
pull_request:
branches:
- master
- v2-alfa
paths:
- 'v2/**'
- '.github/workflows/v2.yml'
push:
branches:
- master
- v2-alfa
paths:
- 'v2/**'
- '.github/workflows/v2.yml'
jobs:
test:
name: Build and test (go ${{ matrix.go }} / ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
matrix:
go: [ '1.19', '1.20' ]
os: [ubuntu-latest, windows-latest]
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v4
with:
go-version: ^${{ matrix.go }}
id: go
- name: Set git to use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: CI
working-directory: v2
run: make -f ../Makefile ci
- name: Run tests under race detector
working-directory: v2
if: runner.os != 'Windows' # These are very slow on Windows, skip them
run: make -f ../Makefile test-race

lint:
name: Linters
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.20
uses: actions/setup-go@v4
with:
go-version: ^1.20
- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Run linters
uses: golangci/golangci-lint-action@v3
with:
working-directory: v2
version: v1.51.1
args: --timeout=3m
ci:
name: CI Success
if: ${{ always() }}
runs-on: ubuntu-latest
needs: [test, lint]
steps:
- run: exit 1
if: >-
${{
contains(needs.*.result, 'failure')
|| contains(needs.*.result, 'cancelled')
}}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/r3labs/sse/v2 v2.8.1
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0
github.com/sebdah/goldie v0.0.0-20180424091453-8784dd1ab561
github.com/sashabaranov/go-openai v1.14.1
github.com/sebdah/goldie/v2 v2.5.3
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ github.com/santhosh-tekuri/jsonschema/v5 v5.3.0 h1:uIkTLo0AGRc8l7h5l9r+GcYi9qfVP
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/sebdah/goldie v0.0.0-20180424091453-8784dd1ab561 h1:IY+sDBJR/wRtsxq+626xJnt4Tw7/ROA9cDIR8MMhWyg=
github.com/sebdah/goldie v0.0.0-20180424091453-8784dd1ab561/go.mod h1:lvjGftC8oe7XPtyrOidaMi0rp5B9+XY/ZRUynGnuaxQ=
github.com/sashabaranov/go-openai v1.14.1 h1:jqfkdj8XHnBF84oi2aNtT8Ktp3EJ0MfuVjvcMkfI0LA=
github.com/sashabaranov/go-openai v1.14.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
Expand Down
21 changes: 21 additions & 0 deletions v2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 WunderGraph UG (haftungsbeschränkt)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions v2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.PHONY: test
test:
go test ./...

.PHONY: test-quick
test-quick:
go test -count=1 ./...

.PHONY: test-race
test-race:
go test -race ./...

# updateTestFixtures will update all! golden fixtures
.PHONY: updateTestFixtures
updateTestFixtures:
go test ./pkg/... -update

.PHONY: format
format:
go fmt ./...

.PHONY: prepare-merge
prepare-merge: format test

.PHONY: ci
ci: test

.PHONY: ci-quick
ci-full: test-quick

.PHONY: generate
generate: $(GOPATH)/bin/go-enum $(GOPATH)/bin/mockgen $(GOPATH)/bin/stringer
go generate ./...
go mod tidy

$(GOPATH)/bin/go-enum:
go get -u github.com/abice/go-enum
go install github.com/abice/go-enum

$(GOPATH)/bin/mockgen:
go get -u github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen

$(GOPATH)/bin/stringer:
go get -u -a golang.org/x/tools/cmd/stringer
go install golang.org/x/tools/cmd/stringer
143 changes: 143 additions & 0 deletions v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
[![GoDoc](https://godoc.org/github.com/wundergraph/graphql-go-tools/v2?status.svg)](https://godoc.org/github.com/wundergraph/graphql-go-tools/v2)
[![CI](https://github.com/wundergraph/graphql-go-tools/workflows/ci/badge.svg)](https://github.com/wundergraph/graphql-go-tools/workflows/ci/badge.svg)
# graphql-go-tools

[<p align="center"><img height="auto" src="./assets/logo.png"></p>](https://wundergraph.com/)

## Apollo Federation Gateway Replacement

This library can be used as a replacement for the Apollo Federation Gateway.
It implements the [Apollo Federation Specification](https://www.apollographql.com/docs/apollo-server/federation/federation-spec/).

In addition to the scope of other implementations, this one supports **Subscriptions!**

Check out the [Demo](/examples/federation).

## Overview

This repository implements low level building blocks to write graphql services in Go.

With this library you could build tools like:
- proxies
- caches
- server/client implementations
- a WAF
- be creative! =)

Currently implemented:

- GraphQL AST as of https://graphql.github.io/graphql-spec/June2018
- Token lexing
- AST parsing: parse bytes/string into AST
- AST printing: print an AST to an io.Writer
- supports indentation
- AST validation:
- all rules from the spec implemented
- AST visitor:
- simple visitor: fastest implementation, without field type definition information
- visitor: a bit more overhead but has field type definitions and other quirks
- AST normalization
- remove unnecessary include/skip directives
- field deduplication
- field selection merging
- fragment definition removal
- fragment spread inlining
- inline fragment merging
- remove self aliasing
- type extension merging
- remove type extensions
- Introspection: transforms a graphql schema into a resolvable Data Source
- AST execution
- query planning: turns a Query AST into a cacheable execution plan
- supported DataSources:
- GraphQL (multiple GraphQL services can be combined)
- static (static embedded data)
- REST
- query execution: takes a context object and executes an execution plan
- Middleware:
- Operation Complexity: Calculates the complexity of an operation based on the GitHub algorithm
- OperationReport: Makes it easy to collect errors during all phases of a request and enables easy error printing according to the GraphQL spec
- Playground: Easy hosting of GraphQL Playground (no external dependencies, simple middleware)
- Import Statements: combine multiple GraphQL files into one single schema using #import statements
- Implements the Apollo Federation Specification: Replacement for Apollo Federation Gateway

## Go version Info

This repos uses go modules so make sure to use the latest version of Go.

## Docs

https://godoc.org/github.com/wundergraph/graphql-go-tools/v2

## Usage

Look into the docs.
Other than that, tests definitely help understanding this library.

## Testing

`make test`

## Linting

`make lint`

## Performance

Most hot path operations have 0 allocations.
You should expect this library to exceed all alternatives in terms of performance.
I've compared my implementation vs. others but why trust my numbers?
Feel free to add comparisons via PR.

## Benchmarks

Parse Kitchen Sink (1020 chars, example from Facebook):
```shell script
pkg: github.com/wundergraph/graphql-go-tools/v2/pkg/astparser
BenchmarkKitchenSink 189426 5652 ns/op 0 B/op 0 allocs/op
BenchmarkKitchenSink 198253 5526 ns/op 0 B/op 0 allocs/op
BenchmarkKitchenSink 199924 5553 ns/op 0 B/op 0 allocs/op
BenchmarkKitchenSink 212695 5804 ns/op 0 B/op 0 allocs/op
```

CPU and Memory consumption for lexing, parsing as well as most other operations is neglectable, even for larger queries.

## Contributors

- [Jens Neuse][jens-neuse-github] (Project Lead & Active Maintainer)
- [Mantas Vidutis][mantas-vidutis-github]
- Contributions to the http proxy & the Context Middleware
- [Jonas Bergner][jonas-bergner-github]
- Contributions to the initial version of the parser, contributions to the tests
- Implemented Type Extension merging [#108](https://github.com/wundergraph/graphql-go-tools/v2/pull/108)
- [Patric Vormstein][patric-vormstein-github] (Active Maintainer)
- Fixed lexer on windows [#92](https://github.com/wundergraph/graphql-go-tools/v2/pull/92)
- Author of the graphql package to simplify the usage of the library
- Refactored the http package to simplify usage with http servers
- Author of the starwars package to enhance testing
- [Sergey Petrunin][sergey-petrunin-github] (Active Maintainer)
- Helped cleaning up the API of the pipeline package [#166](https://github.com/wundergraph/graphql-go-tools/v2/pull/166)
- Refactored the ast package into multiple files
- Author of the introspection converter (introspection JSON -> AST)
- Fixed various bugs in the parser & visitor & printer
- Refactored and enhanced the astimport package
- [Vasyl Domanchuk][vasyl-github]
- Implemented the logic to generate a federation configuration
- Added federation example

[jens-neuse-github]: https://github.com/jensneuse
[mantas-vidutis-github]: https://github.com/mvid
[jonas-bergner-github]: https://github.com/java-jonas
[patric-vormstein-github]: https://github.com/pvormste
[sergey-petrunin-github]: https://github.com/spetrunin
[vasyl-github]: https://github.com/chedom

## Contributions

Feel free to file an issue in case of bugs.
We're open to your ideas to enhance the repository.

You are open to contribute via PR's.
Please open an issue to discuss your idea before implementing it so we can have a discussion.
Make sure to comply with the linting rules.
You must not add untested code.
Binary file added v2/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions v2/cmd/directiveUnmarshalCode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cmd

import (
"bytes"
"io"
"os"

"github.com/spf13/cobra"

"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
"github.com/wundergraph/graphql-go-tools/v2/pkg/astparser"
"github.com/wundergraph/graphql-go-tools/v2/pkg/codegen"
"github.com/wundergraph/graphql-go-tools/v2/pkg/imports"
"github.com/wundergraph/graphql-go-tools/v2/pkg/operationreport"
)

var (
filesRegex string
packageName string
outFile string
directiveStructSuffix string
)

// directiveUnmarshalCodeCmd represents the directiveUnmarshalCode command
var directiveUnmarshalCodeCmd = &cobra.Command{
Use: "directiveUnmarshalCode",
Short: "Generates go code to unmarshal directives",
Long: `directiveUnmarshalCode is a cli to generate code to unmarshal directives, input object type definitions, scalars and enums into go code
This is a convenient helper to make working with configurations attached to a GraphQL schema using directives easier.`,
Example: `graphql-go-tools gen directiveUnmarshalCode -f ./pkg/codegen/testdata/schema.graphql -p main`,
RunE: func(cmd *cobra.Command, args []string) error {
scanner := &imports.Scanner{}
file, err := scanner.ScanRegex(filesRegex)
if err != nil {
return err
}

buf := bytes.Buffer{}
err = file.Render(false, &buf)
if err != nil {
return err
}

parser := astparser.NewParser()
doc := ast.NewDocument()
doc.Input.ResetInputBytes(buf.Bytes())
rep := &operationreport.Report{}

parser.Parse(doc, rep)
if rep.HasErrors() {
return rep
}

var out io.Writer
if outFile == "" {
out = os.Stdout
} else {
o, err := os.Create(outFile)
if err != nil {
return err
}
defer o.Close()
out = o
}

config := codegen.Config{
PackageName: packageName,
DirectiveStructSuffix: directiveStructSuffix,
}

gen := codegen.New(doc, config)
_, err = gen.Generate(out)
return err
},
}

func init() {
genCmd.AddCommand(directiveUnmarshalCodeCmd)

directiveUnmarshalCodeCmd.Flags().StringVarP(&filesRegex, "filesRegex", "f", "", "filesRegex is a regex to specify all the files the generator should use (required)")
_ = directiveUnmarshalCodeCmd.MarkFlagRequired("filesRegex")

directiveUnmarshalCodeCmd.Flags().StringVarP(&packageName, "packageName", "p", "", "packageName is the package for the generated code (required)")
_ = directiveUnmarshalCodeCmd.MarkFlagRequired("packageName")

directiveUnmarshalCodeCmd.Flags().StringVarP(&directiveStructSuffix, "directiveStructSuffix", "s", "", "directiveStructSuffix is the suffix which gets appended to all directive struct names to avoid naming collisions (optional)")

directiveUnmarshalCodeCmd.Flags().StringVarP(&outFile, "outFile", "o", "", "outFile is a flag to redirect the output directly into a file (optional)")
}
Loading

0 comments on commit 17d5e22

Please sign in to comment.