newrelic / newrelic-node-apollo-server-plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apollo Server 4 Compatibility

jorgechacons opened this issue · comments

Description

As part of the Apollo Server 4 Migration, all the plugins needs to be updated to use the nedw imports listed here

Expected Behavior

New relic apollo server plugin is compatible with the new apollo server 4 release.

Steps to Reproduce

Create an apollo server using as4 lib and add your latest version of the new relic apollo server plugin

Thanks for the heads up @jorgechacons. I will take an assessment on what it'll take to support and get back to you.

@jorgechacons have you tried using the plugin with apollo server 4? A lot has changed with apollo server 4 but the method to register a plugin has stayed the same. I haven't hit any issues thus far. I'm working on adding a test suite for now but I haven't had to change any plugin code whatsoever.

All attempts at reproducing this issue failed, or not enough information was available to reproduce the issue. Reading the code produces no clues as to why this behavior would occur. If more information appears later, please reopen the issue.

Thanks for adding the tests @bizob2828. Even with the tests, I can't quite figure out how to get the NR loaded appropriately. Can you perhaps provide an example?

With all sorts of attempts, I keep running into mostly issues with types (e.g.  Type 'GraphQLServerContext' is missing the following properties from type 'GraphQLServiceContext': schemaHash, serverlessFramework).

Thanks for adding the tests @bizob2828. Even with the tests, I can't quite figure out how to get the NR loaded appropriately. Can you perhaps provide an example?

With all sorts of attempts, I keep running into mostly issues with types (e.g.  Type 'GraphQLServerContext' is missing the following properties from type 'GraphQLServiceContext': schemaHash, serverlessFramework).

@sebastianmulders can you share a code snippet or repo? It'd be easier for me to help with that.

Thanks for your quick response @bizob2828. When working on an example repo, I was surprised to find out that I could not reproduce the issue directly. After some research, I found that the culprit seems to be apollo-server-base-plugin. Without that, it works fine.

I installed that because of this Yarn warn:
warning " > @newrelic/apollo-server-plugin@2.0.1" has unmet peer dependency "apollo-server-plugin-base@>=0.10.1".. To resolve this warning, I simply did a yarn add apollo-server-plugin-base. And when doing that, it results in TS2322: Type 'ApolloServerPlugin<BaseContext>' is not assignable to type 'ApolloServerPlugin<MyContext>'.

See this example repo.

Edit:
I realise now this can simply be solved by doing: const newRelicPlugin = createNewRelicPlugin() as ApolloServerPlugin<MyContext>;. I'm sure I tried that before without any luck, but seems to work now. Sorry for the inconvenience! Not sure what I did wrong here before.

@sebastianmulders thanks for the repro case. I figured out what the issue is but I'm not sure the right way to fix. Either way this will require us to make a release. Basically the issue is that in apollo 4 the interface for plugins changed. Here is a diff of the fix but I need to figure out how to make it work for apollo server 3 as well.

diff --git a/index.d.ts b/index.d.ts
index ef24412..00b3764 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,4 +1,5 @@
-import type { ApolloServerPlugin } from "apollo-server-plugin-base";
+//import type { ApolloServerPlugin } from "apollo-server-plugin-base";
+import type { ApolloServerPlugin } from "@apollo/server";
 
 export type NRPluginConfig = {
   captureScalars?: boolean;
diff --git a/package.json b/package.json
index 975e91d..a8a051a 100644
--- a/package.json
+++ b/package.json
@@ -57,7 +57,7 @@
     "tsd": "^0.18.0"
   },
   "peerDependencies": {
-    "apollo-server-plugin-base": ">=0.10.1",
+    "@apollo/server": ">=4.0.0",
     "newrelic": ">=6.13.0"
   },
   "tsd": {

Sorry for my late response @bizob2828 and thanks @sebastianmulders , yes, I think it would be more of a type mismatch issue, maybe it should be as simple as updating the imports on your plugin, I fixed some custom plugins on my end, doing that

@jorgechacons and @sebastianmulders I'm pretty confident in my fix but do you think you can verify #228 please?

If you pull this branch and run

npm ci
npm pack

Then copy the .tgz to your apollo app and run npm i newrelic-apollo-server-plugin-2.0.1.tgz and run it should work now. That also assumes you update your code slightly here:

import { ApolloServerPlugin, ApolloServer } from '@apollo/server';
import createNewRelicPlugin from '@newrelic/apollo-server-plugin';

const newRelicPlugin = createNewRelicPlugin<ApolloServerPlugin>({}) 
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [
    newRelicPlugin,
  ],
});

@bizob2828 can confirm this works! Good solution, thanks.

@sebastianmulders and @jorgechacons please upgrade to 2.1.0. This has a fix for using the plugin with typescript on Apollo 4