graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

directive on FIELD_DEFINITION doesn't get called when subscribe

Open UchihaYuki opened this issue 1 year ago • 0 comments

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • [ ] 2. A failing test has been provided
  • [ ] 3. A local solution has been provided
  • [ ] 4. A pull request is pending review

Describe the bug

To Reproduce Steps to reproduce the behavior:

extend type Subscription {
  authenticated: String @authenticated
}
const typeDefs = `
  directive @authenticated on FIELD_DEFINITION
`;

function transformer(openidConfigurationUrl: string) {
  return (schema: GraphQLSchema) =>
    mapSchema(schema, {
      [MapperKind.OBJECT_FIELD]: (
        fieldConfig: GraphQLFieldConfig<any, IamGraphqlServerContext, any>,
      ) => {
        const options = getDirective(schema, fieldConfig, "authenticated")?.[0];
        if (!options) return fieldConfig;

        const { resolve = defaultFieldResolver } = fieldConfig;
        fieldConfig.resolve = async (source, args, context, info) => {
          console.log("@authenticated");
          const idTokenPayload =
            await getIdTokenPayloadFromContext(openidConfigurationUrl)(context);
          if (!idTokenPayload) throw new Error401();

          context.idTokenPayload = idTokenPayload;

          return await resolve(source, args, context, info);
        };
        return fieldConfig;
      },
    });
}
  Subscription: {
    authenticated: {
      subscribe: async (_, __, context) => {
        console.log("subscribe authenticated", context.idTokenPayload, context.connectionParams);
        return (async function* () {
          for (const i of [0, 1, 2]) {
            yield { authenticated: "authenticated" };
          }
        })();
      },
    },
  },

Expected behavior console.log("@authenticated"); should be executed before console.log("subscribe authenticated", context.idTokenPayload, context.connectionParams); But console.log("@authenticated"); is executed after every emitted value

Environment:

  • OS: Windows 10
  • @graphql-tools/...: 10.3.1
  • NodeJS: 20.14.0

Additional context

UchihaYuki avatar Jul 08 '24 10:07 UchihaYuki