Skip to content

Commit

Permalink
feat: Add basic support for GraphQL (#49)
Browse files Browse the repository at this point in the history
* Add basic support for GraphQL

* Search for gql operations using search query
  • Loading branch information
alexbrazier committed Jun 27, 2021
1 parent 1218f34 commit 6c11bb2
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 35 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: '12'
- run: yarn install --frozen-lockfile
node-version: '14'
- run: yarn bootstrap --frozen-lockfile
- run: yarn lint
- run: yarn typescript
- run: yarn prettier
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: '12'
- run: yarn install --frozen-lockfile
node-version: '14'
- run: yarn bootstrap --frozen-lockfile
- run: yarn test --coverage
2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"test": "jest"
},
"dependencies": {
"@apollo/client": "^3.3.20",
"expo": "~41.0.1",
"graphql": "^15.5.1",
"react": "~17.0.2",
"react-native": "~0.63.4"
},
Expand Down
5 changes: 4 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import NetworkLogger, {
getBackHandler,
startNetworkLogging,
} from 'react-native-network-logger';
import { getRates } from './apolloClient';

export default function App() {
const formData = new FormData();
Expand All @@ -35,12 +36,14 @@ export default function App() {
fetch('https://httpstat.us/500');
// Non JSON response
fetch('https://postman-echo.com/stream/2');

getRates();
// Test requests that fail
// fetch('https://failingrequest');
};

startNetworkLogging({
ignoredHosts: ['192.168.1.28'],
ignoredHosts: ['192.168.1.28', '127.0.0.1'],
maxRequests: 500,
});

Expand Down
21 changes: 21 additions & 0 deletions example/src/apolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

const client = new ApolloClient({
uri: 'https://48p1r2roz4.sse.codesandbox.io',
cache: new InMemoryCache(),
});

export const getRates = async () => {
return client
.query({
query: gql`
query GetRates {
rates(currency: "USD") {
currency
}
}
`,
fetchPolicy: 'network-only',
})
.catch((e: any) => console.log(e.message));
};
98 changes: 96 additions & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
# yarn lockfile v1


"@apollo/client@^3.3.20":
version "3.3.20"
resolved "https://registry.yarnpkg.com/@apollo/client/-/client-3.3.20.tgz#8f0935fa991857e9cf2e73c9bd378ad7ec97caf8"
integrity sha512-hS7UmBwJweudw/J3M0RAcusMHNiRuGqkRH6g91PM2ev8cXScIMdXr/++9jo7wD1nAITMCMF4HQQ3LFaw/Or0Bw==
dependencies:
"@graphql-typed-document-node/core" "^3.0.0"
"@types/zen-observable" "^0.8.0"
"@wry/context" "^0.6.0"
"@wry/equality" "^0.5.0"
fast-json-stable-stringify "^2.0.0"
graphql-tag "^2.12.0"
hoist-non-react-statics "^3.3.2"
optimism "^0.16.0"
prop-types "^15.7.2"
symbol-observable "^4.0.0"
ts-invariant "^0.7.0"
tslib "^1.10.0"
zen-observable "^0.8.14"

"@babel/code-frame@7.10.4", "@babel/code-frame@~7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
Expand Down Expand Up @@ -1417,6 +1436,11 @@
"@babel/code-frame" "7.10.4"
chalk "^4.1.0"

"@graphql-typed-document-node/core@^3.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.0.tgz#0eee6373e11418bfe0b5638f654df7a4ca6a3950"
integrity sha512-wYn6r8zVZyQJ6rQaALBEln5B1pzxb9shV5Ef97kTvn6yVGrqyXVnDqnU24MXnFubR+rZjBY9NWuxX3FB2sTsjg==

"@hapi/address@2.x.x":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.1.4.tgz#5d67ed43f3fd41a69d4b9ff7b56e7c0d1d0a81e5"
Expand Down Expand Up @@ -2267,6 +2291,11 @@
dependencies:
"@types/yargs-parser" "*"

"@types/zen-observable@^0.8.0":
version "0.8.2"
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.2.tgz#808c9fa7e4517274ed555fa158f2de4b4f468e71"
integrity sha512-HrCIVMLjE1MOozVoD86622S7aunluLb2PJdPfb3nYiEtohm8mIB/vyv0Fd37AdeMFrTUQXEunw78YloMA3Qilg==

"@unimodules/core@~7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@unimodules/core/-/core-7.1.0.tgz#69139bcfecbacd23778142b2f463605a131cafb5"
Expand Down Expand Up @@ -2426,13 +2455,34 @@
"@webassemblyjs/wast-parser" "1.9.0"
"@xtuc/long" "4.2.2"

"@wry/context@^0.6.0":
version "0.6.0"
resolved "https://registry.yarnpkg.com/@wry/context/-/context-0.6.0.tgz#f903eceb89d238ef7e8168ed30f4511f92d83e06"
integrity sha512-sAgendOXR8dM7stJw3FusRxFHF/ZinU0lffsA2YTyyIOfic86JX02qlPqPVqJNZJPAxFt+2EE8bvq6ZlS0Kf+Q==
dependencies:
tslib "^2.1.0"

"@wry/equality@^0.1.2":
version "0.1.11"
resolved "https://registry.yarnpkg.com/@wry/equality/-/equality-0.1.11.tgz#35cb156e4a96695aa81a9ecc4d03787bc17f1790"
integrity sha512-mwEVBDUVODlsQQ5dfuLUS5/Tf7jqUKyhKYHmVi4fPB6bDMOfWvUPJmKgS1Z7Za/sOI3vzWt4+O7yCiL/70MogA==
dependencies:
tslib "^1.9.3"

"@wry/equality@^0.5.0":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@wry/equality/-/equality-0.5.1.tgz#b22e4e1674d7bf1439f8ccdccfd6a785f6de68b0"
integrity sha512-FZKbdpbcVcbDxQrKcaBClNsQaMg9nof1RKM7mReJe5DKUzM5u8S7T+PqwNqvib5O2j2xxF1R4p5O3+b6baTrbw==
dependencies:
tslib "^2.1.0"

"@wry/trie@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@wry/trie/-/trie-0.3.0.tgz#3245e74988c4e3033299e479a1bf004430752463"
integrity sha512-Yw1akIogPhAT6XPYsRHlZZIS0tIGmAl9EYXHi2scf7LPKKqdqmow/Hu4kEqP2cJR3EjaU/9L0ZlAjFf3hFxmug==
dependencies:
tslib "^2.1.0"

"@xtuc/ieee754@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@xtuc/ieee754/-/ieee754-1.2.0.tgz#eef014a3145ae477a1cbc00cd1e552336dceb790"
Expand Down Expand Up @@ -6134,6 +6184,13 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.3
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==

graphql-tag@^2.12.0:
version "2.12.5"
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.12.5.tgz#5cff974a67b417747d05c8d9f5f3cb4495d0db8f"
integrity sha512-5xNhP4063d16Pz3HBtKprutsPrmHZi5IdUGOWRxA2B6VF7BIRGOHZ5WQvDmJXZuPcBg7rYwaFxvQYjqkSdR3TQ==
dependencies:
tslib "^2.1.0"

graphql-tools@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/graphql-tools/-/graphql-tools-3.0.0.tgz#ff22ad15315fc268de8639d03936b911d78b9e9b"
Expand All @@ -6152,6 +6209,11 @@ graphql@0.13.2:
dependencies:
iterall "^1.2.1"

graphql@^15.5.1:
version "15.5.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.5.1.tgz#f2f84415d8985e7b84731e7f3536f8bb9d383aad"
integrity sha512-FeTRX67T3LoE3LWAxxOlW2K3Bz+rMYAC18rRguK4wgXaTZMiJwSUwDmPFo3UadAKbzirKIg5Qy+sNJXbpPRnQw==

gzip-size@5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-5.1.1.tgz#cb9bee692f87c0612b232840a873904e4c135274"
Expand Down Expand Up @@ -6306,6 +6368,13 @@ hoek@4.x.x:
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb"
integrity sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA==

hoist-non-react-statics@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"

hosted-git-info@^2.6.0:
version "2.8.9"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
Expand Down Expand Up @@ -8921,6 +8990,14 @@ opn@^5.5.0:
dependencies:
is-wsl "^1.1.0"

optimism@^0.16.0:
version "0.16.1"
resolved "https://registry.yarnpkg.com/optimism/-/optimism-0.16.1.tgz#7c8efc1f3179f18307b887e18c15c5b7133f6e7d"
integrity sha512-64i+Uw3otrndfq5kaoGNoY7pvOhSsjFEN4bdEFh80MWVk/dbgJfMv7VFDeCT8LxNAlEVhQmdVEbfE7X2nWNIIg==
dependencies:
"@wry/context" "^0.6.0"
"@wry/trie" "^0.3.0"

optimize-css-assets-webpack-plugin@^5.0.3:
version "5.0.4"
resolved "https://registry.yarnpkg.com/optimize-css-assets-webpack-plugin/-/optimize-css-assets-webpack-plugin-5.0.4.tgz#85883c6528aaa02e30bbad9908c92926bb52dc90"
Expand Down Expand Up @@ -10158,7 +10235,7 @@ react-error-overlay@^6.0.9:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.9.tgz#3c743010c9359608c375ecd6bc76f35d93995b0a"
integrity sha512-nQTTcUu+ATDbrSD1BZHr5kgSD4oF8OFjxun8uAaL8RwPBacGBNPf/yAuVVdx17N8XNzRDMrZ9XcKZHCjPW+9ew==

react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4:
react-is@^16.12.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
Expand Down Expand Up @@ -11544,6 +11621,11 @@ symbol-observable@^1.0.4:
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==

symbol-observable@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-4.0.0.tgz#5b425f192279e87f2f9b937ac8540d1984b39205"
integrity sha512-b19dMThMV4HVFynSAM1++gBHAbk2Tc/osgLIBZMKsyqh34jb2e8Os7T6ZW/Bt3pJFdBTd2JwAnAAEQV7rSNvcQ==

tapable@^1.0.0, tapable@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
Expand Down Expand Up @@ -11834,6 +11916,13 @@ ts-invariant@^0.4.0:
dependencies:
tslib "^1.9.3"

ts-invariant@^0.7.0:
version "0.7.5"
resolved "https://registry.yarnpkg.com/ts-invariant/-/ts-invariant-0.7.5.tgz#f9658719f9a7737b117d09820d952aacf6263f9c"
integrity sha512-qfVyqTYWEqADMtncLqwpUdMjMSXnsqOeqGtj1LeJNFDjz8oqZ1YxLEp29YCOq65z0LgEiERqQ8ThVjnfibJNpg==
dependencies:
tslib "^2.1.0"

ts-pnp@^1.1.6:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92"
Expand All @@ -11849,6 +11938,11 @@ tslib@^2.0.3:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.2.0.tgz#fb2c475977e35e241311ede2693cee1ec6698f5c"
integrity sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==

tslib@^2.1.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.0.tgz#803b8cdab3e12ba581a4ca41c8839bbb0dacb09e"
integrity sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==

tty-browserify@0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"
Expand Down Expand Up @@ -13007,7 +13101,7 @@ zen-observable-ts@^0.8.6:
tslib "^1.9.3"
zen-observable "^0.8.0"

zen-observable@^0.8.0:
zen-observable@^0.8.0, zen-observable@^0.8.14:
version "0.8.15"
resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15"
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==
2 changes: 1 addition & 1 deletion src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Logger {
) => {
const networkInfo = this.getRequest(index);
if (!networkInfo) return;
Object.assign(networkInfo, update);
networkInfo.update(update);
};

private openCallback = (method: RequestMethod, url: string, xhr: XHR) => {
Expand Down
31 changes: 25 additions & 6 deletions src/NetworkRequestInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class NetworkRequestInfo {
serverError = undefined;
startTime: number = 0;
endTime: number = 0;
gqlOperation?: string;

constructor(id: string, type: string, method: RequestMethod, url: string) {
this.id = id;
Expand Down Expand Up @@ -54,23 +55,41 @@ export default class NetworkRequestInfo {
return parts.filter(Boolean).join(' ');
}

update(values: Partial<NetworkRequestInfo>) {
Object.assign(this, values);
if (values.dataSent) {
const data = this.parseData(values.dataSent);
this.gqlOperation = data?.operationName;
}
}

private escapeQuotes(value: string) {
return value.replace?.(/'/g, `\\'`);
}

private stringifyFormat(data: any) {
private parseData(data: any) {
try {
if (data?._parts?.length) {
return JSON.stringify(fromEntries(data?._parts), null, 2);
return fromEntries(data?._parts);
}
return JSON.stringify(JSON.parse(data), null, 2);
return JSON.parse(data);
} catch (e) {
return `${data}`;
return { data };
}
}

getRequestBody() {
return this.stringifyFormat(this.dataSent);
private stringifyFormat(data: any) {
return JSON.stringify(this.parseData(data), null, 2);
}

getRequestBody(replaceEscaped = false) {
const body = this.stringifyFormat(this.dataSent);

if (replaceEscaped) {
return body.replace(/\\n/g, '\n').replace(/\\"/g, '"');
}

return body;
}

private async parseResponseBlob() {
Expand Down
Loading

0 comments on commit 6c11bb2

Please sign in to comment.