Comments (5)
Hey @hongbo-miao, you're very welcome!
Thank you for asking this question. In short - yes, this library supports express-graphql
and it should fulfil all your requirements.
On the side note, 101 Switching Protocols
is good. This indicates that the HTTP handshake went well and the browser is "switching" to the WebSocket Protocol.
Back to your problem - we don't support specifying the context during the server creation (#13); however, you can still inject it in the execute
and/or subscribe
operations. Also, you can indeed perform all 3 operations through WebSocket.
Here is how:
// server.ts
import http from 'http';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { createServer } from 'graphql-transport-ws';
import { execute, subscribe } from 'graphql';
// your schema
const schema = { ... };
// your context
const context = {
dataLoaders: {
user: userDataLoader,
},
};
// create express and middleware
const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema,
context,
}),
);
// create a http server using express
const server = http.createServer(app);
// listen on port 7070
server.listen(7070, () => {
// and create a WebSocket server on the same port
createServer(
{
schema,
execute: (args) =>
execute({
...args,
context, // inject the context
}),
subscribe: (args) =>
subscribe({
...args,
context, // inject the context
}),
},
{
server,
path: '/graphql', // you can even use the same path (just use the `ws` scheme)
},
);
});
// client.ts
import { createClient } from 'graphql-transport-ws';
const client = createClient({
url: 'ws://localhost:7070/graphql',
});
// query
client.subscribe(
{
query: `{ sayHi }`,
},
{
next: (data) => {
console.log(data);
},
error: (error) => {
console.error(error);
},
complete: () => {
console.log('said hi');
},
},
);
// subscription
client.subscribe(
{
query: `subscriptions { planetsInOurSolarSystem }`,
},
{
next: (data) => {
// will be called 8 times
console.log(data);
},
error: (error) => {
console.error(error);
},
complete: () => {
console.log('no more planets');
},
},
);
from graphql-ws.
I added a server usage with Express GraphQL
recipe 👉 680673e.
from graphql-ws.
Thanks @enisdenjo for the explaining!
Oh now I get it, with graphql-transport-ws
, it can provide query and mutation on ws
or wss
protocal.
And thanks for the context
method!
I succeed on the demo using Schema Definition Language with buildSchema
, but my case actually failed on using GraphQLSchema object way, since it is different topic, I will ask in another ticket. Posted at #15
from graphql-ws.
I think to work with express-graphql
, I can use express-graphql
for query and mutation schema, and graphql-transport-ws
for subscription schema only (?) Let me try.
So then my following up question will this support context
so that I can use dataloader? Thanks
from graphql-ws.
Hmm, still no luck.
My client is at https://localhost:8080
Server is at https://localhost:5000
For graphql-transport-ws
, I am using wss://localhost:5000/graphql
Got many "101 Switching Protocols" on client side...
from graphql-ws.
Related Issues (20)
- Hasura connection error is not reported correctly
- Server race condition: connection ack + first subscribe HOT 6
- Allow to update connectionParams
- fix incorrect Bad Gateway error code HOT 3
- Subscriptions not working with Hasura via apollo Client graphql. error - Error: WebSocket implementation missing; HOT 1
- rename onMessage in server to postMessage, withMessage (or something more appropriate) HOT 1
- How to use `graphql-ws` protocol?
- Websocket using bun server - Unable to access request object in ctx.extras
- Warning Workspaces can only be enabled in private projects. HOT 3
- Detect subscription successfully started
- Errors during streaming subscriptions aren't properly handled or emitted HOT 1
- httpOnly cookie authentication
- Promises getting rejected with non-Error objects HOT 2
- Some subscriptions remain open after a websocket has been closed HOT 2
- Getting "Internal error occurred during message handling. Please check your implementation." after following recipe
- Clarify Contribution Policy
- Clients can be sent an `Error` message after cancellation. HOT 2
- How to avoid logging of hole object in graphql-ws?
- Consider migrating to crossws
- Make code and reason optional when closing a connection 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-ws.