Skip to content

Commit

Permalink
Merge pull request #984 from apollostack/remove-fragments
Browse files Browse the repository at this point in the history
deprecate fragments everywhere
  • Loading branch information
helfer authored Dec 3, 2016
2 parents b2d9a7c + 78ccf53 commit 2b0755b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 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
- Deprecate usage of fragment option and createFragment function [PR #984](https://github.com/apollostack/apollo-client/pull/984)

### 0.5.9
- Prevent Redux from crashing when an uncaught ApolloError is raised in an Apollo reducer. [PR #874](https://github.com/apollostack/apollo-client/pull/874)
Expand Down
62 changes: 60 additions & 2 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ 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,
Expand Down Expand Up @@ -249,10 +255,23 @@ 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);
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);
Expand Down Expand Up @@ -286,10 +305,23 @@ 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);
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);
Expand Down Expand Up @@ -336,6 +368,19 @@ export default class ApolloClient {
public mutate(options: MutationOptions): Promise<ApolloQueryResult> {
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);

Expand All @@ -350,6 +395,19 @@ export default class ApolloClient {
public subscribe(options: DeprecatedSubscriptionOptions): Observable<any> {
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);

Expand Down
23 changes: 22 additions & 1 deletion src/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,31 @@ let printFragmentWarnings = true;
// 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[][]) = []
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) => {
Expand Down
8 changes: 6 additions & 2 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,9 @@ it('should not let errors in observer.next reach the store', (done) => {
// hacky solution that allows us to test whether the warning is printed
const oldWarn = console.warn;
console.warn = (str: string) => {
assert.include(str, 'Warning: fragment with name');
if (!str.match(/deprecated/)) {
assert.include(str, 'Warning: fragment with name');
}
};

createFragment(fragmentDoc);
Expand Down Expand Up @@ -1622,7 +1624,9 @@ it('should not let errors in observer.next reach the store', (done) => {
it('should not print a warning if we call disableFragmentWarnings', (done) => {
const oldWarn = console.warn;
console.warn = (str: string) => {
done(new Error('Returned a warning despite calling disableFragmentWarnings'));
if (!str.match(/deprecated/)) {
done(new Error('Returned a warning despite calling disableFragmentWarnings'));
}
};
disableFragmentWarnings();
createFragment(gql`
Expand Down
2 changes: 1 addition & 1 deletion test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require('source-map-support').install();
console.warn = console.error = (...messages: string[]) => {
console.log(`==> Error in test: Tried to log warning or error with message:
`, ...messages);
if (!process.env.CI) {
if (!process.env.CI && !messages[0].match(/deprecated/)) {
process.exit(1);
}
};
Expand Down

0 comments on commit 2b0755b

Please sign in to comment.