Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into schemaconfig-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jun 26, 2019
2 parents 9be5aad + 5e7e94c commit 64aca61
Show file tree
Hide file tree
Showing 66 changed files with 9,195 additions and 2,440 deletions.
7 changes: 7 additions & 0 deletions .circleci/check-coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -euo pipefail
go get github.com/mattn/goveralls

go test -coverprofile=/tmp/coverage.out -coverpkg=./... $(go list github.com/99designs/gqlgen/... | grep -v server)
goveralls -coverprofile=/tmp/coverage.out -service=circle-ci -repotoken=$REPOTOKEN -ignore='example/*/*,example/*/*/*,integration/*,integration/*/*,codegen/testserver/*'
10 changes: 10 additions & 0 deletions .circleci/check-fmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -euo pipefail

go fmt ./...
if [[ $(git --no-pager diff) ]] ; then
echo "you need to run "go fmt" and commit the changes"
git --no-pager diff
exit 1
fi
11 changes: 11 additions & 0 deletions .circleci/check-generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -euo pipefail

go generate ./...

if [[ $(git --no-pager diff) ]] ; then
echo "you need to run "go generate ./..." and commit the changes"
git --no-pager diff
exit 1
fi
12 changes: 8 additions & 4 deletions .circleci/integration.sh → .circleci/check-integration
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/bin/bash

set -eu
set -euo pipefail

cd integration

go run ./server/server.go &

sleep 2

echo "### running jest integration spec"
./node_modules/.bin/jest --color


echo "### validating introspected schema"
./node_modules/.bin/graphql get-schema
SERVER_URL=http://localhost:8080/query ./node_modules/.bin/graphql get-schema

if ! diff <(tail -n +3 schema-expected.graphql) <(tail -n +3 schema-fetched.graphql) ; then
echo "The expected schema has changed, you need to update schema-expected.graphql with any expected changes"
exit 1
fi


7 changes: 7 additions & 0 deletions .circleci/check-linting
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -euo pipefail

curl -sL --fail https://github.com/golangci/golangci-lint/releases/download/v1.13/golangci-lint-1.13-linux-amd64.tar.gz | tar zxv --strip-components=1 --dir=/go/bin

golangci-lint run
77 changes: 38 additions & 39 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
version: 2
jobs:
build:
working_directory: /app
lint:
docker:
- image: docker:18
- image: circleci/golang:1.12
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run:
name: "docker build"
command: |
docker build -f .circleci/golang.Dockerfile -t gqlgen/golang .
docker build -f .circleci/node.Dockerfile -t gqlgen/node .
- run: go mod download
- run: .circleci/check-fmt
- run: .circleci/check-linting
- run: .circleci/check-generate

- run:
name: "golang tests"
command: docker run --rm gqlgen/golang .circleci/test.sh

- run:
name: "integration tests"
command: |
function cleanup {
echo "Cleaning up containers..."
docker kill $SERVER_CONTAINER 1>/dev/null 2>/dev/null || true
docker rm --force -v $SERVER_CONTAINER 1>/dev/null 2>/dev/null || true
}
trap cleanup EXIT
SERVER_CONTAINER=$(docker run -d \
-e PORT=1234 \
--name integration_server \
gqlgen/golang go run ./integration/server/server.go \
)
sleep 20
docker run \
-e SERVER_URL=http://integration_server:1234/query \
--link=integration_server \
gqlgen/node ../.circleci/integration.sh
test:
docker:
- image: circleci/golang:1.12
steps:
- checkout
- run: go mod download
- run: go test -race ./...

echo "### server logs"
docker logs $SERVER_CONTAINER
cover:
docker:
- image: circleci/golang:1.12
steps:
- checkout
- run: go mod download
- run: .circleci/check-coverage

exit $(docker inspect $SERVER_CONTAINER --format='{{.State.ExitCode}}')
integration:
docker:
- image: alpine:3.10
steps:
- checkout
- run: apk add --no-cache --no-progress nodejs npm go musl-dev git bash
- run: go mod download
- run: cd integration ; npm install
- run: .circleci/check-integration

workflows:
version: 2
build_and_test:
jobs:
- lint
- test
- cover
- integration

10 changes: 0 additions & 10 deletions .circleci/golang.Dockerfile

This file was deleted.

9 changes: 0 additions & 9 deletions .circleci/node.Dockerfile

This file was deleted.

21 changes: 0 additions & 21 deletions .circleci/test.sh

This file was deleted.

16 changes: 16 additions & 0 deletions codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ type FieldArgument struct {
Value interface{} // value set in Data
}

//ImplDirectives get not Builtin and location ARGUMENT_DEFINITION directive
func (f *FieldArgument) ImplDirectives() []*Directive {
d := make([]*Directive, 0)
for i := range f.Directives {
if !f.Directives[i].IsBuiltin() && f.Directives[i].IsLocation(ast.LocationArgumentDefinition) {
d = append(d, f.Directives[i])
}
}

return d
}

