Skip to content

Commit

Permalink
fix handling multiple default values in input object
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Apr 2, 2024
1 parent 3e48ba6 commit 3e7a171
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
20 changes: 10 additions & 10 deletions lib/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ type Arg struct {

type Field struct {
BaseFileInfo
Name string
Args []*Arg
Type string
Null bool
IsList bool
IsListNull bool
DefaultValue *string
Directives []*Directive
Descriptions *[]string
Comments *[]string
Name string
Args []*Arg
Type string
Null bool
IsList bool
IsListNull bool
DefaultValues *[]string
Directives []*Directive
Descriptions *[]string
Comments *[]string
}

type Scalar struct {
Expand Down
21 changes: 20 additions & 1 deletion lib/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ func (s *Schema) Parse(p *Parser) {
} else {
fd.IsListNull = true
}
if p.lex.peek() == '=' {
p.lex.consumeToken(tokEqual)
defaultValues := []string{}
for p.lex.peek() == '[' {
p.lex.consumeIdent(tokLBracket)
for p.lex.peek() != ']' {
tex, _ := p.lex.consumeIdentInclString(tokNumber)
te := tex.String()
defaultValues = append(defaultValues, te)
if p.lex.peek() == ',' {
p.lex.consumeToken(tokComma)
}
}
p.lex.consumeIdent(tokRBracket)
fd.DefaultValues = &defaultValues
}
}
} else {
fd.IsList = false
fd.IsListNull = false
Expand All @@ -335,7 +352,9 @@ func (s *Schema) Parse(p *Parser) {
p.lex.consumeToken(tokEqual)
tex, _ := p.lex.consumeIdentInclString(tokNumber)
te := tex.String()
fd.DefaultValue = &te
defaultValues := []string{}
defaultValues = append(defaultValues, te)
fd.DefaultValues = &defaultValues
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,17 @@ func (ms *MergedSchema) WriteSchema(s *Schema) string {
if p.IsList && !p.IsListNull {
ms.buf.WriteString("!")
}
if p.DefaultValue != nil {
ms.buf.WriteString(" = " + *p.DefaultValue)
if p.DefaultValues != nil {
if p.IsList {
ms.buf.WriteString(" = ")
ms.buf.WriteString("[")
ms.stitchDefaultValues(p.DefaultValues)
ms.buf.WriteString("]")
} else {
ms.buf.WriteString(" = ")
ms.stitchDefaultValues(p.DefaultValues)
}
}

ms.stitchDirectives(p.Directives)

ms.buf.WriteString("\n")
Expand Down
1 change: 1 addition & 0 deletions test/default_value/generated.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type Query {

input User {
name: String! = "woonki"
nicknames: [String!]! = ["mununki", "arnold"]
}
2 changes: 1 addition & 1 deletion test/object_extension/generated.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Person implements Node @talkable @walkable {
type Person implements Node @walkable @talkable {
id: ID!
createTime: Time!
updateTime: Time!
Expand Down

0 comments on commit 3e7a171

Please sign in to comment.