Skip to content

Commit

Permalink
fix 1865
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto committed Jan 14, 2025
1 parent b1fa3c4 commit db4cdbf
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 7 deletions.

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

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

7 changes: 7 additions & 0 deletions examples/simple/src/graphql/generated/schema.gql

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

2 changes: 2 additions & 0 deletions examples/simple/src/graphql/generated/schema.ts

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

56 changes: 56 additions & 0 deletions examples/simple/src/graphql/mutations/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { User } from "../../ent";
import {
UserAuthInput,
UserAuthJWTInput,
UserAuthJWTLogin,
UserAuthJWTPayload,
UserAuthPayload,
} from "./auth_types";
Expand Down Expand Up @@ -125,6 +126,61 @@ export class AuthResolver {
return new UserAuthJWTPayload(encodeGQLID(user), token);
}

@gqlMutation({
class: "AuthResolver",
name: "userAuthJWT2",
type: "UserAuthJWTLogin",
description: "authenticate a user with JWT",
async: true,
args: [
gqlContextType(),
{
name: "emailAddress",
type: "String",
},
{
name: "password",
type: "String",
},
],
})
async userAuthJWT2(
context: RequestContext,
emailAddress: string,
password: string,
): Promise<UserAuthJWTLogin> {
const [viewer, token] = await useAndVerifyAuthJWT(
context,
async () => {
const data = await User.validateEmailPassword(emailAddress, password);
return data?.id;
},
{
secretOrKey: "secret",
signInOptions: {
algorithm: "HS256",
audience: "https://foo.com/website",
issuer: "https://foo.com",
expiresIn: "1h",
},
},
User.loaderOptions(),
// don't store this in session since we're using JWT here
{
session: false,
},
);
if (!viewer) {
throw new Error("not the right credentials");
}
const user = await viewer?.viewer();
if (!user) {
throw new Error("not the right credentials");
}

return new UserAuthJWTLogin(encodeGQLID(user), token);
}

@gqlMutation({
class: "AuthResolver",
name: "phoneAvailable",
Expand Down
20 changes: 20 additions & 0 deletions examples/simple/src/graphql/mutations/auth_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,23 @@ export class UserAuthJWTPayload {
this.viewerID = viewerID;
}
}

@gqlObjectType()
export class UserAuthJWTLogin {
@gqlField({
class: "UserAuthJWTLogin",
type: GraphQLString,
})
token: string;

@gqlField({
class: "UserAuthJWTLogin",
type: GraphQLID,
})
viewerID: ID;

constructor(viewerID: ID, token: string) {
this.token = token;
this.viewerID = viewerID;
}
}
15 changes: 8 additions & 7 deletions internal/graphql/custom_ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ type fieldConfigBuilder interface {
getArg() string
getName() string
getResolveMethodArg() string
getTypeImports(processor *codegen.Processor, s *gqlSchema) []*tsimport.ImportPath
getTypeImports(processor *codegen.Processor, cd *CustomData, s *gqlSchema) []*tsimport.ImportPath
getArgs(s *gqlSchema) []*fieldConfigArg
getReturnTypeHint() string
getArgMap(cd *CustomData) map[string]*CustomObject
Expand Down Expand Up @@ -305,7 +305,7 @@ func (mfcg *mutationFieldConfigBuilder) getResolveMethodArg() string {
return mfcg.field.getResolveMethodArg()
}

func (mfcg *mutationFieldConfigBuilder) getTypeImports(processor *codegen.Processor, s *gqlSchema) []*tsimport.ImportPath {
func (mfcg *mutationFieldConfigBuilder) getTypeImports(processor *codegen.Processor, cd *CustomData, s *gqlSchema) []*tsimport.ImportPath {
if len(mfcg.field.Results) != 1 {
panic(fmt.Errorf("invalid number of results for custom field %s", mfcg.field.FunctionName))
}
Expand All @@ -322,10 +322,11 @@ func (mfcg *mutationFieldConfigBuilder) getTypeImports(processor *codegen.Proces
if imp != nil {
ret = append(ret, imp)
} else {

ret = append(ret, &tsimport.ImportPath{
// TODO we should pass this in instead of automatically doing this
Import: names.ToClassType(mfcg.field.GraphQLName, "PayloadType"),
// local import file which exists in the same file
// i.e. UserAuthPayload -> UserAuthPayloadType
// UserAuthLogin -> UserAuthLoginType
Import: names.ToClassType(r.Type, "Type"),
ImportPath: "",
})
}
Expand Down Expand Up @@ -396,7 +397,7 @@ func (qfcg *queryFieldConfigBuilder) getResolveMethodArg() string {
return qfcg.field.getResolveMethodArg()
}

func (qfcg *queryFieldConfigBuilder) getTypeImports(processor *codegen.Processor, s *gqlSchema) []*tsimport.ImportPath {
func (qfcg *queryFieldConfigBuilder) getTypeImports(processor *codegen.Processor, cd *CustomData, s *gqlSchema) []*tsimport.ImportPath {
if len(qfcg.field.Results) != 1 {
panic("invalid number of results for custom field")
}
Expand Down Expand Up @@ -647,7 +648,7 @@ func buildFieldConfigFrom(builder fieldConfigBuilder, processor *codegen.Process
Description: field.Description,
Arg: builder.getArg(),
ResolveMethodArg: builder.getResolveMethodArg(),
TypeImports: builder.getTypeImports(processor, s),
TypeImports: builder.getTypeImports(processor, cd, s),
ArgImports: argImports,
// reserve and use them. no questions asked
ReserveAndUseImports: field.ExtraImports,
Expand Down

0 comments on commit db4cdbf

Please sign in to comment.