-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Description
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",
}
)
jacobmischka
Metadata
Metadata
Assignees
Labels
No labels