Skip to content

Commit

Permalink
Add "metadata" argument to watchQuery
Browse files Browse the repository at this point in the history
Libraries such as react-apollo can pass metadata with their queries,
which can be read by tools such as the Apollo Chrome DevTools.
  • Loading branch information
glasser committed Dec 8, 2016
1 parent e7d8d86 commit 75ceaad
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- Calls to watchQuery can include metadata, for use with debugging. [PR #1010](https://github.com/apollostack/apollo-client/pull/1010)

### 0.5.12
- Errors thrown in afterwares bubble up [PR #982](https://github.com/apollostack/apollo-client/pull/982)
Expand Down
1 change: 1 addition & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface QueryInitAction {
storePreviousVariables: boolean;
isRefetch: boolean;
isPoll: boolean;
metadata: any;
}

export function isQueryInitAction(action: ApolloAction): action is QueryInitAction {
Expand Down
2 changes: 2 additions & 0 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ export class QueryManager {
forceFetch = false,
returnPartialData = false,
noFetch = false,
metadata = null,
} = options;

const {
Expand Down Expand Up @@ -523,6 +524,7 @@ export class QueryManager {
storePreviousVariables: shouldFetch,
isPoll: fetchType === FetchType.poll,
isRefetch: fetchType === FetchType.refetch,
metadata,
});

// If there is no part of the query we need to fetch from the server (or,
Expand Down
12 changes: 12 additions & 0 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export interface WatchQueryOptions extends ModifiableWatchQueryOptions {
*/
// TODO REFACTOR: rename this to document. Didn't do it yet because it's in a lot of tests.
query: Document;

/**
* Arbitrary metadata stored in Redux with this query. Designed for debugging,
* developer tools, etc.
*/
metadata?: any;
}

// This interface is deprecated because we no longer pass around fragments separately in the core.
Expand All @@ -80,6 +86,12 @@ export interface DeprecatedWatchQueryOptions extends ModifiableWatchQueryOptions
* referenced from the query document.
*/
fragments?: FragmentDefinition[];

/**
* Arbitrary metadata stored in Redux with this query. Designed for debugging,
* developer tools, etc.
*/
metadata?: any;
}

export interface FetchMoreQueryOptions {
Expand Down
2 changes: 2 additions & 0 deletions src/queries/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type QueryStoreValue = {
forceFetch: boolean;
returnPartialData: boolean;
lastRequestId: number;
metadata: any;
}

export interface SelectionSetWithRoot {
Expand Down Expand Up @@ -115,6 +116,7 @@ export function queries(
forceFetch: action.forceFetch,
returnPartialData: action.returnPartialData,
lastRequestId: action.requestId,
metadata: action.metadata,
};

return newState;
Expand Down
37 changes: 37 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,43 @@ describe('QueryManager', () => {
]);
});

it('should store metadata with watched queries', () => {
const query = gql`
query {
author {
firstName
lastName
}
}`;

const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const queryManager = mockQueryManager(
{
request: { query },
result: { data },
}
);

const observable = queryManager.watchQuery({
query,
metadata: { foo: 'bar' },
});
return observableToPromise({ observable },
(result) => {
assert.deepEqual(result.data, data);
assert.deepEqual(
queryManager.getApolloState().queries[observable.queryId].metadata,
{ foo: 'bar' }
);
}
);
});

it('should error when we orphan a real-id node in the store with a real-id node', () => {
const query1 = gql`
query {
Expand Down
1 change: 1 addition & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ describe('client', () => {
returnPartialData: false,
lastRequestId: 1,
previousVariables: null,
metadata: null,
},
},
mutations: {},
Expand Down
2 changes: 2 additions & 0 deletions test/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe('createApolloStore', () => {
'returnPartialData': false,
'stopped': false,
'variables': {},
'metadata': null,
},
},
mutations: {},
Expand All @@ -183,6 +184,7 @@ describe('createApolloStore', () => {
storePreviousVariables: false,
isPoll: false,
isRefetch: false,
metadata: null,
});

store.dispatch({
Expand Down

0 comments on commit 75ceaad

Please sign in to comment.