示例#1
0
func parseInputValueDef(parser *Parser) (interface{}, error) {
	start := parser.Token.Start
	name, err := parseName(parser)
	if err != nil {
		return nil, err
	}
	_, err = expect(parser, lexer.TokenKind[lexer.COLON])
	if err != nil {
		return nil, err
	}
	ttype, err := parseType(parser)
	if err != nil {
		return nil, err
	}
	var defaultValue ast.Value
	if skip(parser, lexer.TokenKind[lexer.EQUALS]) {
		val, err := parseConstValue(parser)
		if err != nil {
			return nil, err
		}
		if val, ok := val.(ast.Value); ok {
			defaultValue = val
		}
	}
	return ast.NewInputValueDefinition(&ast.InputValueDefinition{
		Name:         name,
		Type:         ttype,
		DefaultValue: defaultValue,
		Loc:          loc(parser, start),
	}), 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)
	}
}
func TestSchemaParser_SimpleFieldWithArgWithDefaultValue(t *testing.T) {
	body := `
type Hello {
  world(flag: Boolean = true): String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: testLoc(1, 53),
		Definitions: []ast.Node{
			ast.NewObjectDefinition(&ast.ObjectDefinition{
				Loc: testLoc(1, 53),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(6, 11),
				}),
				Interfaces: []*ast.Named{},
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: testLoc(16, 51),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   testLoc(16, 21),
						}),
						Arguments: []*ast.InputValueDefinition{
							ast.NewInputValueDefinition(&ast.InputValueDefinition{
								Loc: testLoc(22, 42),
								Name: ast.NewName(&ast.Name{
									Value: "flag",
									Loc:   testLoc(22, 26),
								}),
								Type: ast.NewNamed(&ast.Named{
									Loc: testLoc(28, 35),
									Name: ast.NewName(&ast.Name{
										Value: "Boolean",
										Loc:   testLoc(28, 35),
									}),
								}),
								DefaultValue: ast.NewBooleanValue(&ast.BooleanValue{
									Value: true,
									Loc:   testLoc(38, 42),
								}),
							}),
						},
						Type: ast.NewNamed(&ast.Named{
							Loc: testLoc(45, 51),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   testLoc(45, 51),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}
func TestSchemaParser_SimpleInputObject(t *testing.T) {
	body := `
input Hello {
  world: String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: testLoc(1, 32),
		Definitions: []ast.Node{
			ast.NewInputObjectDefinition(&ast.InputObjectDefinition{
				Loc: testLoc(1, 32),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(7, 12),
				}),
				Fields: []*ast.InputValueDefinition{
					ast.NewInputValueDefinition(&ast.InputValueDefinition{
						Loc: testLoc(17, 30),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   testLoc(17, 22),
						}),
						Type: ast.NewNamed(&ast.Named{
							Loc: testLoc(24, 30),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   testLoc(24, 30),
							}),
						}),
						DefaultValue: nil,
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}
func TestSchemaParser_SimpleFieldWithTwoArg(t *testing.T) {
	body := `
type Hello {
  world(argOne: Boolean, argTwo: Int): String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: testLoc(1, 61),
		Definitions: []ast.Node{
			ast.NewObjectDefinition(&ast.ObjectDefinition{
				Loc: testLoc(1, 61),
				Name: ast.NewName(&ast.Name{
					Value: "Hello",
					Loc:   testLoc(6, 11),
				}),
				Interfaces: []*ast.Named{},
				Fields: []*ast.FieldDefinition{
					ast.NewFieldDefinition(&ast.FieldDefinition{
						Loc: testLoc(16, 59),
						Name: ast.NewName(&ast.Name{
							Value: "world",
							Loc:   testLoc(16, 21),
						}),
						Arguments: []*ast.InputValueDefinition{
							ast.NewInputValueDefinition(&ast.InputValueDefinition{
								Loc: testLoc(22, 37),
								Name: ast.NewName(&ast.Name{
									Value: "argOne",
									Loc:   testLoc(22, 28),
								}),
								Type: ast.NewNamed(&ast.Named{
									Loc: testLoc(30, 37),
									Name: ast.NewName(&ast.Name{
										Value: "Boolean",
										Loc:   testLoc(30, 37),
									}),
								}),
								DefaultValue: nil,
							}),
							ast.NewInputValueDefinition(&ast.InputValueDefinition{
								Loc: testLoc(39, 50),
								Name: ast.NewName(&ast.Name{
									Value: "argTwo",
									Loc:   testLoc(39, 45),
								}),
								Type: ast.NewNamed(&ast.Named{
									Loc: testLoc(47, 50),
									Name: ast.NewName(&ast.Name{
										Value: "Int",
										Loc:   testLoc(47, 50),
									}),
								}),
								DefaultValue: nil,
							}),
						},
						Type: ast.NewNamed(&ast.Named{
							Loc: testLoc(53, 59),
							Name: ast.NewName(&ast.Name{
								Value: "String",
								Loc:   testLoc(53, 59),
							}),
						}),
					}),
				},
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}