Giter VIP home page Giter VIP logo

Comments (5)

enisdenjo avatar enisdenjo commented on May 23, 2024 2

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.

enisdenjo avatar enisdenjo commented on May 23, 2024 1

I added a server usage with Express GraphQL recipe 👉 680673e.

from graphql-ws.

hongbo-miao avatar hongbo-miao commented on May 23, 2024 1

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.

hongbo-miao avatar hongbo-miao commented on May 23, 2024

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.

hongbo-miao avatar hongbo-miao commented on May 23, 2024

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...

image

from graphql-ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.