Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support to optional function errorsCheck and parseAndHandleHttpResponse #5

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"react-apollo": "^2"
},
"devDependencies": {
"graphql": "^14",
"graphql-tag": "^2",
"react-apollo": "^2",
"jsdoc-md": "^1.7.0"
}
}
72 changes: 61 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import get from "lodash/get";
import omit from "lodash/omit";
import { ApolloLink, Observable } from "apollo-link";
import {
selectURI,
selectHttpOptionsAndBody,
fallbackHttpConfig,
serializeFetchParameter,
createSignalIfSupported,
parseAndCheckHttpResponse
fallbackHttpConfig,
parseAndCheckHttpResponse,
selectHttpOptionsAndBody,
selectURI,
serializeFetchParameter
} from "apollo-link-http-common";

import { extractFiles } from "extract-files";
import get from "lodash/get";
import omit from "lodash/omit";

interface Options {
uri?: string;
Expand All @@ -20,12 +21,25 @@ interface Options {
includeExtensions?: any;
}

// function to check and display errors
function checkAndDisplayErrors(result) {
/**
* check and display errors
* @param result
*/
function debugErrors(result) {
if (result.errors && result.errors.length > 0) {
console.group("GraphQL Errors");
result.errors.map(console.log);
console.groupEnd();
}
return result;
}

/**
* checks for errors and throws if they are available.
* @param result
*/
function errorsCheck(result) {
if (result.errors && result.errors.length > 0) {
throw result;
}
return result;
Expand Down Expand Up @@ -175,8 +189,44 @@ const reduxOfflineApolloLink = (
operation.setContext({ response });
return response;
})
.then(parseAndCheckHttpResponse(operation))
.then(checkAndDisplayErrors)
.then(response => {
if (
linkFetchOptions.parseAndHandleHttpResponse &&
typeof linkFetchOptions.parseAndHandleHttpResponse === "function"
) {
return response
.text()
.then(bodyText => {
try {
return JSON.parse(bodyText);
} catch (err) {
const parseError = err;
parseError.name = "ServerParseError";
parseError.response = response;
parseError.statusCode = response.status;
parseError.bodyText = bodyText;
return Promise.reject(parseError);
}
})
.then(result => {
return linkFetchOptions.parseAndHandleHttpResponse(
operation,
result
);
});
}
return parseAndCheckHttpResponse(operation)(response);
})
.then(debugErrors)
.then(result => {
if (
linkFetchOptions.errorsCheck &&
typeof linkFetchOptions.errorsCheck === "function"
) {
return linkFetchOptions.errorsCheck(result);
}
return errorsCheck(result);
})
.then(result => {
if (
linkFetchOptions.payloadFormatter &&
Expand Down
Loading