Skip to content

Commit

Permalink
Convert tests from IntrospectionFragmentMatcher to possibleTypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jul 18, 2019
1 parent aa53d50 commit 56d14ec
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 155 deletions.
10 changes: 8 additions & 2 deletions packages/apollo-client/src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@ describe('ApolloClient', () => {
it('should warn when the data provided does not match the query shape', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
cache: new InMemoryCache({
// Passing an empty map enables the warning:
possibleTypes: {},
}),
});

return withWarning(() => {
Expand Down Expand Up @@ -1073,7 +1076,10 @@ describe('ApolloClient', () => {
it('should warn when the data provided does not match the fragment shape', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
cache: new InMemoryCache({
// Passing an empty map enables the warning:
possibleTypes: {},
}),
});

return withWarning(() => {
Expand Down
145 changes: 26 additions & 119 deletions packages/apollo-client/src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { cloneDeep, assign } from 'lodash';
import { GraphQLError, ExecutionResult, DocumentNode } from 'graphql';
import gql from 'graphql-tag';
import { ApolloLink, Observable } from 'apollo-link';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
FragmentMatcherInterface,
} from 'apollo-cache-inmemory';
import { InMemoryCache, PossibleTypesMap } from 'apollo-cache-inmemory';
import { stripSymbols } from 'apollo-utilities';

import { QueryManager } from '../core/QueryManager';
Expand Down Expand Up @@ -327,25 +323,9 @@ describe('client', () => {
__typename: 'Query',
};

const ifm = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Query',
possibleTypes: [
{
name: 'Record',
},
],
},
],
},
},
return clientRoundtrip(query, { data }, null, {
Query: ['Record'],
});

return clientRoundtrip(query, { data }, null, ifm);
});

it('should merge fragments on root query', () => {
Expand Down Expand Up @@ -378,25 +358,9 @@ describe('client', () => {
__typename: 'Query',
};

const ifm = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Query',
possibleTypes: [
{
name: 'Record',
},
],
},
],
},
},
return clientRoundtrip(query, { data }, null, {
Query: ['Record'],
});

return clientRoundtrip(query, { data }, null, ifm);
});

it('store can be rehydrated from the server', () => {
Expand Down Expand Up @@ -1218,41 +1182,16 @@ describe('client', () => {
],
};

const fancyFragmentMatcher = (
idValue: any, // TODO types, please.
typeCondition: string,
context: any,
): boolean => {
const obj = context.store.get(idValue.id);

if (!obj) {
return false;
}

const implementingTypesMap: { [key: string]: string[] } = {
Item: ['ColorItem', 'MonochromeItem'],
};

if (obj.__typename === typeCondition) {
return true;
}

const implementingTypes = implementingTypesMap[typeCondition];
if (implementingTypes && implementingTypes.indexOf(obj.__typename) > -1) {
return true;
}

return false;
};

const link = mockSingleLink({
request: { query },
result: { data: result },
});
const client = new ApolloClient({
link,
cache: new InMemoryCache({
fragmentMatcher: { match: fancyFragmentMatcher },
possibleTypes: {
Item: ['ColorItem', 'MonochromeItem'],
},
}),
});
return client.query({ query }).then((actualResult: any) => {
Expand Down Expand Up @@ -1297,30 +1236,13 @@ describe('client', () => {
result: { data: result },
});

const ifm = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Item',
possibleTypes: [
{
name: 'ColorItem',
},
{
name: 'MonochromeItem',
},
],
},
],
},
},
});

const client = new ApolloClient({
link,
cache: new InMemoryCache({ fragmentMatcher: ifm }),
cache: new InMemoryCache({
possibleTypes: {
Item: ['ColorItem', 'MonochromeItem'],
},
}),
});

return client.query({ query }).then(actualResult => {
Expand Down Expand Up @@ -1380,30 +1302,13 @@ describe('client', () => {
},
);

const ifm = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Item',
possibleTypes: [
{
name: 'ColorItem',
},
{
name: 'MonochromeItem',
},
],
},
],
},
},
});

