Exemple #1
0
func parseTypeExtensionDefinition(parser *Parser) (*ast.TypeExtensionDefinition, error) {
	start := parser.Token.Start
	_, err := expectKeyWord(parser, "extend")
	if err != nil {
		return nil, err
	}

	definition, err := parseObjectTypeDefinition(parser)
	if err != nil {
		return nil, err
	}
	return ast.NewTypeExtensionDefinition(&ast.TypeExtensionDefinition{
		Loc:        loc(parser, start),
		Definition: definition,
	}), nil
}
func TestSchemaParser_SimpleExtension(t *testing.T) {

	body := `
extend type Hello {
  world: String
}`
	astDoc := parse(t, body)
	expected := ast.NewDocument(&ast.Document{
		Loc: loc(1, 38),
		Definitions: []ast.Node{
			ast.NewTypeExtensionDefinition(&ast.TypeExtensionDefinition{
				Loc: loc(1, 38),
				Definition: ast.NewObjectTypeDefinition(&ast.ObjectTypeDefinition{
					Loc: loc(8, 38),
					Name: ast.NewName(&ast.Name{
						Value: "Hello",
						Loc:   loc(13, 18),
					}),
					Interfaces: []*ast.NamedType{},
					Fields: []*ast.FieldDefinition{
						ast.NewFieldDefinition(&ast.FieldDefinition{
							Loc: loc(23, 36),
							Name: ast.NewName(&ast.Name{
								Value: "world",
								Loc:   loc(23, 28),
							}),
							Arguments: []*ast.InputValueDefinition{},
							Type: ast.NewNamedType(&ast.NamedType{
								Loc: loc(30, 36),
								Name: ast.NewName(&ast.Name{
									Value: "String",
									Loc:   loc(30, 36),
								}),
							}),
						}),
					},
				}),
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}