예제 #1
0
func parseType(parser *Parser) (ast.Type, error) {
	start := parser.Token.Start
	var ttype ast.Type
	if skip(parser, lexer.TokenKind[lexer.BRACE_L]) {
		t, err := parseType(parser)
		if err != nil {
			return t, err
		}
		ttype = t
		_, err = expect(parser, lexer.TokenKind[lexer.BRACKET_R])
		if err != nil {
			return ttype, err
		}
		ttype = ast.NewListType(&ast.ListType{
			Type: ttype,
			Loc:  loc(parser, start),
		})
	} else {
		name, err := parseNamedType(parser)
		if err != nil {
			return ttype, err
		}
		ttype = name
	}
	if skip(parser, lexer.TokenKind[lexer.BANG]) {
		ttype = ast.NewNonNullType(&ast.NonNullType{
			Type: ttype,
			Loc:  loc(parser, start),
		})
		return ttype, nil
	}
	return ttype, nil
}
func TestSchemaParser_SimpleFieldWithListArg(t *testing.T) {
	body := `
type Hello {
  world(things: [String]): String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(1, 49),
		Definitions: []ast.Node{
			ast.NewObjectTypeDefinition(&ast.ObjectTypeDefinition{
				Loc: loc(1, 49),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   loc(6, 11),
				}),
				Interfaces: []*ast.NamedType{},
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: loc(16, 47),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   loc(16, 21),
						}),
						Arguments: []*ast.InputValueDefinition{
							ast.NewInputValueDefinition(&ast.InputValueDefinition{
								Loc: loc(22, 38),
								Name: ast.NewName(&ast.Name{
									Value: "things",
									Loc:   loc(22, 28),
								}),
								Type: ast.NewListType(&ast.ListType{
									Loc: loc(30, 38),
									Type: ast.NewNamedType(&ast.NamedType{
										Loc: loc(31, 37),
										Name: ast.NewName(&ast.Name{
											Value: "String",
											Loc:   loc(31, 37),
										}),
									}),
								}),
								DefaultValue: nil,
							}),
						},
						Type: ast.NewNamedType(&ast.NamedType{
							Loc: loc(41, 47),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   loc(41, 47),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}