mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify

Home Page:https://mercurius.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Case for a new hook

Ericnr opened this issue · comments

I want to be able to modify a query's arguments before it gets passed to the resolver. I tried doing this in the preExecution hook but it seems I cant change the type of variables since variables gets type-checked after the hook.

This is a code example using Pothos of what I want to make possible

gqlBuilder.mutationField('completeTodo', (t) =>
  t.fieldWithInput({
    type: GqlTodo,
    input: { 
      todoId: t.input.string({
        extensions: { parse: (todoId) => fromGlobalId(todo) },
      }),
    },
    resolve: async (_, { input }, { db, checkAuth }) => {
      const viewer = await checkAuth();

      const todo = await db.byId('todo', input.todoId); // todoId is an already decoded relay global id

      const completedTodo = await completeTodo(db, todo.id, viewer.id);

      return completedTodo;
    },
  })
);

I expected this to work using the preExecution hook, but if I turn todoId into an int in that hook I get the error

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$input\" got invalid value \"1\" at \"input.id\"; Int cannot represent non-integer value: \"1\"",
      "locations": [
        {
          "line": 1,
          "column": 12
        }
      ]
    }
  ]
}

So I believe this is a case for a new hook that would run before this type-checking occurs.

I just realized this lib doesn't implement the graphql executor itself which is what does the type-checking, so this is not possible. I figure this can be solved at resolver level, so I'd be looking at Pothos plugins. You can close this issue at will!

No worries!