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

Revert "fix(GraphQL): This PR add schema cleaning in GraphQL. (#6523)" #6559

Merged
merged 1 commit into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion graphql/admin/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"bytes"
"context"
"encoding/json"
dgoapi "github.com/dgraph-io/dgo/v200/protos/api"

dgoapi "github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/edgraph"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/graphql/resolve"
Expand Down
111 changes: 14 additions & 97 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,88 +554,11 @@ func completeSchema(sch *ast.Schema, definitions []string) {
}
}

func cleanupInput(sch *ast.Schema, def *ast.Definition, seen map[string]bool) {
// seen helps us avoid cycles
if def == nil || seen[def.Name] {
return
}

i := 0
// recursively walk over fields for an input type and delete those which are themselves empty.
for _, f := range def.Fields {
nt := f.Type.Name()
enum := sch.Types[nt] != nil && sch.Types[nt].Kind == "ENUM"
// Lets skip scalar types and enums.
if _, ok := scalarToDgraph[nt]; ok || enum {
def.Fields[i] = f
i++
continue
}

seen[def.Name] = true
cleanupInput(sch, sch.Types[nt], seen)
seen[def.Name] = false

// If after calling cleanup on an input type, it got deleted then it doesn't need to be
// in the fields for this type anymore.
if sch.Types[nt] == nil {
continue
}
def.Fields[i] = f
i++
}
def.Fields = def.Fields[:i]

if len(def.Fields) == 0 {
delete(sch.Types, def.Name)
}
}

func cleanSchema(sch *ast.Schema) {
// Let's go over inputs of the type TRef, TPatch and AddTInput and delete the ones which
// don't have field inside them.
for k := range sch.Types {
if strings.HasSuffix(k, "Ref") || strings.HasSuffix(k, "Patch") ||
(strings.HasPrefix(k, "Add") && strings.HasSuffix(k, "Input")) {
cleanupInput(sch, sch.Types[k], map[string]bool{})
}
}

// Let's go over mutations and cleanup those which don't have AddTInput defined in the schema
// anymore.
i := 0 // helps us overwrite the array with valid entries.
for _, field := range sch.Mutation.Fields {
custom := field.Directives.ForName("custom")
// We would only modify add type queries.
if custom != nil || !strings.HasPrefix(field.Name, "add") {
sch.Mutation.Fields[i] = field
i++
continue
}

// addT type mutations have an input which is AddTInput so if that doesn't exist anymore,
// we can delete the AddTPayload and also skip this mutation.
typ := field.Name[3:]
input := sch.Types["Add"+typ+"Input"]
if input == nil {
delete(sch.Types, "Add"+typ+"Payload")
continue
}
sch.Mutation.Fields[i] = field
i++

}
sch.Mutation.Fields = sch.Mutation.Fields[:i]
}

func addInputType(schema *ast.Schema, defn *ast.Definition) {
field := getFieldsWithoutIDType(schema, defn)
if len(field) != 0 {
schema.Types["Add"+defn.Name+"Input"] = &ast.Definition{
Kind: ast.InputObject,
Name: "Add" + defn.Name + "Input",
Fields: field,
}
schema.Types["Add"+defn.Name+"Input"] = &ast.Definition{
Kind: ast.InputObject,
Name: "Add" + defn.Name + "Input",
Fields: getFieldsWithoutIDType(schema, defn),
}
}

Expand All @@ -658,12 +581,10 @@ func addReferenceType(schema *ast.Schema, defn *ast.Definition) {
}
}

if len(flds) != 0 {
schema.Types[defn.Name+"Ref"] = &ast.Definition{
Kind: ast.InputObject,
Name: defn.Name + "Ref",
Fields: flds,
}
schema.Types[defn.Name+"Ref"] = &ast.Definition{
Kind: ast.InputObject,
Name: defn.Name + "Ref",
Fields: flds,
}
}

Expand Down Expand Up @@ -1062,12 +983,11 @@ func addAddPayloadType(schema *ast.Schema, defn *ast.Definition) {
addFilterArgument(schema, qry)
addOrderArgument(schema, qry)
addPaginationArguments(qry)
if schema.Types["Add"+defn.Name+"Input"] != nil {
schema.Types["Add"+defn.Name+"Payload"] = &ast.Definition{
Kind: ast.Object,
Name: "Add" + defn.Name + "Payload",
Fields: []*ast.FieldDefinition{qry, numUids},
}

schema.Types["Add"+defn.Name+"Payload"] = &ast.Definition{
Kind: ast.Object,
Name: "Add" + defn.Name + "Payload",
Fields: []*ast.FieldDefinition{qry, numUids},
}
}

Expand Down Expand Up @@ -1258,7 +1178,6 @@ func addAddMutation(schema *ast.Schema, defn *ast.Definition) {
},
}
schema.Mutation.Fields = append(schema.Mutation.Fields, add)

}

func addUpdateMutation(schema *ast.Schema, defn *ast.Definition) {
Expand Down Expand Up @@ -1309,9 +1228,6 @@ func addDeleteMutation(schema *ast.Schema, defn *ast.Definition) {
}

func addMutations(schema *ast.Schema, defn *ast.Definition) {
if schema.Types["Add"+defn.Name+"Input"] == nil {
return
}
addAddMutation(schema, defn)
addUpdateMutation(schema, defn)
addDeleteMutation(schema, defn)
Expand Down Expand Up @@ -1412,6 +1328,7 @@ func getFieldsWithoutIDType(schema *ast.Schema, defn *ast.Definition) ast.FieldL
(!hasID(schema.Types[fld.Type.Name()]) && !hasXID(schema.Types[fld.Type.Name()])) {
continue
}

fldList = append(fldList, createField(schema, fld))
}

Expand Down
1 change: 0 additions & 1 deletion graphql/schema/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ func NewHandler(input string) (Handler, error) {
headers := getAllowedHeaders(sch, defns)
dgSchema := genDgSchema(sch, typesToComplete)
completeSchema(sch, typesToComplete)
cleanSchema(sch)

if len(sch.Query.Fields) == 0 && len(sch.Mutation.Fields) == 0 {
return nil, gqlerror.Errorf("No query or mutation found in the generated schema")
Expand Down
1 change: 1 addition & 0 deletions graphql/schema/schemagen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestSchemaString(t *testing.T) {

_, err = FromString(newSchemaStr)
require.NoError(t, err)

outputFileName := outputDir + testFile.Name()
str2, err := ioutil.ReadFile(outputFileName)
require.NoError(t, err)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Author {
id: ID!
name: String! @search(by: [hash])
posts: [Post]
posts: [Post]
}

interface Post {
Expand All @@ -12,7 +12,7 @@ interface Post {
}

type Question implements Post {
answered: Boolean
answered: Boolean
}

type Answer implements Post {
Expand Down
Loading