Example #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: testLoc(1, 38),
		Definitions: []ast.Node{
			ast.NewTypeExtensionDefinition(&ast.TypeExtensionDefinition{
				Loc: testLoc(1, 38),
				Definition: ast.NewObjectDefinition(&ast.ObjectDefinition{
					Loc: testLoc(8, 38),
					Name: ast.NewName(&ast.Name{
						Value: "Hello",
						Loc:   testLoc(13, 18),
					}),
					Interfaces: []*ast.Named{},
					Fields: []*ast.FieldDefinition{
						ast.NewFieldDefinition(&ast.FieldDefinition{
							Loc: testLoc(23, 36),
							Name: ast.NewName(&ast.Name{
								Value: "world",
								Loc:   testLoc(23, 28),
							}),
							Arguments: []*ast.InputValueDefinition{},
							Type: ast.NewNamed(&ast.Named{
								Loc: testLoc(30, 36),
								Name: ast.NewName(&ast.Name{
									Value: "String",
									Loc:   testLoc(30, 36),
								}),
							}),
						}),
					},
				}),
			}),
		},
	})
	if !reflect.DeepEqual(astDoc, expected) {
		t.Fatalf("unexpected document, expected: %v, got: %v", expected, astDoc)
	}
}