const client = new ApolloClient({
link,
cache: new InMemoryCache({ fragmentMatcher: ifm }),
cache: new InMemoryCache({
possibleTypes: {
Item: ['ColorItem', 'MonochromeItem'],
},
}),
});

const queryUpdaterSpy = jest.fn();
Expand Down Expand Up @@ -2663,7 +2568,10 @@ describe('client', () => {
});
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
cache: new InMemoryCache({
// Passing an empty map enables the warning:
possibleTypes: {},
}),
});

return withWarning(
Expand Down Expand Up @@ -2952,19 +2860,18 @@ function clientRoundtrip(
query: DocumentNode,
data: ExecutionResult,
variables?: any,
fragmentMatcher?: FragmentMatcherInterface,
possibleTypes?: PossibleTypesMap,
) {
const link = mockSingleLink({
request: { query: cloneDeep(query) },
result: data,
});

const config = {};
if (fragmentMatcher) config.fragmentMatcher = fragmentMatcher;

const client = new ApolloClient({
link,
cache: new InMemoryCache(config),
cache: new InMemoryCache({
possibleTypes,
}),
});

return client.query({ query, variables }).then(result => {
Expand Down
17 changes: 3 additions & 14 deletions packages/apollo-client/src/__tests__/local-state/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ApolloClient from '../..';
import { ApolloCache } from 'apollo-cache';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory';
import { ApolloLink, Observable, Operation } from 'apollo-link';
import { hasDirectives } from 'apollo-utilities';
Expand Down Expand Up @@ -227,19 +226,9 @@ describe('General functionality', () => {

const client = new ApolloClient({
cache: new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UnionTypeDefinition',
name: 'Foo',
possibleTypes: [{ name: 'Bar' }, { name: 'Baz' }],
},
],
},
},
}),
possibleTypes: {
Foo: ['Bar', 'Baz'],
},
}),
link,
resolvers,
Expand Down
4 changes: 4 additions & 0 deletions packages/apollo-client/src/__tests__/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ describe('mutation results', () => {
}
return null;
},
// Passing an empty map enables warnings about missing fields:
possibleTypes: {},
}),
});

Expand Down Expand Up @@ -166,6 +168,8 @@ describe('mutation results', () => {
}
return null;
},
// Passing an empty map enables warnings about missing fields:
possibleTypes: {},
}),
});

Expand Down
17 changes: 3 additions & 14 deletions packages/apollo-client/src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import gql from 'graphql-tag';
import { ApolloLink, Observable } from 'apollo-link';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory';
import { GraphQLError } from 'graphql';

Expand Down Expand Up @@ -1411,19 +1410,9 @@ describe('ObservableQuery', () => {
const client = new ApolloClient({
link: ni,
cache: new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Creature',
possibleTypes: [{ name: 'Pet' }],
},
],
},
},
}),
possibleTypes: {
Creature: ['Pet'],
},
}),
});

Expand Down
2 changes: 0 additions & 2 deletions packages/apollo-client/src/core/__tests__/fetchPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { print } from 'graphql/language/printer';
import { ApolloLink, Observable } from 'apollo-link';
import {
InMemoryCache,
IntrospectionFragmentMatcher,
FragmentMatcherInterface,
} from 'apollo-cache-inmemory';
import { stripSymbols } from 'apollo-utilities';

Expand Down
5 changes: 1 addition & 4 deletions packages/apollo-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ export {
} from './core/watchQueryOptions';
export { NetworkStatus } from './core/networkStatus';
export * from './core/types';
export {
Resolver,
FragmentMatcher as LocalStateFragmentMatcher,
} from './core/LocalState';
export { Resolver } from './core/LocalState';

export { isApolloError, ApolloError } from './errors/ApolloError';

Expand Down

0 comments on commit 56d14ec

Please sign in to comment.