Skip to content

Commit

Permalink
Merge branch 'master' into dustin/upgrade-goccy-json-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech authored Nov 4, 2024
2 parents d4d957b + 7aa4add commit f692fbf
Show file tree
Hide file tree
Showing 14 changed files with 2,614 additions and 132 deletions.
2,343 changes: 2,257 additions & 86 deletions execution/engine/execution_engine_test.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions v2/pkg/ast/ast_enum_type_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func (d *Document) EnumTypeDefinitionContainsEnumValue(enumTypeDef int, valueNam
return false
}

func (d *Document) EnumTypeDefinitionContainsEnumValueWithDirective(enumTypeDefRef int, valueName, directiveName ByteSlice) (hasValue, hasDirective bool) {
for _, i := range d.EnumTypeDefinitions[enumTypeDefRef].EnumValuesDefinition.Refs {
if bytes.Equal(valueName, d.EnumValueDefinitionNameBytes(i)) {
if _, hasDirective := d.EnumValueDefinitionDirectiveByName(i, directiveName); hasDirective {
return true, true
}
return true, false
}
}
return false, false
}

func (d *Document) AddEnumTypeDefinition(definition EnumTypeDefinition) (ref int) {
d.EnumTypeDefinitions = append(d.EnumTypeDefinitions, definition)
return len(d.EnumTypeDefinitions) - 1
Expand Down
4 changes: 3 additions & 1 deletion v2/pkg/astvalidation/operation_rule_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package astvalidation

import (
"bytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/federation"

"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
"github.com/wundergraph/graphql-go-tools/v2/pkg/astimport"
Expand Down Expand Up @@ -286,7 +287,8 @@ func (v *valuesVisitor) valueSatisfiesEnum(value ast.Value, definitionTypeRef in
}
enumValue := v.operation.EnumValueNameBytes(value.Ref)

if !v.definition.EnumTypeDefinitionContainsEnumValue(node.Ref, enumValue) {
hasValue, isInaccessible := v.definition.EnumTypeDefinitionContainsEnumValueWithDirective(node.Ref, enumValue, federation.InaccessibleDirectiveNameBytes)
if !hasValue || isInaccessible {
v.handleNotExistingEnumValueError(value, definitionTypeRef)
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4171,8 +4171,24 @@ func TestGraphQLDataSource(t *testing.T) {
{
OnTypeNames: [][]byte{[]byte("Error")},
Name: []byte("code"),
Value: &resolve.String{
Path: []string{"code"},
Value: &resolve.Enum{
TypeName: "ErrorCode",
Path: []string{"code"},
Values: []string{
"Internal",
"AuthenticationRequired",
"Unauthorized",
"NotFound",
"Conflict",
"UserAlreadyHasPersonalNamespace",
"TeamPlanInPersonalNamespace",
"InvalidName",
"UnableToDeployEnvironment",
"InvalidWunderGraphConfig",
"ApiEnvironmentNamespaceMismatch",
"UnableToUpdateEdgesOnPersonalEnvironment",
},
InaccessibleValues: []string{},
},
},
{
Expand Down Expand Up @@ -8751,9 +8767,16 @@ func TestGraphQLDataSource(t *testing.T) {
},
{
Name: []byte("tier"),
Value: &resolve.String{
Value: &resolve.Enum{
Nullable: true,
TypeName: "custom_Tier",
Path: []string{"tier"},
Values: []string{
"A",
"B",
"C",
},
InaccessibleValues: []string{},
},
},
{
Expand Down
16 changes: 14 additions & 2 deletions v2/pkg/engine/datasource/introspection_datasource/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,20 @@ func TestIntrospectionDataSourcePlanning(t *testing.T) {
},
{
Name: []byte("kind"),
Value: &resolve.String{
Path: []string{"kind"},
Value: &resolve.Enum{
TypeName: "__TypeKind",
Path: []string{"kind"},
Values: []string{
"SCALAR",
"OBJECT",
"INTERFACE",
"UNION",
"ENUM",
"INPUT_OBJECT",
"LIST",
"NON_NULL",
},
InaccessibleValues: []string{},
},
Position: resolve.Position{
Line: 5,
Expand Down
20 changes: 16 additions & 4 deletions v2/pkg/engine/plan/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,22 @@ func (v *Visitor) resolveFieldValue(fieldRef, typeRef int, nullable bool, path [
}
}
case ast.NodeKindEnumTypeDefinition:
return &resolve.String{
Path: path,
Nullable: nullable,
UnescapeResponseJson: unescapeResponseJson,
values := make([]string, 0, len(v.Definition.EnumTypeDefinitions[typeDefinitionNode.Ref].EnumValuesDefinition.Refs))
inaccessibleValues := make([]string, 0)
for _, valueRef := range v.Definition.EnumTypeDefinitions[typeDefinitionNode.Ref].EnumValuesDefinition.Refs {
valueName := v.Definition.EnumValueDefinitionNameString(valueRef)
values = append(values, valueName)

if _, isInaccessible := v.Definition.EnumValueDefinitionDirectiveByName(valueRef, []byte("inaccessible")); isInaccessible {
inaccessibleValues = append(inaccessibleValues, valueName)
}
}
return &resolve.Enum{
Path: path,
Nullable: nullable,
TypeName: typeName,
Values: values,
InaccessibleValues: inaccessibleValues,
}
case ast.NodeKindObjectTypeDefinition, ast.NodeKindInterfaceTypeDefinition, ast.NodeKindUnionTypeDefinition:
object := &resolve.Object{
Expand Down
7 changes: 7 additions & 0 deletions v2/pkg/engine/resolve/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ func (l *Loader) mergeResult(fetchItem *FetchItem, res *result, items []*astjson
if !hasErrors && !l.resolvable.options.ApolloCompatibilitySuppressFetchErrors {
return l.renderErrorsFailedToFetch(fetchItem, res, invalidGraphQLResponseShape)
}

// we have no data but only errors
// skip value completion
if hasErrors && l.resolvable.options.ApolloCompatibilityValueCompletionInExtensions {
l.resolvable.skipValueCompletion = true
}

// no data
return nil
}
Expand Down
1 change: 1 addition & 0 deletions v2/pkg/engine/resolve/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
NodeKindCustom
NodeKindScalar
NodeKindStaticString
NodeKindEnum
)

type Node interface {
Expand Down
57 changes: 57 additions & 0 deletions v2/pkg/engine/resolve/node_enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package resolve

import "slices"

type Enum struct {
Path []string
Nullable bool
Export *FieldExport `json:"export,omitempty"`
TypeName string
Values []string
InaccessibleValues []string
}

func (_ *Enum) NodeKind() NodeKind {
return NodeKindEnum
}

func (e *Enum) Copy() Node {
return &Enum{
Path: e.Path,
Nullable: e.Nullable,
Export: e.Export,
}
}

func (e *Enum) NodePath() []string {
return e.Path
}

func (e *Enum) NodeNullable() bool {
return e.Nullable
}

func (e *Enum) Equals(n Node) bool {
other, ok := n.(*Enum)
if !ok {
return false
}

if e.Nullable != other.Nullable {
return false
}

if !slices.Equal(e.Path, other.Path) {
return false
}

return true
}

func (e *Enum) isValidValue(returnedValue string) bool {
return slices.Contains(e.Values, returnedValue)
}

func (e *Enum) isAccessibleValue(returnedValue string) bool {
return !slices.Contains(e.InaccessibleValues, returnedValue)
}
Loading

0 comments on commit f692fbf

Please sign in to comment.