n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
777 stars 126 forks source link

testkit addPlugin behavior does not match envelop's #2185

Open jdolle opened 5 months ago

jdolle commented 5 months ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow


Describe the bug

addPlugin behavior differs for createTestkit vs passing plugins into graphql-yoga's createYoga

To Reproduce Steps to reproduce the behavior:

import { useApolloTracing } from "@envelop/apollo-tracing";
import { handleStreamOrSingleExecutionResult } from "@envelop/core";

import { assertSingleExecutionValue, createTestkit } from "@envelop/testing";
import { makeExecutableSchema } from "@graphql-tools/schema";

const useExample = () => {
  return {
    onPluginInit({ addPlugin }) {
      addPlugin(useApolloTracing());
    },
    onExecute() {
      return {
        onExecuteDone(payload) {
          handleStreamOrSingleExecutionResult(payload, ({ result, args, setResult }) => {
            // during test, this will log without the trace
            // but during normal execution, this will log with the trace
            console.log(result);
          });
        }
      }
    },
  }
}

// JEST TEST
test("calls #track", async () => {
  const schema = makeExecutableSchema({
    typeDefs: `
      type Query {
        foo: String
      }
      `,
    resolvers: {
      Query: {
        foo: jest.fn().mockResolveValue("ok"),
      },
    },
  });
  const testInstance = createTestkit([useExample()], schema);
  const result = await testInstance.execute(`{ errors }`);
});

Expected behavior

test kit flow call order should match yoga server / envelop server. In both cases for the example above, the result should include the trace data.

Environment:

Additional context

If instead of implementing onExecute, I use addPlugin to add the same logic, it calls in the expected order from the testkit

EmrysMyrddin commented 5 months ago

Hi, thank you for raising this to our attention!

I have just added a test to our testing kit, and it appears the execution order is correct in both Envelop and Testkit. So the problem you are facing is probably not related to this.

In fact, from what I see, it's a bit strange that you are seeing traces in the the normal execution, since your plugin's hooks should be executed before useApolloTracing ones (and if I well remember, it adds the tracing in onExecuteDone too. So if it happens in normal execution, it means something else is actually putting this tracing information elsewhere. Are you sure you don't also have the tracing plugin directly in your yoga configuration?

Can you provide a minimal reproduction with a Stackblitz or a repo we can clone?