-
Notifications
You must be signed in to change notification settings - Fork 2k
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
usage reporting doesn't work with old graphqlExpress middleware in apollo-server v2.18 #4588
Comments
While I'm here I note that legacyOptions.ts isn't copying |
This error happens if you try to use usage reporting in AS v2.18 and you’re using the old plain middleware API (just calling For example, the reporting user had this code returning an express middleware:
This doesn't call serverWillStart plugins! A fix for this user was to change to:
But we should make sure the experience is better for folks using these old modules. Possible solutions include one or more of:
Unclear if it's OK for us to just decide that you have to use a slightly more modern API to do usage reporting. |
Heh, the old middlewares aren't exported. So I think I'll just improve the error to link to here. |
requestDidStartHandler is not a function
with AS v2.18This error generally should arise when people try to use the unexported AS 1.x API instead of the 2.x API. (If we find other cases that trigger this error we can update the linked issue.) Fixes #4588.
@glasser How does that apply to import {ApolloServer} from 'apollo-server-express'
const server = new ApolloServer({
schema,
plugins: [],
engine: {
reportSchema: false,
apiKey: apolloApiKey, // apolloApiKey is undefined in unit tests
},
})
// ...
import {createTestClient} from 'apollo-server-testing'
const {query} = createTestClient(server)
const res = await query({
query: gql`...`
})
}) Edit I found out that my issue in CI only was due to the fact that my CI exposes a variable I must say that this behavior is dangerous and took me a while to debug as I expected that setting Leaving this here as it may help somebody else. |
Hi, I see your concern. It would make sense if setting I think having The main impact of APOLLO_KEY is enabling usage reporting (schema reporting is not on by default); as of v2.18 you can explicitly disable the plugin in your tests with https://www.apollographql.com/docs/apollo-server/api/plugin/usage-reporting/#disabling-the-plugin |
Thank you very much for the detailed response @glasser. I think it's great that this lives in this thread as it is linked in Apollo's error people will experience if they make the same mistake as me. Disabling the plugin is very useful indeed. For my very specific issue with APOLLO_KEY set in CI (for deployments), I chose to simply remove APOLLO_KEY in my test setup and avoid having // jest.setup.ts
delete process.env.APOLLO_KEY |
(This issue is linked from an error message, so the description has been updated to be relevant.)
In some cases, the Apollo usage reporting plugin (which sends information on how your server is being used to Apollo's servers to power Apollo Studio) can find itself in a state where it's serving a GraphQL request without ever having been properly started up.
The main reason this can happen is if you don't use supported
ApolloServer
2.x APIs to connect your ApolloServer to your web server. For example, let's say you've written this code:Note that this is importing the function graphqlExpress from the unsupported
dist
subdirectory ofapollo-server-express
;graphqlExpress
is a function that was part of theapollo-server
1.x API (before theApolloServer
class existed) but is not part of the 2.x API.When you use this internal function, the full Apollo Server lifecycle doesn't take place, and so usage reporting doesn't work properly. You should instead use use the
applyMiddleware
orgetMiddleware
methods onApolloServer
, which properly start your server's plugin lifecycle. Note that by defaultapplyMiddleware
adds cors and body-parser middlewares, so to maintain the same behavior, you can disable them:The text was updated successfully, but these errors were encountered: