Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entgql: create plugin for gqlgen [Stage1] #241

Merged
merged 23 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions entgql/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package entgql
import (
"encoding/json"

"entgo.io/ent/entc/gen"
"entgo.io/ent/schema"
)

Expand Down Expand Up @@ -125,6 +126,19 @@ func (a *Annotation) Decode(annotation interface{}) error {
return json.Unmarshal(buf, a)
}

// decodeAnnotation decodes the annotation from the schema.
func decodeAnnotation(annotations gen.Annotations) (*Annotation, error) {
ant := &Annotation{}
if annotations == nil || annotations[ant.Name()] == nil {
return ant, nil
}

a8m marked this conversation as resolved.
Show resolved Hide resolved
if err := ant.Decode(annotations[ant.Name()]); err != nil {
return nil, err
}
return ant, nil
}

var (
_ schema.Annotation = (*Annotation)(nil)
_ schema.Merger = (*Annotation)(nil)
Expand Down
38 changes: 38 additions & 0 deletions entgql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"github.com/graphql-go/graphql/language/printer"
"github.com/graphql-go/graphql/language/source"
"github.com/graphql-go/graphql/language/visitor"

ast2 "github.com/vektah/gqlparser/v2/ast"
)

type (
Expand All @@ -49,6 +51,9 @@ type (
hooks []gen.Hook
templates []*gen.Template
scalarFunc func(*gen.Field, gen.Op) string

schema *ast2.Schema
models map[string]string
}

// ExtensionOption allows for managing the Extension configuration
Expand Down Expand Up @@ -188,6 +193,14 @@ func WithWhereFilters(b bool) ExtensionOption {
}
}

// WithSchemaGenerator add a hook for generate GQL schema
func WithSchemaGenerator() ExtensionOption {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

That means users can use it even if they don't use gqlgen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it require some refactor for WhereInput feature to working as the same way

return func(e *Extension) error {
e.hooks = append(e.hooks, e.genSchema())
return nil
}
}

// WithMapScalarFunc allows users to provides a custom function that
// maps an ent.Field (*gen.Field) into its GraphQL scalar type. If the
// function returns an empty string, the extension fallbacks to the its
Expand Down Expand Up @@ -313,6 +326,31 @@ func (e *Extension) isInput(name string) bool {
return false
}

// genSchema returns a new hook for generating
// the GraphQL schema from the graph.
func (e *Extension) genSchema() gen.Hook {
return func(next gen.Generator) gen.Generator {
return gen.GenerateFunc(func(g *gen.Graph) error {
if err := next.Generate(g); err != nil {
return err
}

genSchema, err := newSchemaGenerator(g)
if err != nil {
return err
}
if e.schema, err = genSchema.prepareSchema(); err != nil {
return err
}
if e.models, err = genSchema.genModels(); err != nil {
return err
}

return nil
})
}
}

// genWhereInputs returns a new hook for generating
// <T>WhereInputs in the GraphQL schema.
func (e *Extension) genWhereInputs() gen.Hook {
Expand Down
77 changes: 77 additions & 0 deletions entgql/internal/todoplugin/cmd/entgqlgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2019-present Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"log"

"entgo.io/contrib/entgql"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/codegen/config"
)

func main() {
var (
schemaPath = flag.String("path", "", "path to schema directory")
)
flag.Parse()
if *schemaPath == "" {
log.Fatal("entgqlgen: must specify schema path. use entgqlgen -path ./ent/schema")
}

exEntGQL, err := entgql.NewExtension(
entgql.WithSchemaGenerator(),
)
if err != nil {
log.Fatalf("creating entgql extension: %v", err)
}
err = entc.Generate(*schemaPath, &gen.Config{
Header: `
// Copyright 2019-present Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Code generated by entc, DO NOT EDIT.
`,
}, entc.Extensions(
// EntGQL should be the latest extension in the list
exEntGQL,
))
if err != nil {
log.Fatalf("running ent codegen: %v", err)
}

cfg, err := config.LoadConfigFromDefaultLocations()
if err != nil {
log.Fatalf("failed to load config %v", err)
}
err = api.Generate(cfg, api.PrependPlugin(exEntGQL.CreatePlugin()))
if err != nil {
log.Fatalf("running gqlgen: %v", err)
}
}
199 changes: 199 additions & 0 deletions entgql/internal/todoplugin/ent/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading