Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-apollo-client

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-apollo-client - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

12

CHANGELOG.md

@@ -0,1 +1,7 @@

# [1.2.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v1.2.0) (2021-08-10)
### Features
* Handle fragments [#24](https://github.com/Mike-Gibson/mock-apollo-client/issues/31)
# [1.1.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v1.1.0) (2021-04-10)

@@ -22,2 +28,8 @@

# [0.7.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v0.7.0) (2021-04-27)
### Features
* Support subscriptions [#28](https://github.com/Mike-Gibson/mock-apollo-client/issues/28)
# [0.6.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v0.6.0) (2021-04-01)

@@ -24,0 +36,0 @@

21

dist/mockLink.js

@@ -76,4 +76,23 @@ "use strict";

exports.MockLink = MockLink;
var normalise = function (requestQuery) {
var stripped = utilities_1.removeConnectionDirectiveFromDocument(requestQuery);
stripped = stripped !== null
? stripTypenames(stripped)
: null;
return stripped === null
? requestQuery
: stripped;
};
var stripTypenames = function (document) {
return graphql_1.visit(document, {
Field: {
enter: function (node) { return node.name.value === '__typename'
? null
: undefined; },
},
});
};
var requestToKey = function (query) {
var queryString = query && graphql_1.print(query);
var normalised = normalise(query);
var queryString = query && graphql_1.print(normalised);
var requestKey = { query: queryString };

@@ -80,0 +99,0 @@ return JSON.stringify(requestKey);

2

package.json
{
"name": "mock-apollo-client",
"version": "1.1.0",
"version": "1.2.0",
"description": "Library to help unit testing when using apollo-client",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -280,1 +280,48 @@ # Mock Apollo Client

Note: it is not possible to specify the `link` to use as this is how `mock-apollo-client` injects its behaviour.
### Fragments
If your queries or mutations use fragments against union or interface types, you must inject a cache object when creating the mock client which has been provided with `possibleTypes`, and also include the correct `__typename` field when mocking the response.
For example:
```typescript
import { InMemoryCache } from '@apollo/client';
import { createMockClient } from 'mock-apollo-client';
const cache = new InMemoryCache({
possibleTypes: {
Hardware: ['Memory', 'Cpu'],
},
});
const mockClient = createMockClient({ cache });
```
You must then ensure that the query result includes the `__typename` field as it would when calling your actual GraphQL API. This is to ensure that the fragment matching works as expected:
```typescript
const query = gql`
query Hardware {
hardware {
id
... on Memory {
size
}
... on Cpu {
speed
}
}
}
`;
const mockData = {
hardware: {
__typename: 'Memory',
id: 2,
size: '16gb',
},
};
const requestHandler = jest.fn().mockResolvedValue({ data: mockData });
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc