Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('creates query manager lazily', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
});
expect(client.queryManager).toBeUndefined();
// We only create the query manager on the first query
client.initQueryManager();
expect(client.queryManager).toBeDefined();
expect(client.cache).toBeDefined();
});
queryDeduplication = true,
defaultOptions,
assumeImmutableResults = false,
resolvers,
typeDefs,
fragmentMatcher,
name: clientAwarenessName,
version: clientAwarenessVersion,
} = options;
let { link } = options;
// If a link hasn't been defined, but local state resolvers have been set,
// setup a default empty link.
if (!link && resolvers) {
link = ApolloLink.empty();
}
if (!link || !cache) {
throw new InvariantError(
"In order to initialize Apollo Client, you must specify 'link' and 'cache' properties in the options object.\n" +
"These options are part of the upgrade requirements when migrating from Apollo Client 1.x to Apollo Client 2.x.\n" +
"For more information, please visit: https://www.apollographql.com/docs/tutorial/client.html#apollo-client-setup"
);
}
// remove apollo-client supported directives
this.link = link;
this.cache = cache;
this.store = new DataStore(cache);
this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
this.queryDeduplication = queryDeduplication;
it('should run initializers asynchronously', async (done) => {
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});
client.runInitializers({
primaryUserId: () => 100,
});
expect(cache.extract()).toEqual({});
await client.runInitializers({
secondaryUserId: () => 200,
});
expect(cache.extract()).toEqual({
ROOT_QUERY: {
primaryUserId: 100,
secondaryUserId: 200,
},
});
it('gives appropriate networkStatus for refetched queries', done => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
resolvers: {
Query: {
hero(_data, args) {
return {
__typename: 'Hero',
...args,
name: 'Luke Skywalker',
};
},
},
},
});
const observable = client.watchQuery({
query: gql`
done => {
const query = gql`
{
car @client {
engine {
torque @export(as: "torque")
}
}
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
resolvers: {},
});
cache.writeData({
data: {
car: {
engine: {
cylinders: 8,
torque: 7200,
__typename: 'Engine',
},
__typename: 'Car',
},
},
});
() => {
const query = gql`
{
fie: foo @client {
bar
}
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
resolvers: {},
});
cache.writeData({
data: {
foo: {
bar: 'yo',
__typename: 'Foo',
},
},
});
return client.query({ query }).then(({ data }) => {
expect({ ...data }).toMatchObject({
fie: { bar: 'yo', __typename: 'Foo' },
});
expect(() => {
new ApolloClient({ link: ApolloLink.empty() } as any);
}).toThrowErrorMatchingSnapshot();
});