Skip to content

Commit

Permalink
fixed style issues + fixed a problem where warning messages where pri…
Browse files Browse the repository at this point in the history
…nting where they were not supposed to
  • Loading branch information
Poincare committed Jul 1, 2016
1 parent f7e8d16 commit c6ad8db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ export class QueryManager {

// Returns a query listener that will update the given observer based on the
// results (or lack thereof) for a particular query.
public queryListenerForObserver(options: WatchQueryOptions,
observer: Observer<GraphQLResult>): QueryListener {
public queryListenerForObserver(
options: WatchQueryOptions,
observer: Observer<GraphQLResult>
): QueryListener {
return (queryStoreValue: QueryStoreValue) => {
if (!queryStoreValue.loading || queryStoreValue.returnPartialData) {
// XXX Currently, returning errors and data is exclusive because we
Expand Down Expand Up @@ -418,7 +420,7 @@ export class QueryManager {
}

public fetchQuery(queryId: string, options: WatchQueryOptions): Promise<GraphQLResult> {
return this.fetchQueryOverInterface(queryId, options, this.networkInterface);
return this.fetchQueryOverInterface(queryId, options, this.networkInterface);
}

public generateQueryId() {
Expand Down Expand Up @@ -510,9 +512,11 @@ export class QueryManager {
});
}

private fetchQueryOverInterface(queryId: string,
options: WatchQueryOptions,
network: NetworkInterface): Promise<GraphQLResult> {
private fetchQueryOverInterface(
queryId: string,
options: WatchQueryOptions,
network: NetworkInterface
): Promise<GraphQLResult> {
const {
query,
variables,
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export function createFragment(doc: Document, fragments: FragmentDefinition[] =
// 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.`);
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);
Expand All @@ -106,12 +108,16 @@ export function createFragment(doc: Document, fragments: FragmentDefinition[] =
}

// This function disables the warnings printed about fragment names. One place where this chould be
// is called when writing unit tests that depend on Apollo Client and use named fragments that may
// have the Nsame name across different unit tests.
// 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 = {};
Expand Down
14 changes: 13 additions & 1 deletion test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ApolloClient, {
fragmentDefinitionsMap,
clearFragmentDefinitions,
disableFragmentWarnings,
enableFragmentWarnings,
} from '../src';

import {
Expand Down Expand Up @@ -59,6 +60,9 @@ import * as chaiAsPromised from 'chai-as-promised';
// make it easy to assert with promises
chai.use(chaiAsPromised);

// Turn off warnings for repeated fragment names
disableFragmentWarnings();

describe('client', () => {
it('does not require any arguments and creates store lazily', () => {
const client = new ApolloClient();
Expand Down Expand Up @@ -1097,7 +1101,7 @@ describe('client', () => {

// hacky solution that allows us to test whether the warning is printed
const oldWarn = console.warn;
console.warn = (str) => {
console.warn = (str, vals) => {
assert.include(str, 'Warning: fragment with name');
};

Expand All @@ -1108,6 +1112,8 @@ describe('client', () => {
});

it('should issue a warning if we try query with a conflicting fragment name', (done) => {
enableFragmentWarnings();

const client = new ApolloClient({
networkInterface: mockNetworkInterface(),
});
Expand Down Expand Up @@ -1136,9 +1142,13 @@ describe('client', () => {
done();
};
client.query({ query: queryDoc });

disableFragmentWarnings();
});

it('should issue a warning if we try to watchQuery with a conflicting fragment name', (done) => {
enableFragmentWarnings();

const client = new ApolloClient({
networkInterface: mockNetworkInterface(),
});
Expand Down Expand Up @@ -1167,6 +1177,8 @@ describe('client', () => {
done();
};
client.watchQuery({ query: queryDoc });

disableFragmentWarnings();
});

it('should allow passing fragments to query', (done) => {
Expand Down

0 comments on commit c6ad8db

Please sign in to comment.