From 1db0be1fdb6c07faedce9ffeb3574a683693c0fd Mon Sep 17 00:00:00 2001 From: woonki Date: Thu, 5 Oct 2023 02:41:32 +0900 Subject: [PATCH] Fix parse error when argument name has input (#39) --- .gitignore | 2 +- Makefile | 16 ++++++++++++++++ lib/lex.go | 13 +++++++++++-- lib/parse.go | 2 +- lib/parse_test.go | 8 ++++++++ test/arg_input/generated.graphql | 17 +++++++++++++++++ .../{ => arg_input/schema}/CreateLog-ko.graphql | 0 .../{ => arg_input/schema}/CreateLog-ok.graphql | 0 test/{ => basic}/generated.graphql | 9 --------- test/{ => basic/schema}/CheckIfExists.graphql | 0 test/{ => basic/schema}/Directives.graphql | 0 test/{ => basic/schema}/GetMyProfile.graphql | 0 12 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 Makefile create mode 100644 test/arg_input/generated.graphql rename test/{ => arg_input/schema}/CreateLog-ko.graphql (100%) rename test/{ => arg_input/schema}/CreateLog-ok.graphql (100%) rename test/{ => basic}/generated.graphql (87%) rename test/{ => basic/schema}/CheckIfExists.graphql (100%) rename test/{ => basic/schema}/Directives.graphql (100%) rename test/{ => basic/schema}/GetMyProfile.graphql (100%) diff --git a/.gitignore b/.gitignore index f0cd22c..118b865 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ gqlmerge .idea demo/demo.yml -!test/*.graphql +!test/**/*.graphql diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2b35530 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +SHELL = /bin/bash + +.PHONY: all build test + +all: build test + +build: + go build + +test: build + @for dir in $(shell find test -type d -name schema); do \ + basedir=`dirname $$dir`; \ + output="$$basedir/generated.graphql"; \ + echo "Merging $$dir into $$output..."; \ + ./gqlmerge $$dir $$output; \ + done diff --git a/lib/lex.go b/lib/lex.go index 7114f56..415e083 100644 --- a/lib/lex.go +++ b/lib/lex.go @@ -460,7 +460,7 @@ func (l *lexer) consumeToken(expected tokenType) { l.skipSpace() } -func (l *lexer) consumeIdent() (*token, *[]string) { +func (l *lexer) consumeIdent(includings ...tokenType) (*token, *[]string) { comments := []string{} for { tok := l.next() @@ -468,7 +468,16 @@ func (l *lexer) consumeIdent() (*token, *[]string) { comments = append(comments, tok.String()) continue } - if tok.typ != tokIdent { + + 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() diff --git a/lib/parse.go b/lib/parse.go index 5f73144..7731913 100644 --- a/lib/parse.go +++ b/lib/parse.go @@ -40,7 +40,7 @@ func (p *Parser) parseArgs() []*Arg { p.lex.consumeToken(tokLParen) for p.lex.peek() != ')' { arg := Arg{} - name, comments := p.lex.consumeIdent() + name, comments := p.lex.consumeIdent(tokInput, tokType) arg.Name = name.String() arg.Descriptions = comments p.lex.consumeToken(tokColon) diff --git a/lib/parse_test.go b/lib/parse_test.go index 73a9f08..8a62952 100644 --- a/lib/parse_test.go +++ b/lib/parse_test.go @@ -49,6 +49,14 @@ func TestParsing(t *testing.T) { scalar DateTime # TEST union Response = Success | Failure + + input CreateLogInput { + text: String! + } + + type Mutation { + createLogKo(input: CreateLogInput!): String! + } ` s := Schema{} diff --git a/test/arg_input/generated.graphql b/test/arg_input/generated.graphql new file mode 100644 index 0000000..eb4764c --- /dev/null +++ b/test/arg_input/generated.graphql @@ -0,0 +1,17 @@ +schema { + query: Query + mutation: Mutation + subscription: Subscription +} + +type Mutation { + createLogKo(input: CreateLogInput!): String! + createLogOk(inputLog: CreateLogInput!): String! +} + + + + +input CreateLogInput { + text: String! +} diff --git a/test/CreateLog-ko.graphql b/test/arg_input/schema/CreateLog-ko.graphql similarity index 100% rename from test/CreateLog-ko.graphql rename to test/arg_input/schema/CreateLog-ko.graphql diff --git a/test/CreateLog-ok.graphql b/test/arg_input/schema/CreateLog-ok.graphql similarity index 100% rename from test/CreateLog-ok.graphql rename to test/arg_input/schema/CreateLog-ok.graphql diff --git a/test/generated.graphql b/test/basic/generated.graphql similarity index 87% rename from test/generated.graphql rename to test/basic/generated.graphql index d8e26b9..4681131 100644 --- a/test/generated.graphql +++ b/test/basic/generated.graphql @@ -32,11 +32,6 @@ type User implements Node & Owner { avatar: Url } -type Mutation { - createLogKo(inputx: CreateLogInput!): String! - createLogOk(inputLog: CreateLogInput!): String! -} - type UserResponse { ok: Boolean! error: String @@ -76,7 +71,3 @@ input CreateChatInput { " post_id " postId: ID! } - -input CreateLogInput { - text: String! -} diff --git a/test/CheckIfExists.graphql b/test/basic/schema/CheckIfExists.graphql similarity index 100% rename from test/CheckIfExists.graphql rename to test/basic/schema/CheckIfExists.graphql diff --git a/test/Directives.graphql b/test/basic/schema/Directives.graphql similarity index 100% rename from test/Directives.graphql rename to test/basic/schema/Directives.graphql diff --git a/test/GetMyProfile.graphql b/test/basic/schema/GetMyProfile.graphql similarity index 100% rename from test/GetMyProfile.graphql rename to test/basic/schema/GetMyProfile.graphql