func (f *FieldArgument) DirectiveObjName() string {
return "rawArgs"
}

func (f *FieldArgument) Stream() bool {
return f.Object != nil && f.Object.Stream
}
Expand Down
20 changes: 4 additions & 16 deletions codegen/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@ func (ec *executionContext) {{ $name }}(ctx context.Context, rawArgs map[string]
{{- range $i, $arg := . }}
var arg{{$i}} {{ $arg.TypeReference.GO | ref}}
if tmp, ok := rawArgs[{{$arg.Name|quote}}]; ok {
{{- if $arg.Directives }}
getArg0 := func(ctx context.Context) (interface{}, error) { return ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, tmp) }

{{- range $i, $directive := $arg.Directives }}
getArg{{add $i 1}} := func(ctx context.Context) (res interface{}, err error) {
{{- range $dArg := $directive.Args }}
{{- if and $dArg.TypeReference.IsPtr ( notNil "Value" $dArg ) }}
{{ $dArg.VarName }} := {{ $dArg.Value | dump }}
{{- end }}
{{- end }}
n := getArg{{$i}}
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs "tmp" "n" }})
}
{{- end }}

tmp, err = getArg{{$arg.Directives|len}}(ctx)
{{- if $arg.ImplDirectives }}
directive0 := func(ctx context.Context) (interface{}, error) { return ec.{{ $arg.TypeReference.UnmarshalFunc }}(ctx, tmp) }
{{ template "implDirectives" $arg }}
tmp, err = directive{{$arg.ImplDirectives|len}}(ctx)
if err != nil {
return nil, err
}
Expand Down
24 changes: 18 additions & 6 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Binder connects graphql types to golang types using static analysis
type Binder struct {
pkgs []*packages.Package
pkgs map[string]*packages.Package
schema *ast.Schema
cfg *Config
References []*TypeReference
Expand All @@ -26,7 +26,9 @@ func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
return nil, err
}

mp := map[string]*packages.Package{}
for _, p := range pkgs {
populatePkg(mp, p)
for _, e := range p.Errors {
if e.Kind == packages.ListError {
return nil, p.Errors[0]
Expand All @@ -35,12 +37,23 @@ func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
}

return &Binder{
pkgs: pkgs,
pkgs: mp,
schema: s,
cfg: c,
}, nil
}

func populatePkg(mp map[string]*packages.Package, p *packages.Package) {
imp := code.NormalizeVendor(p.PkgPath)
if _, ok := mp[imp]; ok {
return
}
mp[imp] = p
for _, p := range p.Imports {
populatePkg(mp, p)
}
}

func (b *Binder) TypePosition(typ types.Type) token.Position {
named, isNamed := typ.(*types.Named)
if !isNamed {
Expand Down Expand Up @@ -75,10 +88,9 @@ func (b *Binder) FindType(pkgName string, typeName string) (types.Type, error) {
}

func (b *Binder) getPkg(find string) *packages.Package {
for _, p := range b.pkgs {
if code.NormalizeVendor(find) == code.NormalizeVendor(p.PkgPath) {
return p
}
imp := code.NormalizeVendor(find)
if p, ok := b.pkgs[imp]; ok {
return p
}
return nil
}
Expand Down
40 changes: 37 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -63,6 +64,13 @@ func LoadConfigFromDefaultLocations() (*Config, error) {
return LoadConfig(cfgFile)
}

var path2regex = strings.NewReplacer(
`.`, `\.`,
`*`, `.+`,
`\`, `[\\/]`,
`/`, `[\\/]`,
)

// LoadConfig reads the gqlgen.yml config file
func LoadConfig(filename string) (*Config, error) {
config := DefaultConfig()
Expand All @@ -79,9 +87,35 @@ func LoadConfig(filename string) (*Config, error) {
preGlobbing := config.SchemaFilename
config.SchemaFilename = StringList{}
for _, f := range preGlobbing {
matches, err := filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
var matches []string

// for ** we want to override default globbing patterns and walk all
// subdirectories to match schema files.
if strings.Contains(f, "**") {
pathParts := strings.SplitN(f, "**", 2)
rest := strings.TrimPrefix(strings.TrimPrefix(pathParts[1], `\`), `/`)
// turn the rest of the glob into a regex, anchored only at the end because ** allows
// for any number of dirs in between and walk will let us match against the full path name
globRe := regexp.MustCompile(path2regex.Replace(rest) + `$`)

if err := filepath.Walk(pathParts[0], func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if globRe.MatchString(strings.TrimPrefix(path, pathParts[0])) {
matches = append(matches, path)
}

return nil
}); err != nil {
return nil, errors.Wrapf(err, "failed to walk schema at root %s", pathParts[0])
}
} else {
matches, err = filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
}
}

for _, m := range matches {
Expand Down
Loading

0 comments on commit 64aca61

Please sign in to comment.