Skip to content

honestbank/hijack

Repository files navigation

Hijack (Hi Jack!)

Hijack lets you hijack graphql requests on the fly and return custom responses based on operation being fired.

Examples:

defer hijack.Start(func(operationName string) (string, error) {
return `{
"data": {
"character": {
"id": "11 (Mock)",
"name": "Albert Einstein (from mock)",
"status": "Alive (mock)"
}
}
}`, nil
}, "rickandmortyapi.com")()
client := graphql.NewClient("https://rickandmortyapi.com/graphql")
request := graphql.NewRequest(`query GetCharacterByID{ character(id:"11"){id, name, status }}`)
response := OperationResult{}
_ = client.Run(context.Background(), request, &response)
fmt.Println(response.Character.ID)
fmt.Println(response.Character.Name)
fmt.Println(response.Character.Status)
//Output:11 (Mock)
// Albert Einstein (from mock)
// Alive (mock)
h := handlers.New()
h.Set("GetCharacterByID", func(operationName string) (string, error) {
return `{
"data": {
"character": {
"id": "11 (Mock)",
"name": "Albert Einstein (from mock)",
"status": "Alive (mock)"
}
}
}`, nil
})
h.Set("BadOperation", func(operationName string) (string, error) {
return "", errors.New("error")
})
defer hijack.Start(h.Handle, "rickandmortyapi.com")()
client := graphql.NewClient("https://rickandmortyapi.com/graphql")
request := graphql.NewRequest(`query GetCharacterByID{ character(id:"11"){id, name, status }}`)
response := OperationResult{}
_ = client.Run(context.Background(), request, &response)
fmt.Println(response.Character.ID)
fmt.Println(response.Character.Name)
fmt.Println(response.Character.Status)
//Output:11 (Mock)
// Albert Einstein (from mock)
// Alive (mock)

Planned Features (PRs welcome)

  • fixtures support
  • handlers with input and operation name