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

Subscription Server returns "Must provide document." for Apollo v3 #5852

Closed
janandreschweiger opened this issue Oct 31, 2021 · 14 comments
Closed

Comments

@janandreschweiger
Copy link

Our firm migrated yesterday to Apollo v3 (apollo-server-express v3.4.0).

Unfortunately, subscriptions return the following response:

{ "message": "Must provide document" }

We made following observations:

  1. This is a server issue, as we experience this error with Apollo Studio too.
  2. The connection is established, as the server fires the onConnect event.
  3. The request fires the onOperation event with the right message.
  4. The subscribe event of the subscription is however never called.

This is my index.mjs:

import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import http from 'http';
import cors from 'cors';
import { execute, subscribe } from 'graphql';
import { graphqlUploadExpress } from 'graphql-upload';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { makeExecutableSchema } from '@graphql-tools/schema';

import typeDefs from './moduleHandler/graphqlModule/typeDefs.mjs';
import resolvers from './moduleHandler/graphqlModule/resolvers.mjs';


const expressApp = express();
const corsOptions = {
  origin: (origin, callback) => {
    callback(null, true);
    return;
  },
  credentials: true
};

expressApp.use(cors(corsOptions));
expressApp.use(graphqlUploadExpress({ }));

const httpServer = http.createServer(expressApp);

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

const subscriptionServer = SubscriptionServer.create({
  schema,
  execute,
  subscribe,
  onConnect: async (connectionParams, webSocket) => { // subscriptions
    console.log('on Connect')  // fires
    return { }
  },
  onOperation: async (_message, params, ws) => {
    console.log(_message)  // fires
    /* prints my subcription:
        {
   	id: '23',
	   type: 'start',
	   payload: {
	     query: 'subscription Subscription($category: ConversationOverviewCategory!) {\n' +
	       '  conversationOverviewListen(category: $category) {\n' +
	       '    item {\n' +
	       '      base { _id }\n' +
	       '    }\n' +
	       '  }\n' +
	       '}',
	     operationName: 'Subscription',
	     variables: { category: 'ALL' }
	   }
	 }
    */
    return params;
  }, {
  server: httpServer,
  path: '/subscriptions'
});

const apolloServer = new ApolloServer({
  schema,
  playground: false,
  resolverValidationOptions: {
    requireResolversForResolveType: false
  },
  formatError: err => {
    console.log(err); // not called
  },
  plugins: [{
    async serverWillStart() {
      return {
        async drainServer() {
          console.log('bye');  // not called
          subscriptionServer.close();
        }
      };
    }
  }],
  uploads: false,
  path: '/graphql'
});

await apolloServer.start();
apolloServer.applyMiddleware({ app: expressApp, cors: corsOptions, path: '/graphql' });

httpServer.listen(80, async () => {
  httpServer.setTimeout(1 * 60000) // 1 min
  console.log('started');  // called
});

My resolvers.mjs:

export default {
  Subscription: {
   conversationOverviewListen: {
    subscribe: () => console.log("never called"),
  }
};

We are happy for any help. Thank you!!

@nils-tahler
Copy link

Could you please have a look on this one @glasser? I have the exact same issue.

@nero-nazok
Copy link

Same here. I don't know what I am doing wrong. I also have found this issue on stackoverflow:
https://stackoverflow.com/questions/69778672/apollo-v3-graphql-subscription-error-must-provide-document

@glasser
Copy link
Member

glasser commented Nov 1, 2021

Can you make this available as a full cloneable repository or codesandbox.io sandbox? It's a lot easier to debug when we know we're running the same version of all dependencies due to having the package.json and lock files included.

@janandreschweiger
Copy link
Author

janandreschweiger commented Nov 1, 2021

Just use this official example and upgrade apollo-server-express to the latest version:
https://codesandbox.io/s/github/apollographql/docs-examples/tree/main/apollo-server/v3/subscriptions?fontsize=14&hidenavigation=1&initialpath=/graphql&theme=dark&file=/index.js

It works for 3.2.1 that is used by the sample.

@glasser
Copy link
Member

glasser commented Nov 1, 2021

Sorry, are you saying that this works in 3.2.1 and not in later versions? It's a lot easier for me to follow if there's no creativity involved (ie, literally just npm i && npm start demonstrates the issue)

@glasser
Copy link
Member

glasser commented Nov 1, 2021

(I also don't believe that there was an Apollo Server 3.2.1? This sort of question is why full reproductions help.)

@janandreschweiger
Copy link
Author

Sorry 3.1.2, it is the version used by the sample. Just change the version of apollo-server-express to "^3.4.0" in the package.json, which is the latest version.

@janandreschweiger
Copy link
Author

You will see that it works for 3.1.2, but not for the latest version 3.4.0.
But again, I am completely fine now. I just downgraded apollo-server-express. I just raised this issue in case you're interested in fixing it. If you have other priorities, we can just close it.

@glasser
Copy link
Member

glasser commented Nov 1, 2021

I upgraded that example to apollo-server-express 3.4.0 and was able to successfully run a subscription via Explorer.

I'm sure this is a real problem because three of you have reported it, but I can't make any progress without a reproduction that doesn't require me to apply any creativity or hope that our package lock files manage to be exactly identical due to sheer luck. Looking forward to reopening this issue when it has a reproduction.

@glasser glasser closed this as completed Nov 1, 2021
@glasser
Copy link
Member

glasser commented Nov 1, 2021

(I bet it is not a coincidence that this is right after graphql@16 was released — did you install graphql@16
too?)

@janandreschweiger
Copy link
Author

janandreschweiger commented Nov 1, 2021

Yes

@janandreschweiger
Copy link
Author

You're right, I made some tests: It fails with graphql 16, but works for graphql 15.5.1.

@glasser
Copy link
Member

glasser commented Nov 2, 2021

@janandreschweiger Try upgrading subscriptions-transport-ws to v0.11.0 and see if that helps.

@janandreschweiger
Copy link
Author

Thank you @glasser, this solved my issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants