Skip to content

Commit

Permalink
fix parse and write the default value in arg definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Apr 2, 2024
1 parent a52369b commit f83c8a2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Type struct {
type Arg struct {
Name string
Type string
TypeExt *string // in case of enum e.g. admin(role: Role = ADMIN): Admin!
TypeExt *string // in case of default values e.g. admin(role: Role = ADMIN): Admin!
Null bool
IsList bool
IsListNull bool
Expand Down
35 changes: 35 additions & 0 deletions lib/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,38 @@ func (l *lexer) consumeIdent(includings ...tokenType) (*token, *[]string) {
return tok, &comments
}
}

/*
The `consumeIdent` function treats tokString as a comment.
If you want to scan the tokString for ident, you can use the consumeIdentInclString function.
e.g. scan "woonki" for ident here
test(name: String = "woonki"): Bool
*/
func (l *lexer) consumeIdentInclString(includings ...tokenType) (*token, *[]string) {
comments := []string{}
includings = append(includings, tokString)

for {
tok := l.next()
if tok.typ == tokSingleLineComment || tok.typ == tokBlockString {
comments = append(comments, tok.String())
continue
}

isIncluded := false
for _, incl := range includings {
if tok.typ == incl {
isIncluded = true
break
}
}

if tok.typ != tokIdent && !isIncluded {
errorf(`%s:%d:%d: unexpected "%s"`, l.filename, l.line, l.col, tok.String())
}
l.skipSpace()
return tok, &comments
}
}
14 changes: 7 additions & 7 deletions lib/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ func (p *Parser) parseArgs() []*Arg {
typ, _ := p.lex.consumeIdent()
arg.Type = typ.String()

if p.lex.peek() == '=' {
p.lex.consumeToken(tokEqual)
tex, _ := p.lex.consumeIdent()
te := tex.String()
arg.TypeExt = &te
}

if p.lex.peek() == '!' {
arg.Null = false
p.lex.consumeToken(tokBang)
} else {
arg.Null = true
}

if p.lex.peek() == '=' {
p.lex.consumeToken(tokEqual)
tex, _ := p.lex.consumeIdentInclString(tokNumber)
te := tex.String()
arg.TypeExt = &te
}
}
arg.Directives = p.parseDirectives()

Expand Down
7 changes: 4 additions & 3 deletions lib/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,13 @@ func (ms *MergedSchema) stitchArgument(a *Arg, l int, i int) {
}
} else {
ms.buf.WriteString(a.Type)
if a.TypeExt != nil {
ms.buf.WriteString(" = " + *a.TypeExt)
}
if !a.Null {
ms.buf.WriteString("!")
}
if a.TypeExt != nil {
ms.buf.WriteString(" = " + *a.TypeExt)
}
ms.stitchDirectives(a.Directives)
}

if l <= 2 && i != l-1 {
Expand Down
12 changes: 12 additions & 0 deletions test/default_value/generated.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Query {
test(X: Int = 20): Int
test(X: Int! = 20): Int
test1(X: Int = ADMIN): Int
test2(X: String! = "user"): Int
test3(X: String = "user" @deprecated): Int
test3(X: String = "user" @deprecated, Y: String! = "operator" @unique): Int
}




5 changes: 4 additions & 1 deletion test/default_value/schema/a.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type Query {
test(X: Int = 20): Int
test(X: Int! = 20): Int
test1(X: Int = ADMIN): Int
test2(X: String = "user"): Int
test2(X: String! = "user"): Int
test3(X: String = "user" @deprecated): Int
test3(X: String = "user" @deprecated, Y: String! = "operator" @unique): Int
}

0 comments on commit f83c8a2

Please sign in to comment.