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

(graphcache) - Clean error stacks #751

Merged
merged 6 commits into from
Apr 27, 2020
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
5 changes: 5 additions & 0 deletions .changeset/spotty-maps-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Improve warning and error console output in development by cleaning up the GraphQL trace stack.
2 changes: 2 additions & 0 deletions exchanges/graphcache/src/helpers/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const cache = new Set<string>();

export const currentDebugStack: string[] = [];

export const popDebugNode = () => currentDebugStack.pop();

export const pushDebugNode = (typename: void | string, node: DebugNode) => {
let identifier = '';
if (node.kind === Kind.INLINE_FRAGMENT) {
Expand Down
44 changes: 30 additions & 14 deletions exchanges/graphcache/src/operations/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { write } from './write';
import { query } from './query';

const TODO_QUERY = gql`
query todos {
query Todos {
todos {
id
text
Expand All @@ -22,17 +22,12 @@ const TODO_QUERY = gql`

describe('Query', () => {
let schema, store, alteredRoot;
const spy: { console?: any } = {};

beforeAll(() => {
schema = require('../test-utils/simple_schema.json');
alteredRoot = require('../test-utils/altered_root_schema.json');
});

afterEach(() => {
spy.console.mockRestore();
});

beforeEach(() => {
store = new Store({ schema });
write(
Expand All @@ -46,7 +41,8 @@ describe('Query', () => {
],
}
);
spy.console = jest.spyOn(console, 'warn');

jest.resetAllMocks();
});

it('test partial results', () => {
Expand Down Expand Up @@ -75,35 +71,55 @@ describe('Query', () => {

it('should warn once for invalid fields on an entity', () => {
const INVALID_TODO_QUERY = gql`
query {
query InvalidTodo {
todos {
id
text
incomplete
}
}
`;

query(store, { query: INVALID_TODO_QUERY });
expect(console.warn).toHaveBeenCalledTimes(1);
expect((console.warn as any).mock.calls[0][0]).toMatch(
/Caused At: "InvalidTodo" query/
);

query(store, { query: INVALID_TODO_QUERY });
expect(console.warn).toHaveBeenCalledTimes(1);

expect((console.warn as any).mock.calls[0][0]).toMatch(/incomplete/);
});

it('should warn once for invalid sub-entities on an entity', () => {
it('should warn once for invalid sub-entities on an entity at the right stack', () => {
const INVALID_TODO_QUERY = gql`
query {
query InvalidTodo {
todos {
...ValidTodo
...InvalidFields
}
}

fragment ValidTodo on Todo {
id
text
}

fragment InvalidFields on Todo {
id
writer {
id
text
writer {
id
}
}
}
`;

query(store, { query: INVALID_TODO_QUERY });
expect(console.warn).toHaveBeenCalledTimes(1);
expect((console.warn as any).mock.calls[0][0]).toMatch(
/Caused At: "InvalidTodo" query, "InvalidFields" Fragment/
);

query(store, { query: INVALID_TODO_QUERY });
expect(console.warn).toHaveBeenCalledTimes(1);
expect((console.warn as any).mock.calls[0][0]).toMatch(/writer/);
Expand Down
18 changes: 14 additions & 4 deletions exchanges/graphcache/src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from '../store';

import * as InMemoryData from '../store/data';
import { warn, pushDebugNode } from '../helpers/help';
import { warn, pushDebugNode, popDebugNode } from '../helpers/help';

import {
Context,
Expand Down Expand Up @@ -94,6 +94,10 @@ export const read = (
? readRoot(ctx, rootKey, rootSelect, data)
: readSelection(ctx, rootKey, rootSelect, data);

if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}

return {
dependencies: getCurrentDependencies(),
partial: data === undefined ? false : ctx.partial,
Expand Down Expand Up @@ -209,9 +213,15 @@ export const readFragment = (
entityKey
);

return (
readSelection(ctx, entityKey, getSelectionSet(fragment), {} as Data) || null
);
const result =
readSelection(ctx, entityKey, getSelectionSet(fragment), {} as Data) ||
null;

if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}

return result;
};

const readSelection = (
Expand Down
5 changes: 4 additions & 1 deletion exchanges/graphcache/src/operations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SelectionSet,
isFieldNode,
} from '../ast';
import { warn, pushDebugNode } from '../helpers/help';
import { warn, pushDebugNode, popDebugNode } from '../helpers/help';

import { hasField } from '../store/data';
import { Store, keyOfField } from '../store';
Expand Down Expand Up @@ -118,6 +118,9 @@ export class SelectionIterator {
if (index >= select.length) {
this.indexStack.pop();
this.selectionStack.pop();
if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}
continue;
} else {
const node = select[index];
Expand Down
17 changes: 16 additions & 1 deletion exchanges/graphcache/src/operations/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getFieldAlias,
} from '../ast';

import { invariant, warn, pushDebugNode } from '../helpers/help';
import { invariant, warn, pushDebugNode, popDebugNode } from '../helpers/help';

import { NullArray, Variables, Data, Link, OperationRequest } from '../types';

Expand Down Expand Up @@ -75,6 +75,11 @@ export const startWrite = (
}

writeSelection(ctx, operationName, getSelectionSet(operation), data);

if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}

return result;
};

Expand Down Expand Up @@ -113,6 +118,11 @@ export const writeOptimistic = (
);

writeSelection(ctx, operationName, getSelectionSet(operation), result.data!);

if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}

clearDataState();
return result;
};
Expand Down Expand Up @@ -160,6 +170,10 @@ export const writeFragment = (
);

writeSelection(ctx, entityKey, getSelectionSet(fragment), writeData);

if (process.env.NODE_ENV !== 'production') {
popDebugNode();
}
};

const writeSelection = (
Expand Down Expand Up @@ -221,6 +235,7 @@ const writeSelection = (

if (ctx.optimistic && isRoot) {
const resolver = ctx.store.optimisticMutations[fieldName];

if (!resolver) continue;
// We have to update the context to reflect up-to-date ResolveInfo
updateContext(ctx, typename, typename, fieldKey, fieldName);
Expand Down