Skip to content

Commit

Permalink
fix: add MapperKind.MUTATION to createMapper fn
Browse files Browse the repository at this point in the history
This fix the usage of directives on input fields used on mutation args
  • Loading branch information
felipebergamin committed Sep 27, 2023
1 parent 28145db commit d5f0602
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/createSchemaMapperForVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export const createMapper = <T extends DirectiveLocation>(
visitor.visitQuery(query, schema, directiveName);
return query;
},
[MapperKind.MUTATION](mutation, schema): GraphQLObjectType {
// TODO: rename visitQuery to cover usage on mutations
visitor.visitQuery(mutation, schema, directiveName);
return mutation;
},
[MapperKind.OBJECT_TYPE](type, schema): GraphQLObjectType {
const [directive] = getDirective(schema, type, directiveName) ?? [];
if (!directive) return type;
Expand All @@ -35,8 +40,8 @@ export const createMapper = <T extends DirectiveLocation>(
typeName,
schema,
): GraphQLFieldConfig<unknown, unknown> {
// query fields will be handled by MapperKind.QUERY
if (typeName === 'Query') return fieldConfig;
// query and mutation fields will be handled by MapperKind.QUERY
if (typeName === 'Query' || typeName === 'Mutation') return fieldConfig;
const [directive] = getDirective(schema, fieldConfig, directiveName) ?? [];
if (!directive) return fieldConfig;
// eslint-disable-next-line no-param-reassign
Expand Down
63 changes: 63 additions & 0 deletions lib/foreignNodeId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,67 @@ ${validationDirectionEnumTypeDefs(capitalizedName)}
});
expect(result).toEqual({ data: rootValue });
});

it('should work when used on mutation inputs', async (): Promise<void> => {
const mockResolver = jest.fn().mockReturnValue('return value');
const typeName = 'MyType';
const decodedId = 'abc';
const encodedId = toNodeId('MyType', decodedId);
const schema = new ForeignNodeId().applyToSchema(
makeExecutableSchema({
resolvers: {
Mutation: {
testDirective: mockResolver,
},
Query: {
dummy: () => '',
},
},
typeDefs: [
...directiveTypeDefs,
gql`
input Input1 {
typeId: ID! @foreignNodeId(typename: "${typeName}")
}
type Query {
dummy: String
}
type Mutation {
testDirective(input: Input1): String
}
`,
],
}),
);
const source = print(gql`
mutation Testing($input: Input1!) {
testDirective(input: $input)
}
`);
const contextValue = ForeignNodeId.createDirectiveContext({
fromNodeId,
});

await graphql({
contextValue,
schema,
source,
variableValues: {
input: {
typeId: encodedId,
},
},
});
expect(mockResolver).toHaveBeenCalledTimes(1);
expect(mockResolver).toHaveBeenCalledWith(
undefined,
{
input: {
typeId: decodedId,
},
},
contextValue,
expect.any(Object),
);
});
});
67 changes: 66 additions & 1 deletion lib/pattern.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GraphQLSchema } from 'graphql';
import { GraphQLError, type GraphQLSchema } from 'graphql';
import { gql } from 'graphql-tag';
import { makeExecutableSchema } from '@graphql-tools/schema';

Expand All @@ -23,6 +23,9 @@ type RootValue = {
number?: number;
bool?: boolean;
obj?: { toString(): string };
directiveOnInput?: string | null;
directiveOnMutationArg?: string | null;
directiveOnMutationField?: string | null;
};

type ResultValue = Omit<RootValue, 'obj'> & {
Expand Down Expand Up @@ -54,6 +57,14 @@ const createSchema = ({
bool: Boolean @${name}${directiveArgs}
obj: SomeObj @${name}${directiveArgs}
}
input InputTypeWithDirective {
value: String! @${name}${directiveArgs}
}
type Mutation {
directiveOnInput(input: InputTypeWithDirective!): String
directiveOnMutationArg(input: String! @${name}${directiveArgs}): String
directiveOnMutationField(input: String): String @${name}${directiveArgs}
}
`,
],
}),
Expand All @@ -80,6 +91,60 @@ testEasyDirective({
)`,
name,
testCases: [
{
directiveArgs: '(regexp: "[a-z]+")',
operation:
'mutation TestMutation { directiveOnInput(input: { value: "987" }) }',
tests: [
{
expected: {
data: {
directiveOnInput: null,
},
errors: [new GraphQLError('Does not match pattern: /[a-z]+/')],
},
rootValue: {
directiveOnInput: 'Value returned',
},
},
],
},
{
directiveArgs: '(regexp: "[a-z]+")',
operation:
'mutation TestMutation { directiveOnMutationField(input: "abc 123") }',
tests: [
{
expected: {
data: {
directiveOnMutationField: null,
},
errors: [new GraphQLError('Does not match pattern: /[a-z]+/')],
},
rootValue: {
directiveOnMutationField: '1234',
},
},
],
},
{
directiveArgs: '(regexp: "[a-z]+")',
operation:
'mutation TestMutation { directiveOnMutationArg(input: "123456") }',
tests: [
{
expected: {
data: {
directiveOnMutationArg: null,
},
errors: [new GraphQLError('Does not match pattern: /[a-z]+/')],
},
rootValue: {
directiveOnMutationArg: '1234',
},
},
],
},
{
directiveArgs: '(regexp: "[a-z]+", flags: "i")',
operation: '{ test }',
Expand Down

0 comments on commit d5f0602

Please sign in to comment.