Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use graph-ws instead #3

Open
ziedHamdi opened this issue Jul 27, 2021 · 5 comments
Open

How to use graph-ws instead #3

ziedHamdi opened this issue Jul 27, 2021 · 5 comments

Comments

@ziedHamdi
Copy link

ziedHamdi commented Jul 27, 2021

Hi,

Thanks for this project that simplifies the burden on how to setup subscription. My issue is that it relies on subscriptions-transport-ws which is deprecated to the benefit of https://the-guild.dev/blog/graphql-over-websockets as mentioned on their npm page:

The subscriptions-transport-ws library is not being actively maintained. It is recommended that you use the graphql-ws library instead. For details read the GraphQL over WebSockets announcement.

So I'm struggling about how to get the roots of my gql schema from the object returned by schemaComposer.buildSchema()

subscriptionServer.js

import ws from 'ws'; // yarn add ws
import { useServer } from 'graphql-ws/lib/use/ws';
const GraphQlSchema = require('./graphql').default

//redis must be running
const server = new ws.Server({
    port: 4001,
    path: '/subscribe',
});

useServer(
    // from the previous step
    { schema: GraphQlSchema, roots: GraphQlSchema },
    server
);

console.log('Listening to port 4001');
@ziedHamdi
Copy link
Author

Can you please guide me from where to start my investigation, I'll post the answer if I find it

@nodkz
Copy link
Member

nodkz commented Jul 30, 2021

Nothing to do with roots just pass an empty object to it.
The schema already contains everything that is needed.

@nodkz
Copy link
Member

nodkz commented Jul 30, 2021

useServer(
    // from the previous step
    { schema: GraphQlSchema, roots: {} },
    server
);

@ziedHamdi
Copy link
Author

ziedHamdi commented Aug 5, 2021

Hi Paul,

Sorry for the late reply, I had issues with docker, I was unable to test.

I tried your solution today, the server starts flawlessly. However, I have an issue with the resolvers: 2nd and 3rd arguments are not passed anymore: in the following example arg2 and context are both null

ComplaintTC.addFields({
    userActionRecap: {
        type: ActionRecapList,
        resolve: async (complaint, arg2, context) => {
            logger.debug( "complaint :", complaint, "arg2 : ", arg2, " context: ", context )
            const userRecap = await findUserActionRecaps(complaint, arg2, context);
            return userRecap
        }
    }
})

Here's my server init code for refrerence:

import ws from 'ws'; // yarn add ws
import { useServer } from 'graphql-ws/lib/use/ws';
const GraphQlSchema = require('./graphql').default

const server = new ws.Server({
    port: 4001,
    path: '/subscribe',
});

useServer(
    { schema: GraphQlSchema, roots: {} },
    server
);

@ziedHamdi
Copy link
Author

ziedHamdi commented Aug 11, 2021

Hi Paul,

I received this answer from the GraphQL-ws team:
enisdenjo/graphql-ws#223 (comment)

This is what he writes:
To provide a contextValue to graphql resolvers, you have to supply the context option. Please check the "ws server usage with custom context value" recipe.

The roots option is a convenience that gets injected to the rootValue during graphql's execute or subscribe by operation type.
graphql-ws/src/server.ts

Line 738 in f2a92af

execArgs.rootValue = roots?.[operationAST.operation];

In your case (from the referenced issue), the roots option can be completely omitted as it does nothing anyway.
arg2 (aka args) are arguments provided to the field in the GraphQL query. You supply them from the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants