Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/directives not applied to type args #98

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/createSchemaMapperForVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SchemaMapper } from '@graphql-tools/utils';
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';
import type {
DirectiveLocation,
GraphQLArgument,
GraphQLFieldConfig,
GraphQLObjectType,
GraphQLSchema,
Expand All @@ -26,6 +27,17 @@ export const createMapper = <T extends DirectiveLocation>(
return mutation;
},
[MapperKind.OBJECT_TYPE](type, schema): GraphQLObjectType {
Object.values(type.getFields()).forEach(field => {
field.args.forEach(arg => {
const [directive] = getDirective(schema, arg, directiveName) ?? [];
if (!directive) return;
// eslint-disable-next-line no-param-reassign
visitor.args = directive;
Copy link
Contributor

@DDDKnightmare DDDKnightmare Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, but can't overwriting visitor.args cause problems ?

  • e.g.:
  • 1:
    • store the original value before the forEach loop
    • after the forEach loop, restore the original value, so the other mappers are not affected by the modification in the args
  • 2:
    • use a different visitor instance to visit the fields
  • 3:
    • use visitArgumentsWithDirectiveInObjectFields (same code as this block)

visitor.visitArgumentDefinition(arg as GraphQLArgument, {
field,
});
});
});
const [directive] = getDirective(schema, type, directiveName) ?? [];
if (!directive) return type;
// eslint-disable-next-line no-param-reassign
Expand Down
65 changes: 65 additions & 0 deletions lib/foreignNodeId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,71 @@ ${validationDirectionEnumTypeDefs(capitalizedName)}
expect(result).toEqual({ data: rootValue });
});

it('should decode arguments in type field argument', async (): Promise<void> => {
const schema = new ForeignNodeId().applyToSchema(
makeExecutableSchema({
resolvers: {
Query: {},
TestType: {
typeIds: (_, { ids }) => {
return ids;
},
},
},
typeDefs: [
...directiveTypeDefs,
gql`
type TestType {
typeIds(
ids: [ID!]! @foreignNodeId(typename: "TypeID")
otherArgs: String
): [String!]!
}
type Query {
testType: TestType!
unusedQuery: TestType!
}
`,
],
}),
);
const source = print(gql`
query MyQuery($typeIds: [ID!]!) {
testType {
typeIds(ids: $typeIds)
}
}
`);
const decodedIds = ['123', '345', '678', '910'];
const encodedIds = decodedIds.map(id => toNodeId('TypeID', id));
const variableValues = {
typeIds: encodedIds,
};
const rootValue = {
testType: {},
};

const contextValue = ForeignNodeId.createDirectiveContext({
fromNodeId,
});
const spy = jest.spyOn(contextValue, 'fromNodeId');
const result = await graphql({
contextValue,
rootValue,
schema,
source,
variableValues,
});
expect(spy).toHaveBeenCalledTimes(encodedIds.length);
expect(result).toEqual({
data: {
testType: {
typeIds: decodedIds,
},
},
});
});

it('should work when used on mutation inputs', async (): Promise<void> => {
const mockResolver = jest.fn().mockReturnValue('return value');
const typeName = 'MyType';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@profusion/apollo-validation-directives",
"version": "4.1.1",
"version": "4.1.2",
"description": "GraphQL directives to implement field validations in Apollo Server",
"author": "Gustavo Sverzut Barbieri <[email protected]>",
"license": "MIT",
Expand Down
Loading