question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does this work with express-graphql or replace it?

See original GitHub issue

Thanks @enisdenjo for this awesome package!

A question, does this work with express-graphql or replace it?

Before I had something like

import { graphqlHTTP } from 'express-graphql';

const graphQLMiddleware = graphqlHTTP({
  context: {
    dataLoaders: {
      user: userDataLoader,
    },
  },
  schema,
});

However, when I write code like this, it does not supports context:

import { createServer } from 'graphql-transport-ws';

createServer(
  {
    context: { // <- not support here
      dataLoaders: {
        user: userDataLoader,
      },
    },
    schema,
  },
  {
    server,
    path: '/graphql',
  }
);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
enisdenjocommented, Sep 17, 2020

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');
    },
  },
);
1reaction
Hongbo-Miaocommented, Sep 17, 2020

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 https://github.com/enisdenjo/graphql-transport-ws/issues/15

Read more comments on GitHub >

github_iconTop Results From Across the Web

expressGraphQL is not a function - Stack Overflow
Please replace your expressGraphQL with graphqlHTTP as it was destructured. Use: const { graphqlHTTP } = require('express-graphql');.
Read more >
Running an Express GraphQL Server
The simplest way to run a GraphQL API server is to use Express, a popular web application framework for Node.js. You will need...
Read more >
ExpressJS vs GraphQL | What are the differences? - StackShare
Express is a minimal and flexible node.js web application framework, ... whereas "Schemas defined by the requests made by the user", "Will replace...
Read more >
Creating A GraphQL Server With Node.js And Express - Medium
GraphQL is a language that enables you to provide a complete and understandable description of the data in your API. Furthermore it gives...
Read more >
Create a GraphQL HTTP server with Express. - GitHub
The official GraphQL over HTTP work group is standardizing the way you transport GraphQL over HTTP and it made great progress bringing up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found