Hijack lets you hijack graphql requests on the fly and return custom responses based on operation being fired.
Lines 23 to 45 in 1de6922
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) |
Lines 49 to 77 in 1de6922
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) |
- fixtures support
- handlers with input and operation name