Example #1
0
func parseType(parser *Parser) (ast.Type, error) {
	start := parser.Token.Start
	var ttype ast.Type
	if skip(parser, lexer.TokenKind[lexer.BRACKET_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.NewList(&ast.List{
			Type: ttype,
			Loc:  loc(parser, start),
		})
	} else {
		name, err := parseNamed(parser)
		if err != nil {
			return ttype, err
		}
		ttype = name
	}
	if skip(parser, lexer.TokenKind[lexer.BANG]) {
		ttype = ast.NewNonNull(&ast.NonNull{
			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: testLoc(1, 49),
		Definitions: []ast.Node{
			ast.NewObjectDefinition(&ast.ObjectDefinition{
				Loc: testLoc(1, 49),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(6, 11),
				}),
				Interfaces: []*ast.Named{},
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: testLoc(16, 47),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   testLoc(16, 21),
						}),
						Arguments: []*ast.InputValueDefinition{
							ast.NewInputValueDefinition(&ast.InputValueDefinition{
								Loc: testLoc(22, 38),
								Name: ast.NewName(&ast.Name{
									Value: "things",
									Loc:   testLoc(22, 28),
								}),
								Type: ast.NewList(&ast.List{
									Loc: testLoc(30, 38),
									Type: ast.NewNamed(&ast.Named{
										Loc: testLoc(31, 37),
										Name: ast.NewName(&ast.Name{
											Value: "String",
											Loc:   testLoc(31, 37),
										}),
									}),
								}),
								DefaultValue: nil,
							}),
						},
						Type: ast.NewNamed(&ast.Named{
							Loc: testLoc(41, 47),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   testLoc(41, 47),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}