Skip to content

Bug with use_function_syntax_for_execution_context and @goEnum #3605

@adamgonen

Description

@adamgonen

Using the testserver in the repo usefunctionsyntaxforexecutioncontext, with use_function_syntax_for_execution_context: true and utilizing the @goEnum directive causes function declarations to be redeclared by variable declarations for the enum mappings.

Changing test.graphql to (adding the @goModel and @goEnum to Role:

# Custom directive to log field access (limited to fields, not input fields)
directive @log(message: String) on FIELD_DEFINITION
directive @goModel(
    model: String
    models: [String!]
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
directive @goEnum(value: String) on ENUM_VALUE

# Custom scalar for Date
scalar Date

# Enum for user roles
enum Role @goModel(model: "usefunctionsyntaxforexecutioncontext.RoleModel") {
    ADMIN @goEnum(value: "github.com/99designs/gqlgen/codegen/testserver/usefunctionsyntaxforexecutioncontext.RoleModelAdmin")
    USER @goEnum(value: "github.com/99designs/gqlgen/codegen/testserver/usefunctionsyntaxforexecutioncontext.RoleModelUser")
    GUEST @goEnum(value: "github.com/99designs/gqlgen/codegen/testserver/usefunctionsyntaxforexecutioncontext.RoleModelGuest")
}

# Interface representing an Entity with common fields
interface Entity {
    id: ID!
    createdAt: Date
}

# Type representing a user, implementing the Entity interface
type User implements Entity {
  id: ID!
  name: String!
  email: String!
  age: Int
  role: Role!
  createdAt: Date
}

# Root Query type
type Query {
    # Fetch a user by ID
    getUser(id: ID!): User @log(message: "Fetching a user")
}

With models.go

package usefunctionsyntaxforexecutioncontext

type RoleModel string

var (
	RoleModelAdmin RoleModel = "admin"
	RoleModelUser  RoleModel = "user"
	RoleModelGuest RoleModel = "guest"
)

In generated.go:

func unmarshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel(ctx context.Context, ec *executionContext, v any) (RoleModel, error) {
	tmp, err := graphql.UnmarshalString(v)
	res := unmarshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel[tmp]
	return res, graphql.ErrorOnPath(ctx, err)
}

func marshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel(ctx context.Context, ec *executionContext, sel ast.SelectionSet, v RoleModel) graphql.Marshaler {
	res := graphql.MarshalString(marshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel[v])
	if res == graphql.Null {
		if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
			ec.Errorf(ctx, "the requested element is null which the schema does not allow")
		}
	}
	return res
}

var (
	unmarshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel = map[string]RoleModel{
		"ADMIN": RoleModelAdmin,
		"USER":  RoleModelUser,
		"GUEST": RoleModelGuest,
	}
	marshalNRole2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚋusefunctionsyntaxforexecutioncontextᚐRoleModel = map[RoleModel]string{
		RoleModelAdmin: "ADMIN",
		RoleModelUser:  "USER",
		RoleModelGuest: "GUEST",
	}
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions