diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d325325607..838ae2d1c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/actions.ts b/src/actions.ts index 5a8773b4766..0cedcef9e0d 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -48,6 +48,7 @@ export interface QueryInitAction { storePreviousVariables: boolean; isRefetch: boolean; isPoll: boolean; + metadata: any; } export function isQueryInitAction(action: ApolloAction): action is QueryInitAction { diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index efe917a53d8..6bebba34653 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -476,6 +476,7 @@ export class QueryManager { forceFetch = false, returnPartialData = false, noFetch = false, + metadata = null, } = options; const { @@ -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, diff --git a/src/core/watchQueryOptions.ts b/src/core/watchQueryOptions.ts index fd2dc73fe06..d14108ed4d3 100644 --- a/src/core/watchQueryOptions.ts +++ b/src/core/watchQueryOptions.ts @@ -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. @@ -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 { diff --git a/src/queries/store.ts b/src/queries/store.ts index 755d9f4baae..83a0abde542 100644 --- a/src/queries/store.ts +++ b/src/queries/store.ts @@ -47,6 +47,7 @@ export type QueryStoreValue = { forceFetch: boolean; returnPartialData: boolean; lastRequestId: number; + metadata: any; } export interface SelectionSetWithRoot { @@ -115,6 +116,7 @@ export function queries( forceFetch: action.forceFetch, returnPartialData: action.returnPartialData, lastRequestId: action.requestId, + metadata: action.metadata, }; return newState; diff --git a/test/QueryManager.ts b/test/QueryManager.ts index 273490e7ba1..5a7b134944e 100644 --- a/test/QueryManager.ts +++ b/test/QueryManager.ts @@ -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 { diff --git a/test/client.ts b/test/client.ts index 46cab813909..e9c48d85469 100644 --- a/test/client.ts +++ b/test/client.ts @@ -496,6 +496,7 @@ describe('client', () => { returnPartialData: false, lastRequestId: 1, previousVariables: null, + metadata: null, }, }, mutations: {}, diff --git a/test/store.ts b/test/store.ts index 17ea2e32d34..cadda3f94a3 100644 --- a/test/store.ts +++ b/test/store.ts @@ -159,6 +159,7 @@ describe('createApolloStore', () => { 'returnPartialData': false, 'stopped': false, 'variables': {}, + 'metadata': null, }, }, mutations: {}, @@ -183,6 +184,7 @@ describe('createApolloStore', () => { storePreviousVariables: false, isPoll: false, isRefetch: false, + metadata: null, }); store.dispatch({