Comments (3)
Super delayed response from my side...
Very cool. It seems this is largely an issue with Mirage itself lacking WebSocket support. There's some interest in refactoring Mirage and decoupling it from Pretender to enable usage of other server mocking libraries: miragejs/miragejs#1013. It would be awesome to just support mocking subscriptions out-of-the-box.
from graphql.
Sorry, I don't personally have any suggestions. It would be really nice to have an official way to do this. AFAIK, Mirage doesn't have a built-in solution for dealing with web sockets but discussions have been had on a proposed API miragejs/discuss#23.
Feel free to share a use case here and any ideas you have for a good API and I'd be happy to put some work into it. I've been hesitant to add anything to this library so it may be good to use this project as a proving ground for a solution and then see if we can get something official into Mirage itself.
from graphql.
@jneurock Thanks for the response!
I attempted to go so far as to using graphql-subscriptions
and subscriptions-transport-ws
libraries in my mirage server, but that felt pretty heavy handed.
For the time being, I opted to create a test helper method to add the mocked server to the mirage server context, being sure to use the same wsURL
im using to set up the Apollo WebsocketLink:
// Note-- this is the only lib that worked, mock-socket did not work properly
import { Server } from 'mock-socket-with-protocol';
import type { TestContext } from 'ember-test-helpers';
import Config from 'app/config/environment';
import { assert } from '@ember/debug';
let operationMap: Record<string, string> = {};
export function buildSubscriptionMessage(
operationName: string,
payload: Record<string, any>
): string {
return JSON.stringify({
type: 'data',
payload,
id: operationMap[operationName],
});
}
export function setupWebsocketServer(hooks: NestedHooks): void {
hooks.beforeEach(function (this: TestContext) {
assert(
'[setupWebsocketServer] You must place `setupWebsocketServer` after `setupMirage`.',
this.server
);
this.server.ws = new Server(Config.apollo.wsURL);
this.server.ws.on('connection', (socket: WebSocket) => {
socket.on('message', (data: string) => {
const msg = JSON.parse(data);
if (!msg.payload?.operationName) {
return;
}
operationMap[msg.payload.operationName] = msg.id;
});
});
});
hooks.afterEach(function (this: TestContext) {
operationMap = {};
this.server.ws.stop();
});
}
The utility function buildSubscriptionMessage
is used to mock-send messages "from the backend":
// send example message
describe('a test suite!', function(hooks) {
setupMirage(hooks)
setupWebsocketServer(hooks)
test('a test', function(this: TestContext, assert) {
const done = assert.async();
this.server.ws.send(
buildSubscriptionMessage(
'SubOperationName',
{
id: '123',
moreData: 'Data'
}
)
);
// use a mocked handler somewhere to call done();
assert.verifySteps();
});
});
This is obviously Very pared down; subscriptions-ws-transport
has a very thorough API for building the messages that are delivered to the UI -- this naive buildSubscriptionMessage
will surely need to be fleshed out more to get more types of messages covered but for now, it seems to be getting the job done simply without re-building too much backend logic 🤷
Edit: Updated the solution based on how it has evolved for us!
from graphql.
Related Issues (20)
- Expose API or hooks for custom resolvers HOT 2
- createGraphQLHandler can't deal with directives HOT 4
- Relationship HOT 3
- Handle Custom scalar type like Date HOT 1
- Missing good examples how to use some features on README HOT 1
- How deal with complex queries? HOT 3
- TypeError on `returnType` when returning Union from mutations HOT 3
- Expected Iterable, but did not find one for field HOT 5
- connect ECONNREFUSED 127.0.0.1:4001 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) HOT 3
- Delete mutation example in README is incorrect
- [QUESTION] Does this plugin works for testing? HOT 13
- Mirage: You called server.create(<model_name>) but no model or factory was found HOT 2
- ApolloError: Cannot read property 'type' of undefined HOT 1
- How to respond with a server error? HOT 3
- How can I update an entry in resolver and trigger an update in watchQuery? HOT 1
- When updating data, an error occurs if there is a relationship HOT 1
- Example of own resolvers for query / filter involving Union types HOT 1
- Help Wanted: TypeScript and Priorities HOT 9
- model.hasInverseFor is not a function
- How to reproduce NOT_FOUND error with context.mirageSchema.findBy() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql.