Access context from Apollo GraphQL mutation field directive

566 views Asked by At

I have an input type like this:

input PetGiraffe {
    name: String @addUserLastName
}

Inside the directive, I need access to the request's context, so that I can add the user's last name to the giraffe's name. Here's the relevant part of what I've got so far:

const addUserLastNameDirective = {
    typeDefs: gql`directive @addUserLastName on INPUT_FIELD_DEFINITION`,
    transformer: (schema: GraphQLSchema, directiveName = 'addUserLastName') => {
        return mapSchema(schema, {
            [MapperKind.INPUT_OBJECT_FIELD]: (fieldConfig, fieldName, typeName, schema) => {
                const directive = getDirective(schema, fieldConfig, directiveName)?.[0];
                if (directive) {
                    // Need context in here because the user is in the context.
                }
            },
        });
    },
};

For queries, I understand you can override the fieldConfig.resolve method and get access to the context that way. But if I try that with this mutation, it throws: field has a resolve property, but Input Types cannot define resolvers.

The closest I could find was this from the graphql-tools docs, but that doesn't solve my problem of accessing the context.

0

There are 0 answers