mercurius-js / validation

Adds configurable validation support to Mercurius.
MIT License
29 stars 6 forks source link

Validation stop working if extendSchema is called. #39

Closed joshuaavalon closed 1 year ago

joshuaavalon commented 2 years ago

As title, if I use extendSchema after registering mercurius-validation, it no longer works.

${mercuriusValidation.graphQLTypeDefs}

type Query {
  foo(page: Pagination!): Int!
}

input Pagination {
  skip: Int @constraint(minimum: 0)
  take: Int @constraint(minimum: 1)
}
await fastify.register(mercurius, {
    schema,
    resolvers,
    path: "/graphql",
    graphiql: false
  });
await fastify.register(mercuriusValidation);

// From another plugin
fastify.graphql.extendSchema(schema);
jonnydgreen commented 2 years ago

Great spot! Would you like to send a PR to address this?

joshuaavalon commented 2 years ago

I have notice that it is able to refresh onGatewayReplaceSchema.

https://github.com/mercurius-js/validation/blob/6f8584016d601b7d1b3940ad0b144bb194dbb1c4/index.js#L19-L21

However, there are not any hooks on extendSchema. Unless, this plugin expose a function to force refresh, it needs mercurius to add a new hook.

jonnydgreen commented 2 years ago

I think adding an onExtendSchema system hook to mercurius could be pretty useful! Would you be interested in adding this as well?

@mcollina what do you think?

mcollina commented 2 years ago

I concur with your assessment!

stearm commented 1 year ago

I'm attempting to work on this, but I ran into a problem where extendSchema is defined outside of the fastifyGraphql function, making it impossible for me to access to context parameter that I need to call the hook handler. Am I missing something? Solved.