5.0.0 • Published 1 year ago

graphql-http-ws-server v5.0.0

Weekly downloads
29
License
-
Repository
github
Last release
1 year ago

GraphQL HTTP and WS Server

Note: The server will only intercept WS connections that have the graphQL subscription path

Options

OptionDescriptionDefault
schemaExecutable schemanull
typeDefsGraphQL type definitions (combine with resolvers)null
resolversGraphQL type definitions (combine with typeDefs)null
portPort for HTTP Server to listen on80
graphQLPathPath under HTTP server for GraphQL/graphql
subscriptionsPathPath under WS server for GraphQL/graphql
listenStart HTTP Servertrue
loggerPass extrernal logger (eg: Winston)null
pluginsApollo plugins[]
expressAppPass in an express appnull, module will create
httpServerPass in an HTTP servernull, module will create
wsServerpass in a WS servernull, module will create

Example

import { makeExecutableSchema } from "@graphql-tools/schema";
import gql from 'graphql-tag'
import { EventEmitterAsyncIterator } from 'event-emitter-async-iterator';
import createGraphQLHTTPServer from "graphql-http-ws-server";

const typeDefs = gql(`
    type Query {
        hello: String
    }
    type Subscription {
        time: String
    }
`);

const resolvers = {
    Query: {
        hello: (obj, args, context) => {
            return 'world';
        }
    },
    Subscription: {
        time: {
            subscribe: (obj, args, context, info) => {
                const asyncIterator = new EventEmitterAsyncIterator();

                const sendDate = () => {
                    asyncIterator.pushValue({
                        time: (new Date()).toISOString()
                    });
                };

                const sendInterval = setInterval(() => {
                    sendDate()
                }, 1000);


                asyncIterator.once('return', () => {
                    clearInterval(sendInterval);
                });

                sendDate();

                return asyncIterator;
            }
        },
    }
};

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
});

createGraphQLHTTPServer({
    schema,
    port: 80,
    playground: true,
    graphqlPath: '/graphql',
    subscriptionsPath: '/graphql',
    onConnect: async (params) => { // WS
        console.log("LEGACY WS CONNECT", params);
        return {connection: "Legacy WS Connect"};
    },
    wsContext: (params) => {
        console.log("WS CONNECT", params.connectionParams)
        return {connection: "New WS Connect"};
    },
    httpContext: async ({req}) => { // HTTP
        console.log("HTTP CONNECT", req.headers);
        return {connection: "HTTP Connect"};
    }
});
5.0.0

1 year ago

4.0.1

2 years ago

4.0.0

2 years ago

3.0.2

2 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.2.0

3 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago