From 78b1cb680a800013000fb5a96c11eae8d06cfcec Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 12:14:18 +0800 Subject: [PATCH 01/16] remove fragment support from watchQuery --- src/ApolloClient.ts | 29 +--------- test/client.ts | 130 -------------------------------------------- test/optimistic.ts | 43 --------------- 3 files changed, 1 insertion(+), 201 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 5c9d02413d7..02d61482fb1 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -275,34 +275,7 @@ export default class ApolloClient { } as DeprecatedWatchQueryOptions; } - if (options.fragments && !haveWarnedWatchQuery && process.env.NODE_ENV !== 'production') { - console.warn( - '"fragments" option is deprecated and will be removed in the upcoming versions, ' + - 'please refer to the documentation for how to define fragments: ' + - 'http://dev.apollodata.com/react/fragments.html.', - ); - /* istanbul ignore if */ - if (process.env.NODE_ENV !== 'test') { - // When running tests, we want to print the warning every time - haveWarnedWatchQuery = true; - } - } - - // Register each of the fragments present in the query document. The point - // is to prevent fragment name collisions with fragments that are in the query - // document itself. - createFragment(options.query, undefined, true); - - // We add the fragments to the document to pass only the document around internally. - const fullDocument = addFragmentsToDocument(options.query, options.fragments); - - const realOptions = { - ...options, - query: fullDocument, - }; - delete realOptions.fragments; - - return this.queryManager.watchQuery(realOptions); + return this.queryManager.watchQuery(options); }; /** diff --git a/test/client.ts b/test/client.ts index a7011b4f89c..cd6c20793ff 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1483,38 +1483,6 @@ it('should not let errors in observer.next reach the store', (done) => { disableFragmentWarnings(); }); - it('should issue a warning if we try to watchQuery with a conflicting fragment name', () => { - enableFragmentWarnings(); - - const client = new ApolloClient({ - networkInterface: mockNetworkInterface(), - addTypename: false, - }); - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - }`; - const queryDoc = gql` - query { - author { - firstName - lastName - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - createFragment(fragmentDoc); - - withWarning(() => { - client.watchQuery({ query: queryDoc }); - }, /Warning: fragment with name/); - - disableFragmentWarnings(); - }); - it('should allow passing fragments to query', () => { const queryDoc = gql` query { @@ -1605,104 +1573,6 @@ it('should not let errors in observer.next reach the store', (done) => { }); }); - it('should allow passing fragments to watchQuery', () => { - const queryDoc = gql` - query { - author { - __typename - ...authorDetails - } - }`; - const composedQuery = gql` - query { - author { - __typename - ...authorDetails - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - const data = { - author: { - __typename: 'Author', - firstName: 'John', - lastName: 'Smith', - }, - }; - const networkInterface = mockNetworkInterface({ - request: { query: composedQuery }, - result: { data }, - }); - const client = new ApolloClient({ - networkInterface, - addTypename: false, - }); - const fragmentDefs = createFragment(gql` - fragment authorDetails on Author { - firstName - lastName - }`); - - const observable = client.watchQuery({ query: queryDoc, fragments: fragmentDefs }); - - return observableToPromise({ observable }, (result) => { - assert.deepEqual(result.data, data); - }); - }); - - it('should allow passing fragments in polling queries', () => { - const queryDoc = gql` - query { - author { - __typename - ...authorDetails - } - }`; - const composedQuery = gql` - query { - author { - __typename - ...authorDetails - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - const data = { - author: { - __typename: 'Author', - firstName: 'John', - lastName: 'Smith', - }, - }; - const networkInterface = mockNetworkInterface({ - request: { query: composedQuery }, - result: { data }, - }); - const client = new ApolloClient({ - networkInterface, - addTypename: false, - }); - const fragmentDefs = createFragment(gql` - fragment authorDetails on Author { - firstName - lastName - }`); - - const observable = client.watchQuery({ - query: queryDoc, - pollInterval: 30, - fragments: fragmentDefs, - }); - - return observableToPromise({ observable }, (result) => { - assert.deepEqual(result.data, data); - }); - }); - it('should not print a warning if we call disableFragmentWarnings', (done) => { const oldWarn = console.warn; console.warn = (str: string) => { diff --git a/test/optimistic.ts b/test/optimistic.ts index f953fe2fbb4..b50155f4f23 100644 --- a/test/optimistic.ts +++ b/test/optimistic.ts @@ -1196,49 +1196,6 @@ describe('optimistic mutation - githunt comments', () => { assert.equal(newResult.data.entry.comments.length, 2); }); }); - - it('can post a new comment (with fragments)', () => { - const mutationVariables = { - repoFullName: 'org/repo', - commentContent: 'New Comment', - }; - - let subscriptionHandle: Subscription; - return setup({ - request: { - query: addFragmentsToDocument(addTypenameToDocument(mutationWithFragment), fragmentWithTypenames), - variables: mutationVariables, - }, - result: mutationResult, - }) - .then(() => { - // we have to actually subscribe to the query to be able to update it - return new Promise( (resolve, reject) => { - const handle = client.watchQuery({ - query: queryWithFragment, - variables, - fragments: fragment, - }); - subscriptionHandle = handle.subscribe({ - next(res) { resolve(res); }, - }); - }); - }) - .then(() => { - return client.mutate({ - mutation: mutationWithFragment, - optimisticResponse, - variables: mutationVariables, - updateQueries, - fragments: fragment, - }); - }).then(() => { - return client.query({ query: queryWithFragment, variables, fragments: fragment }); - }).then((newResult: any) => { - subscriptionHandle.unsubscribe(); - assert.equal(newResult.data.entry.comments.length, 2); - }); - }); }); function realIdValue(id: string) { From 47866e328f4772acb189f37f20ea7f9e5964c9a6 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:20:24 +0800 Subject: [PATCH 02/16] remove fragment support from query' --- src/ApolloClient.ts | 29 +---------------- test/client.ts | 77 --------------------------------------------- 2 files changed, 1 insertion(+), 105 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 02d61482fb1..755e64cd197 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -300,34 +300,7 @@ export default class ApolloClient { } as DeprecatedWatchQueryOptions; } - if (options.fragments && !haveWarnedQuery && process.env.NODE_ENV !== 'production') { - console.warn( - '"fragments" option is deprecated and will be removed in the upcoming versions, ' + - 'please refer to the documentation for how to define fragments: ' + - 'http://dev.apollodata.com/react/fragments.html.', - ); - /* istanbul ignore if */ - if (process.env.NODE_ENV !== 'test') { - // When running tests, we want to print the warning every time - haveWarnedQuery = true; - } - } - - // Register each of the fragments present in the query document. The point - // is to prevent fragment name collisions with fragments that are in the query - // document itself. - createFragment(options.query, undefined, true); - - // We add the fragments to the document to pass only the document around internally. - const fullDocument = addFragmentsToDocument(options.query, options.fragments); - - const realOptions = { - ...options, - query: fullDocument, - }; - delete realOptions.fragments; - - return this.queryManager.query(realOptions); + return this.queryManager.query(options); }; /** diff --git a/test/client.ts b/test/client.ts index cd6c20793ff..8ba8d431ac3 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1451,83 +1451,6 @@ it('should not let errors in observer.next reach the store', (done) => { console.warn = oldWarn; }); - it('should issue a warning if we try query with a conflicting fragment name', () => { - enableFragmentWarnings(); - - const client = new ApolloClient({ - networkInterface: mockNetworkInterface(), - addTypename: false, - }); - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - }`; - const queryDoc = gql` - query { - author { - firstName - lastName - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - createFragment(fragmentDoc); - - withWarning(() => { - client.query({ query: queryDoc }); - }, /Warning: fragment with name/); - - disableFragmentWarnings(); - }); - - it('should allow passing fragments to query', () => { - const queryDoc = gql` - query { - author { - __typename - ...authorDetails - } - }`; - const composedQuery = gql` - query { - author { - __typename - ...authorDetails - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - const data = { - author: { - __typename: 'Author', - firstName: 'John', - lastName: 'Smith', - }, - }; - const networkInterface = mockNetworkInterface({ - request: { query: composedQuery }, - result: { data }, - }); - const client = new ApolloClient({ - networkInterface, - addTypename: false, - }); - const fragmentDefs = createFragment(gql` - fragment authorDetails on Author { - firstName - lastName - }`); - - return client.query({ query: queryDoc, fragments: fragmentDefs }).then((result) => { - assert.deepEqual(result.data, data); - }); - }); - it('show allow passing fragments to mutate', () => { const mutationDoc = gql` mutation createAuthor { From b0871eb61ec51279550c7f1c359aff0817f5fc5d Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:22:18 +0800 Subject: [PATCH 03/16] remove fragment support from mutate --- src/ApolloClient.ts | 24 +----------------------- test/client.ts | 45 --------------------------------------------- 2 files changed, 1 insertion(+), 68 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 755e64cd197..a7e1f263ec9 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -337,29 +337,7 @@ export default class ApolloClient { public mutate(options: MutationOptions): Promise { this.initStore(); - if (options.fragments && !haveWarnedMutation && process.env.NODE_ENV !== 'production') { - console.warn( - '"fragments" option is deprecated and will be removed in the upcoming versions, ' + - 'please refer to the documentation for how to define fragments: ' + - 'http://dev.apollodata.com/react/fragments.html.', - ); - /* istanbul ignore if */ - if (process.env.NODE_ENV !== 'test') { - // When running tests, we want to print the warning every time - haveWarnedMutation = true; - } - } - - // We add the fragments to the document to pass only the document around internally. - const fullDocument = addFragmentsToDocument(options.mutation, options.fragments); - - const realOptions = { - ...options, - mutation: fullDocument, - }; - delete realOptions.fragments; - - return this.queryManager.mutate(realOptions); + return this.queryManager.mutate(options); }; public subscribe(options: DeprecatedSubscriptionOptions): Observable { diff --git a/test/client.ts b/test/client.ts index 8ba8d431ac3..784b276fbbd 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1451,51 +1451,6 @@ it('should not let errors in observer.next reach the store', (done) => { console.warn = oldWarn; }); - it('show allow passing fragments to mutate', () => { - const mutationDoc = gql` - mutation createAuthor { - createAuthor { - __typename - ...authorDetails - } - }`; - const composedMutation = gql` - mutation createAuthor { - createAuthor { - __typename - ...authorDetails - } - } - fragment authorDetails on Author { - firstName - lastName - }`; - const data = { - createAuthor: { - __typename: 'Author', - firstName: 'John', - lastName: 'Smith', - }, - }; - const networkInterface = mockNetworkInterface({ - request: { query: composedMutation }, - result: { data }, - }); - const client = new ApolloClient({ - networkInterface, - addTypename: false, - }); - const fragmentDefs = createFragment(gql` - fragment authorDetails on Author { - firstName - lastName - }`); - - return client.mutate({ mutation: mutationDoc, fragments: fragmentDefs }).then((result) => { - assert.deepEqual(result, { data }); - }); - }); - it('should not print a warning if we call disableFragmentWarnings', (done) => { const oldWarn = console.warn; console.warn = (str: string) => { From b49619bf3fb4167058f65c2452978df859af91f7 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:24:16 +0800 Subject: [PATCH 04/16] remove fragments from subscribe --- src/ApolloClient.ts | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index a7e1f263ec9..5f45cd69e35 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -343,27 +343,10 @@ export default class ApolloClient { public subscribe(options: DeprecatedSubscriptionOptions): Observable { this.initStore(); - if (options.fragments && !haveWarnedSubscription && process.env.NODE_ENV !== 'production') { - console.warn( - '"fragments" option is deprecated and will be removed in the upcoming versions, ' + - 'please refer to the documentation for how to define fragments: ' + - 'http://dev.apollodata.com/react/fragments.html.', - ); - /* istanbul ignore if */ - if (process.env.NODE_ENV !== 'test') { - // When running tests, we want to print the warning every time - haveWarnedSubscription = true; - } - } - - // We add the fragments to the document to pass only the document around internally. - const fullDocument = addFragmentsToDocument(options.query, options.fragments); - const realOptions = { ...options, - document: fullDocument, + document: options.query, }; - delete realOptions.fragments; delete realOptions.query; return this.queryManager.startGraphQLSubscription(realOptions); From 898cc990ac271ef5f4d9442af520a51b2a2c8562 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:27:18 +0800 Subject: [PATCH 05/16] remove fragment docstring --- src/ApolloClient.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 5f45cd69e35..967c0a95bd2 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -316,9 +316,6 @@ export default class ApolloClient { * @param options.variables An object that maps from the name of a variable as used in the mutation * GraphQL document to that variable's value. * - * @param options.fragments A list of fragments as returned by {@link createFragment}. These fragments - * can be referenced from within the GraphQL mutation document. - * * @param options.optimisticResponse An object that represents the result of this mutation that will be * optimistically stored before the server has actually returned a result. This is most often * used for optimistic UI, where we want to be able to see the result of a mutation immediately, From 72239e0742845055f3693f571330db6e09c2dec9 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:33:53 +0800 Subject: [PATCH 06/16] remove fragments from query options --- src/core/watchQueryOptions.ts | 10 -------- test/client.ts | 19 +------------- test/fetchMore.ts | 47 ----------------------------------- 3 files changed, 1 insertion(+), 75 deletions(-) diff --git a/src/core/watchQueryOptions.ts b/src/core/watchQueryOptions.ts index 61bd32b2c6c..83fea522237 100644 --- a/src/core/watchQueryOptions.ts +++ b/src/core/watchQueryOptions.ts @@ -1,6 +1,5 @@ import { Document, - FragmentDefinition, } from 'graphql'; import { @@ -81,12 +80,6 @@ export interface DeprecatedWatchQueryOptions extends ModifiableWatchQueryOptions */ query: Document; - /** - * A list of fragments that are returned by {@link createFragment} which can be - * referenced from the query document. - */ - fragments?: FragmentDefinition[]; - /** * Arbitrary metadata stored in Redux with this query. Designed for debugging, * developer tools, etc. @@ -97,7 +90,6 @@ export interface DeprecatedWatchQueryOptions extends ModifiableWatchQueryOptions export interface FetchMoreQueryOptions { query?: Document; variables?: { [key: string]: any }; - fragments?: FragmentDefinition[]; } export type SubscribeToMoreOptions = { @@ -113,14 +105,12 @@ export type SubscribeToMoreOptions = { export interface DeprecatedSubscriptionOptions { query: Document; variables?: { [key: string]: any }; - fragments?: FragmentDefinition[]; }; export interface MutationOptions { mutation: Document; variables?: Object; resultBehaviors?: MutationBehavior[]; - fragments?: FragmentDefinition[]; optimisticResponse?: Object; updateQueries?: MutationQueryReducersMap; refetchQueries?: string[]; diff --git a/test/client.ts b/test/client.ts index 784b276fbbd..a21a60acd47 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1495,22 +1495,6 @@ it('should not let errors in observer.next reach the store', (done) => { assert(fragmentDefinitionsMap.hasOwnProperty('authorDetails')); assert.equal(fragmentDefinitionsMap['authorDetails'].length, 1); }); - - it('should not mutate the input document when querying', () => { - const client = new ApolloClient(); - - const fragments = createFragment(gql` - fragment authorDetails on Author { - author { - firstName - lastName - } - }`); - const query = gql`{ author { ...authorDetails } }`; - const initialDefinitions = query.definitions; - client.query({query, fragments}); - assert.equal(query.definitions, initialDefinitions); - }); }); it('should pass a network error correctly on a mutation', (done) => { @@ -1880,7 +1864,6 @@ function clientRoundrip( query: Document, data: GraphQLResult, variables?: any, - fragments?: FragmentDefinition[], ) { const networkInterface = mockNetworkInterface({ request: { query: cloneDeep(query) }, @@ -1891,7 +1874,7 @@ function clientRoundrip( networkInterface, }); - return client.query({ query, variables, fragments }) + return client.query({ query, variables }) .then((result) => { assert.deepEqual(result.data, data); }); diff --git a/test/fetchMore.ts b/test/fetchMore.ts index d2677025e34..2db9ae61657 100644 --- a/test/fetchMore.ts +++ b/test/fetchMore.ts @@ -228,51 +228,4 @@ describe('fetchMore on an observable query', () => { unsetup(); }); }); - - it('fetching more with fragments', () => { - latestResult = null; - // identical to query2, but with a fragment - const query3 = gql` - query NewComments($start: Int!, $limit: Int!) { - comments(start: $start, limit: $limit) { - ...textFragment - __typename - } - } - `; - const fragment = createFragment(gql` - fragment textFragment on Comment { - text - __typename - } - `); - return setup({ - request: { - query: addFragmentsToDocument(query3, fragment), - variables: variables2, - }, - result: result2, - }).then((watchedQuery) => { - return watchedQuery.fetchMore({ - query: query3, - variables: variables2, - fragments: fragment, - updateQuery: (prev, options) => { - const state = clonedeep(prev) as any; - state.entry.comments = [...state.entry.comments, ...(options.fetchMoreResult as any).data.comments]; - return state; - }, - }); - }).then(() => { - const comments = latestResult.data.entry.comments; - assert.lengthOf(comments, 20); - for (let i = 1; i <= 10; i++) { - assert.equal(comments[i - 1].text, `comment ${i}`); - } - for (let i = 11; i <= 20; i++) { - assert.equal(comments[i - 1].text, `new comment ${i}`); - } - unsetup(); - }); - }); }); From 334cdbdebc39008503be351d67d77844bac5ea24 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:37:25 +0800 Subject: [PATCH 07/16] remove DeprecatedWatchQueryOptions and DeprecationSubscriptionOptions --- src/ApolloClient.ts | 14 +++++++------- src/core/watchQueryOptions.ts | 17 +---------------- src/index.ts | 4 ++-- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 967c0a95bd2..1c3ad319654 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -44,8 +44,8 @@ import { } from './util/Observable'; import { - DeprecatedWatchQueryOptions, - DeprecatedSubscriptionOptions, + WatchQueryOptions, + SubscriptionOptions, MutationOptions, } from './core/watchQueryOptions'; @@ -265,14 +265,14 @@ export default class ApolloClient { * a description of store reactivity. * */ - public watchQuery(options: DeprecatedWatchQueryOptions): ObservableQuery { + public watchQuery(options: WatchQueryOptions): ObservableQuery { this.initStore(); if (!this.shouldForceFetch && options.forceFetch) { options = { ...options, forceFetch: false, - } as DeprecatedWatchQueryOptions; + } as WatchQueryOptions; } return this.queryManager.watchQuery(options); @@ -287,7 +287,7 @@ export default class ApolloClient { * how this query should be treated e.g. whether it is a polling query, whether it should hit the * server at all or just resolve from the cache, etc. */ - public query(options: DeprecatedWatchQueryOptions): Promise { + public query(options: WatchQueryOptions): Promise { this.initStore(); // XXX what if I pass pollInterval? Will it just keep running? @@ -297,7 +297,7 @@ export default class ApolloClient { options = { ...options, forceFetch: false, - } as DeprecatedWatchQueryOptions; + } as WatchQueryOptions; } return this.queryManager.query(options); @@ -337,7 +337,7 @@ export default class ApolloClient { return this.queryManager.mutate(options); }; - public subscribe(options: DeprecatedSubscriptionOptions): Observable { + public subscribe(options: SubscriptionOptions): Observable { this.initStore(); const realOptions = { diff --git a/src/core/watchQueryOptions.ts b/src/core/watchQueryOptions.ts index 83fea522237..1cd0105fda0 100644 --- a/src/core/watchQueryOptions.ts +++ b/src/core/watchQueryOptions.ts @@ -72,21 +72,6 @@ export interface WatchQueryOptions extends ModifiableWatchQueryOptions { metadata?: any; } -// This interface is deprecated because we no longer pass around fragments separately in the core. -export interface DeprecatedWatchQueryOptions extends ModifiableWatchQueryOptions { - /** - * A GraphQL document that consists of a single query to be sent down to the - * server. - */ - query: Document; - - /** - * Arbitrary metadata stored in Redux with this query. Designed for debugging, - * developer tools, etc. - */ - metadata?: any; -} - export interface FetchMoreQueryOptions { query?: Document; variables?: { [key: string]: any }; @@ -102,7 +87,7 @@ export type SubscribeToMoreOptions = { onError?: (error: Error) => void; }; -export interface DeprecatedSubscriptionOptions { +export interface SubscriptionOptions { query: Document; variables?: { [key: string]: any }; }; diff --git a/src/index.ts b/src/index.ts index c8e71dda8c9..e800fcd7c23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,7 @@ import { import { WatchQueryOptions, MutationOptions, - DeprecatedSubscriptionOptions, + SubscriptionOptions, } from './core/watchQueryOptions'; import { @@ -116,7 +116,7 @@ export { MutationBehavior, MutationQueryReducersMap, Subscription, - DeprecatedSubscriptionOptions as SubscriptionOptions, + SubscriptionOptions, ApolloStore, ApolloClient }; From 8ea7d0b28a44603a7cc7cd4887e1680fdd5a66e2 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:38:12 +0800 Subject: [PATCH 08/16] remove unnecessary variables --- src/ApolloClient.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index 1c3ad319654..a58dd4e5092 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -85,12 +85,6 @@ function defaultReduxRootSelector(state: any) { return state[DEFAULT_REDUX_ROOT_KEY]; } -// deprecation warning flags -let haveWarnedQuery = false; -let haveWarnedWatchQuery = false; -let haveWarnedMutation = false; -let haveWarnedSubscription = false; - /** * This is the primary Apollo Client class. It is used to send GraphQL documents (i.e. queries * and mutations) to a GraphQL spec-compliant server over a {@link NetworkInterface} instance, From 1322c09623264aafda47fde2520143453dc14754 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:39:02 +0800 Subject: [PATCH 09/16] remove unnecessary imports --- src/ApolloClient.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ApolloClient.ts b/src/ApolloClient.ts index a58dd4e5092..35464de9005 100644 --- a/src/ApolloClient.ts +++ b/src/ApolloClient.ts @@ -61,12 +61,6 @@ import { storeKeyNameFromFieldNameAndArgs, } from './data/storeUtils'; -import { createFragment } from './fragments'; - -import { - addFragmentsToDocument, -} from './queries/getFromAST'; - import { version, } from './version'; From 644010468694f7d20e2a45073f39167f2f346e01 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:41:32 +0800 Subject: [PATCH 10/16] remove fragment support from fetchMore --- src/core/ObservableQuery.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/core/ObservableQuery.ts b/src/core/ObservableQuery.ts index a743af83561..bd1e392f02f 100644 --- a/src/core/ObservableQuery.ts +++ b/src/core/ObservableQuery.ts @@ -25,8 +25,6 @@ import { tryFunctionOrLogError } from '../util/errorHandling'; import { NetworkStatus } from '../queries/store'; -import { addFragmentsToDocument } from '../queries/getFromAST'; - import isEqual = require('lodash/isEqual'); export type ApolloCurrentResult = { @@ -198,12 +196,9 @@ export class ObservableQuery extends Observable { }; } - // We add the fragments to the document to pass only the document around internally. - const fullQuery = addFragmentsToDocument(combinedOptions.query, combinedOptions.fragments); - combinedOptions = { ...combinedOptions, - query: fullQuery, + query: combinedOptions.query, forceFetch: true, } as WatchQueryOptions; return this.queryManager.fetchQuery(qid, combinedOptions); From 9fd5004240899fb32eb354496159bb8b6451f96f Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:43:32 +0800 Subject: [PATCH 11/16] remove unnecessary import from tests --- test/fetchMore.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/fetchMore.ts b/test/fetchMore.ts index 2db9ae61657..370f672b4a0 100644 --- a/test/fetchMore.ts +++ b/test/fetchMore.ts @@ -10,12 +10,6 @@ import clonedeep = require('lodash/cloneDeep'); import gql from 'graphql-tag'; -import { addFragmentsToDocument } from '../src/queries/getFromAST'; - -import { - createFragment, -} from '../src/index'; - describe('updateQuery on a simple query', () => { const query = gql` query thing { From 62451e09bfea22c1e3fe7652474d1636cbf2506f Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:51:43 +0800 Subject: [PATCH 12/16] remove fragment related code from index.ts and tests --- src/index.ts | 12 --- test/client.ts | 192 --------------------------------------------- test/getFromAST.ts | 136 -------------------------------- test/optimistic.ts | 29 +++---- 4 files changed, 9 insertions(+), 360 deletions(-) diff --git a/src/index.ts b/src/index.ts index e800fcd7c23..347cc4a9827 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,12 +62,6 @@ import { } from './errors/ApolloError'; import ApolloClient from './ApolloClient'; -import { - createFragment, - clearFragmentDefinitions, - disableFragmentWarnings, - enableFragmentWarnings, -} from './fragments'; import { ApolloQueryResult, @@ -91,12 +85,6 @@ export { NetworkStatus, ApolloError, - // fragment stuff - createFragment, - clearFragmentDefinitions, - disableFragmentWarnings, - enableFragmentWarnings, - getQueryDefinition, getFragmentDefinitions, FragmentMap, diff --git a/test/client.ts b/test/client.ts index a21a60acd47..93722bbe45d 100644 --- a/test/client.ts +++ b/test/client.ts @@ -3,11 +3,7 @@ const { assert } = chai; import * as sinon from 'sinon'; import ApolloClient, { - createFragment, - clearFragmentDefinitions, - disableFragmentWarnings, printAST, - enableFragmentWarnings, } from '../src'; import { @@ -88,7 +84,6 @@ import assign = require('lodash/assign'); chai.use(chaiAsPromised); // Turn off warnings for repeated fragment names -disableFragmentWarnings(); graphqlTagDisableFragmentWarnings(); describe('client', () => { @@ -1310,193 +1305,6 @@ it('should not let errors in observer.next reach the store', (done) => { assert.equal(printAST(query), print(query)); }); - describe('fragment referencing', () => { - afterEach(() => { - // after each test, we have to empty out fragmentDefinitionsMap since that is - // global state that will be held across all client instances. - clearFragmentDefinitions(); - }); - - it('should return a fragment def with a unique name', () => { - const fragment = gql` - fragment authorDetails on Author { - author { - firstName - lastName - } - } - `; - const fragmentDefs = createFragment(fragment); - assert.equal(fragmentDefs.length, 1); - assert.equal(print(fragmentDefs[0]), print(getFragmentDefinitions(fragment)[0])); - }); - - it('should correctly return multiple fragments from a single document', () => { - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - } - fragment personDetails on Person { - name - } - `; - const fragmentDefs = createFragment(fragmentDoc); - assert.equal(fragmentDefs.length, 2); - const expFragmentDefs = getFragmentDefinitions(fragmentDoc); - assert.equal(print(fragmentDefs[0]), print(expFragmentDefs[0])); - assert.equal(print(fragmentDefs[1]), print(expFragmentDefs[1])); - }); - - it('should correctly return fragment defs with one fragment depending on another', () => { - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - ...otherAuthorDetails - }`; - const otherFragmentDoc = gql` - fragment otherFragmentDoc on Author { - address - }`; - const fragmentDefs = createFragment(fragmentDoc, getFragmentDefinitions(otherFragmentDoc)); - assert.equal(fragmentDefs.length, 2); - const expFragmentDefs = getFragmentDefinitions(otherFragmentDoc) - .concat(getFragmentDefinitions(fragmentDoc)); - assert.deepEqual(fragmentDefs.map(print), expFragmentDefs.map(print)); - }); - - it('should return fragment defs with a multiple fragments depending on other fragments', () => { - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - ...otherAuthorDetails - } - - fragment onlineAuthorDetails on Author { - email - ...otherAuthorDetails - }`; - const otherFragmentDoc = gql` - fragment otherAuthorDetails on Author { - address - }`; - const fragmentDefs = createFragment(fragmentDoc, getFragmentDefinitions(otherFragmentDoc)); - assert.equal(fragmentDefs.length, 3); - - const expFragmentDefs = getFragmentDefinitions(otherFragmentDoc) - .concat(getFragmentDefinitions(fragmentDoc)); - assert.deepEqual(fragmentDefs.map(print), expFragmentDefs.map(print)); - }); - - it('should always return a flat array of fragment defs', () => { - const fragmentDoc1 = gql` - fragment authorDetails on Author { - firstName - lastName - ...otherAuthorDetails - }`; - const fragmentDoc2 = gql` - fragment otherAuthorDetails on Author { - address - }`; - const fragmentDoc3 = gql` - fragment personDetails on Person { - personDetails - }`; - const fragments1 = createFragment(fragmentDoc1); - const fragments2 = createFragment(fragmentDoc2); - const fragments3 = createFragment(fragmentDoc3, [fragments1, fragments2]); - assert.equal(fragments1.length, 1); - assert.equal(fragments2.length, 1); - assert.equal(fragments3.length, 3); - }); - - it('should add a fragment to the fragmentDefinitionsMap', () => { - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - }`; - assert.equal(Object.keys(fragmentDefinitionsMap).length, 0); - createFragment(fragmentDoc); - assert.equal(Object.keys(fragmentDefinitionsMap).length, 1); - assert(fragmentDefinitionsMap.hasOwnProperty('authorDetails')); - assert.equal(fragmentDefinitionsMap['authorDetails'].length, 1); - assert.equal(print(fragmentDefinitionsMap['authorDetails']), print(getFragmentDefinitions(fragmentDoc)[0])); - }); - - it('should add fragments with the same name to fragmentDefinitionsMap + print warning', () => { - const fragmentDoc = gql` - fragment authorDetails on Author { - firstName - lastName - } - fragment authorDetails on Author { - address - }`; - - // hacky solution that allows us to test whether the warning is printed - const oldWarn = console.warn; - console.warn = (str: string) => { - if (!str.match(/deprecated/)) { - assert.include(str, 'Warning: fragment with name'); - } - }; - - createFragment(fragmentDoc); - assert.equal(Object.keys(fragmentDefinitionsMap).length, 1); - assert.equal(fragmentDefinitionsMap['authorDetails'].length, 2); - console.warn = oldWarn; - }); - - it('should not print a warning if we call disableFragmentWarnings', (done) => { - const oldWarn = console.warn; - console.warn = (str: string) => { - if (!str.match(/deprecated/)) { - done(new Error('Returned a warning despite calling disableFragmentWarnings')); - } - }; - disableFragmentWarnings(); - createFragment(gql` - fragment authorDetails on Author { - firstName - } - `); - createFragment(gql` - fragment authorDetails on Author { - lastName - }`); - - // create fragment operates synchronously so if it returns and doesn't call - // console.warn, we are done. - setTimeout(() => { - console.warn = oldWarn; - done(); - }, 100); - }); - - it('should not add multiple instances of the same fragment to fragmentDefinitionsMap', () => { - createFragment(gql` - fragment authorDetails on Author { - author { - firstName - lastName - } - }`); - createFragment(gql` - fragment authorDetails on Author { - author { - firstName - lastName - } - }`); - assert(fragmentDefinitionsMap.hasOwnProperty('authorDetails')); - assert.equal(fragmentDefinitionsMap['authorDetails'].length, 1); - }); - }); - it('should pass a network error correctly on a mutation', (done) => { const mutation = gql` mutation { diff --git a/test/getFromAST.ts b/test/getFromAST.ts index 21036e816fc..85e1cc0a789 100644 --- a/test/getFromAST.ts +++ b/test/getFromAST.ts @@ -14,10 +14,6 @@ import { OperationDefinition, } from 'graphql'; -import { - createFragment, -} from '../src'; - import { print } from 'graphql-tag/printer'; import gql from 'graphql-tag'; import { assert } from 'chai'; @@ -234,136 +230,4 @@ describe('AST utility functions', () => { getQueryDefinition(queryWithTypeDefination); }, 'Schema type definitions not allowed in queries. Found: "InputObjectTypeDefinition"'); }); - - it('should attach fragments properly', () => { - const subjectInfo = createFragment(gql` - fragment subjectInfo on Subject { - id - name - }`, - ); - - const businessAreaInfo = createFragment(gql` - fragment businessAreaInfo on BusinessArea { - id - name - subjects { - ...subjectInfo - } - }`, - [subjectInfo], - ); - - const query = gql` - query { - businessAreas { - ...businessAreaInfo - } - } - `; - - const fullDoc = addFragmentsToDocument(query, businessAreaInfo); - - assert.equal(print(fullDoc), `{ - businessAreas { - ...businessAreaInfo - } -} - -fragment subjectInfo on Subject { - id - name -} - -fragment businessAreaInfo on BusinessArea { - id - name - subjects { - ...subjectInfo - } -} -`); - }); - - it('should only attach distinct fragments', () => { - const subjectInfo = createFragment(gql` - fragment subjectInfo on Subject { - id - name - }`, - ); - - const businessAreaInfo = createFragment(gql` - fragment businessAreaInfo on BusinessArea { - id - name - subjects { - ...subjectInfo - } - }`, - [subjectInfo, subjectInfo], - ); - - // to test that depending on the same fragment twice in different fragments won't add it twice. - const whateverAreaInfo = createFragment(gql` - fragment whateverAreaInfo on WhateverArea { - subject { - ...subjectInfo - } - }`, - [subjectInfo], - ); - - // XXX we don't attach a fragment here, because if we do, it won't be === to - // the same fragment created with createFragment. - const query = gql` - query { - businessAreas { - ...businessAreaInfo - } - whateverAreas { - ...whateverAreaInfo - } - } - - #fragment subjectInfo on Subject { - # id - # name - #} - `; - - let fullDoc = addFragmentsToDocument(query, businessAreaInfo); - fullDoc = addFragmentsToDocument(fullDoc, whateverAreaInfo); - // tests to make sure we can't add subjectInfo twice, even in separate call - fullDoc = addFragmentsToDocument(fullDoc, subjectInfo); - - assert.equal(print(fullDoc), `{ - businessAreas { - ...businessAreaInfo - } - whateverAreas { - ...whateverAreaInfo - } -} - -fragment subjectInfo on Subject { - id - name -} - -fragment businessAreaInfo on BusinessArea { - id - name - subjects { - ...subjectInfo - } -} - -fragment whateverAreaInfo on WhateverArea { - subject { - ...subjectInfo - } -} -`); - }); }); diff --git a/test/optimistic.ts b/test/optimistic.ts index b50155f4f23..8dbc1e245a9 100644 --- a/test/optimistic.ts +++ b/test/optimistic.ts @@ -2,7 +2,7 @@ import * as chai from 'chai'; const { assert } = chai; import mockNetworkInterface from './mocks/mockNetworkInterface'; -import ApolloClient, { createFragment } from '../src'; +import ApolloClient from '../src'; import { MutationBehaviorReducerArgs, MutationBehavior, MutationQueryReducersMap } from '../src/data/mutationResults'; import { NormalizedCache, StoreObject } from '../src/data/storeUtils'; import { addFragmentsToDocument } from '../src/queries/getFromAST'; @@ -1028,24 +1028,6 @@ describe('optimistic mutation - githunt comments', () => { } } `; - const fragment = createFragment(gql` - fragment authorFields on User { - postedBy { - login - html_url - } - } - `); - const fragmentWithTypenames = createFragment(gql` - fragment authorFields on User { - postedBy { - login - html_url - __typename - } - __typename - } - `); const queryWithFragment = gql` query Comment($repoName: String!) { entry(repoFullName: $repoName) { @@ -1054,6 +1036,13 @@ describe('optimistic mutation - githunt comments', () => { } } } + + fragment authorFields on User { + postedBy { + login + html_url + } + } `; const variables = { repoName: 'org/repo', @@ -1091,7 +1080,7 @@ describe('optimistic mutation - githunt comments', () => { result, }, { request: { - query: addFragmentsToDocument(addTypenameToDocument(queryWithFragment), fragment), + query: addTypenameToDocument(queryWithFragment), variables, }, result, From a8278daad4da46566d4279c8b2376d1814d94684 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:53:51 +0800 Subject: [PATCH 13/16] remove unnecessary import --- test/client.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/client.ts b/test/client.ts index 93722bbe45d..d3e588f2f6c 100644 --- a/test/client.ts +++ b/test/client.ts @@ -10,8 +10,6 @@ import { disableFragmentWarnings as graphqlTagDisableFragmentWarnings, } from 'graphql-tag'; -import { fragmentDefinitionsMap } from '../src/fragments'; - import { GraphQLError, GraphQLResult, From a779b96ab1cd18f998dd9803a02a702c9e427ba5 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:54:02 +0800 Subject: [PATCH 14/16] delete fragments file --- src/fragments.ts | 89 ------------------------------------------------ 1 file changed, 89 deletions(-) delete mode 100644 src/fragments.ts diff --git a/src/fragments.ts b/src/fragments.ts deleted file mode 100644 index 906758e24e6..00000000000 --- a/src/fragments.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { - Document, - FragmentDefinition, -} from 'graphql'; - -import flatten = require('lodash/flatten'); - -import { - getFragmentDefinitions, -} from './queries/getFromAST'; - -// A map going from the name of a fragment to that fragment's definition. -// The point is to keep track of fragments that exist and print a warning if we encounter two -// fragments that have the same name, i.e. the values *should* be of arrays of length 1. -// Note: this variable is exported solely for unit testing purposes. It should not be touched -// directly by application code. -export let fragmentDefinitionsMap: { [fragmentName: string]: FragmentDefinition[] } = {}; - -// Specifies whether or not we should print warnings about conflicting fragment names. -let printFragmentWarnings = true; - -// Takes a document, extracts the FragmentDefinitions from it and puts -// them in this.fragmentDefinitions. The second argument specifies the fragments -// that the fragment in the document depends on. The fragment definition array from the document -// is concatenated with the fragment definition array passed as the second argument and this -// concatenated array is returned. -let haveWarned = false; - -export function createFragment( - doc: Document, - fragments: (FragmentDefinition[] | FragmentDefinition[][]) = [], - internalUse = false, -): FragmentDefinition[] { - - if (!internalUse) { - if (! haveWarned) { - if (process.env.NODE_ENV !== 'production') { - console.warn( - '"createFragment" is deprecated and will be removed in version 0.6, ' + - 'please refer to the documentation for how to define fragments: ' + - 'http://dev.apollodata.com/react/fragments.html.', - ); - } - /* istanbul ignore if */ - if (process.env.NODE_ENV !== 'test') { - // When running tests, we want to print the warning every time - haveWarned = true; - } - } - } - - fragments = flatten(fragments) as FragmentDefinition[] ; - const fragmentDefinitions = getFragmentDefinitions(doc); - fragmentDefinitions.forEach((fragmentDefinition: FragmentDefinition) => { - const fragmentName = fragmentDefinition.name.value; - if (fragmentDefinitionsMap.hasOwnProperty(fragmentName) && - fragmentDefinitionsMap[fragmentName].indexOf(fragmentDefinition) === -1) { - // this is a problem because the app developer is trying to register another fragment with - // the same name as one previously registered. So, we tell them about it. - if (printFragmentWarnings) { - console.warn(`Warning: fragment with name ${fragmentDefinition.name.value} already exists. -Apollo Client enforces all fragment names across your application to be unique; read more about -this in the docs: http://docs.apollostack.com/`); - } - - fragmentDefinitionsMap[fragmentName].push(fragmentDefinition); - } else if (!fragmentDefinitionsMap.hasOwnProperty(fragmentName)) { - fragmentDefinitionsMap[fragmentName] = [fragmentDefinition]; - } - }); - - return fragments.concat(fragmentDefinitions); -} - -// This function disables the warnings printed about fragment names. One place where this chould be -// called is within writing unit tests that depend on Apollo Client and use named fragments that may -// have the same name across different unit tests. -export function disableFragmentWarnings() { - printFragmentWarnings = false; -} - -export function enableFragmentWarnings() { - printFragmentWarnings = true; -} - -// This function is used to be empty the namespace of fragment definitions. Used for unit tests. -export function clearFragmentDefinitions() { - fragmentDefinitionsMap = {}; -} From bbdcab14205a9f9d2a9a477417890fd80c11fe91 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:55:20 +0800 Subject: [PATCH 15/16] remove imports of addFragmentsToDocument --- test/optimistic.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/optimistic.ts b/test/optimistic.ts index 8dbc1e245a9..a683bac726d 100644 --- a/test/optimistic.ts +++ b/test/optimistic.ts @@ -5,7 +5,6 @@ import mockNetworkInterface from './mocks/mockNetworkInterface'; import ApolloClient from '../src'; import { MutationBehaviorReducerArgs, MutationBehavior, MutationQueryReducersMap } from '../src/data/mutationResults'; import { NormalizedCache, StoreObject } from '../src/data/storeUtils'; -import { addFragmentsToDocument } from '../src/queries/getFromAST'; import assign = require('lodash/assign'); import clonedeep = require('lodash/cloneDeep'); From ca96a0a8d6f821e6d00669e95574a89e6e0bb51a Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Sun, 25 Dec 2016 23:57:24 +0800 Subject: [PATCH 16/16] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 106f25abd67..ab4aa3ac732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Expect active development and potentially significant breaking changes in the `0 - ... +- Remove fragment option from query, watchQuery etc. [PR #1096](https://github.com/apollostack/apollo-client/pull/1096) ### 0.5.25 - Pass variables into result reducers [PR #1088](https://github.com/apollostack/apollo-client/pull/1088)