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

Forward directive arguments params to context type #9669

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/shaggy-roses-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-codegen/testing": minor
"@graphql-codegen/typescript-resolvers": minor
"@graphql-codegen/visitor-plugin-common": minor
---

Forward directive arguments params to context type
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { getRootTypeNames } from '@graphql-tools/utils';
import autoBind from 'auto-bind';
import {
ASTNode,
ObjectValueNode,
ValueNode,
DirectiveDefinitionNode,
EnumTypeDefinitionNode,
FieldDefinitionNode,
Expand Down Expand Up @@ -51,6 +53,48 @@ import {
} from './utils.js';
import { OperationVariablesToObject } from './variables-to-object.js';

// converts an ObjectValueNode to the JS object it represents
const objectNodeToObject = (objectNode: ObjectValueNode) => {
const { fields } = objectNode;
return fields.reduce((acc, field) => {
// for some reason this does not seem to match the type
const fieldName = field.name as unknown as string;
robertherber marked this conversation as resolved.
Show resolved Hide resolved
return {
...acc,
[fieldName]: valueNodeToValue(field.value),
};
}, {});
};

// converts an ValueNode to the JS value it represents
const valueNodeToValue = (value: ValueNode): unknown => {
if (value.kind === 'StringValue') {
return value.value;
}
if (value.kind === 'ObjectValue') {
return objectNodeToObject(value);
}
if (value.kind === 'ListValue') {
return value.values.map(valueNodeToValue);
}
if (value.kind === 'NullValue') {
return null;
}
if (value.kind === 'BooleanValue') {
return value.value;
}
if (value.kind === 'IntValue') {
return parseInt(value.value, 10);
}
if (value.kind === 'FloatValue') {
return parseFloat(value.value);
}
if (value.kind === 'EnumValue') {
return value.value;
}
return null;
};

export interface ParsedResolversConfig extends ParsedConfig {
contextType: ParsedMapper;
fieldContextTypes: Array<string>;
Expand Down Expand Up @@ -1490,7 +1534,15 @@ export class BaseResolversVisitor<
const name = directive.name as unknown as string;
const directiveMap = this._directiveContextTypesMap[name];
if (directiveMap) {
contextType = `${directiveMap.type}<${contextType}>`;
const args = directive.arguments?.reduce((prev, { name, value }) => {
// for some reason this does not seem to match the type
const argumentName = name as unknown as string;
return {
...prev,
[argumentName]: valueNodeToValue(value),
};
}, {});
contextType = `${directiveMap.type}<${contextType}, ${JSON.stringify(args)}>`;
}
}
return contextType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Scalars = {
export type MyType = {
__typename?: 'MyType';
foo: Scalars['String']['output'];
bar: Scalars['String']['output'];
otherType?: Maybe<MyOtherType>;
withArgs?: Maybe<Scalars['String']['output']>;
unionChild?: Maybe<ChildUnion>;
Expand Down Expand Up @@ -234,12 +235,15 @@ export type MyDirectiveDirectiveArgs = {

export type MyDirectiveDirectiveResolver<Result, Parent, ContextType = any, Args = MyDirectiveDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type AuthenticatedDirectiveArgs = { };
export type AuthenticatedDirectiveArgs = {
role?: Maybe<Scalars['String']['input']>;
};

export type AuthenticatedDirectiveResolver<Result, Parent, ContextType = any, Args = AuthenticatedDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type MyTypeResolvers<ContextType = any, ParentType = ResolversParentTypes['MyType']> = ResolversObject<{
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -493,12 +497,15 @@ export type MyDirectiveDirectiveArgs = {

export type MyDirectiveDirectiveResolver<Result, Parent, ContextType = any, Args = MyDirectiveDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type AuthenticatedDirectiveArgs = { };
export type AuthenticatedDirectiveArgs = {
role?: Types.Maybe<Types.Scalars['String']['input']>;
};

export type AuthenticatedDirectiveResolver<Result, Parent, ContextType = any, Args = AuthenticatedDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = ResolversObject<{
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Types.Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Types.Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<Types.MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Types.Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -628,6 +635,7 @@ export type Scalars = {
export type MyType = {
__typename?: 'MyType';
foo: Scalars['String']['output'];
bar: Scalars['String']['output'];
otherType?: Maybe<MyOtherType>;
withArgs?: Maybe<Scalars['String']['output']>;
unionChild?: Maybe<ChildUnion>;
Expand Down Expand Up @@ -838,12 +846,15 @@ export type MyDirectiveDirectiveArgs = {

export type MyDirectiveDirectiveResolver<Result, Parent, ContextType = any, Args = MyDirectiveDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type AuthenticatedDirectiveArgs = { };
export type AuthenticatedDirectiveArgs = {
role?: Maybe<Scalars['String']['input']>;
};

export type AuthenticatedDirectiveResolver<Result, Parent, ContextType = any, Args = AuthenticatedDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;

export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = ResolversObject<{
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down
5 changes: 5 additions & 0 deletions packages/plugins/typescript/resolvers/tests/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ describe('ResolversTypes', () => {
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1332,6 +1333,7 @@ describe('ResolversTypes', () => {
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1413,6 +1415,7 @@ describe('ResolversTypes', () => {
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1483,6 +1486,7 @@ describe('ResolversTypes', () => {
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1553,6 +1557,7 @@ describe('ResolversTypes', () => {
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down
19 changes: 16 additions & 3 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export type ResolverAuthenticated<TResult, TParent = {}, TContext = {}, TArgs =
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: ResolverAuthenticated<ResolversTypes['String'], ParentType, ContextType>;
bar?: ResolverAuthenticated<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -485,6 +486,7 @@ export type ResolverAuthenticated<TResult, TParent = {}, TContext = {}, TArgs =
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: ResolverAuthenticated<ResolversTypes['String'], ParentType, ContextType>;
bar?: ResolverAuthenticated<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -916,6 +918,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -991,6 +994,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1064,6 +1068,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = MyCustomCtx, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1155,6 +1160,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = MyCustomCtx, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1230,6 +1236,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = MyCustomCtx, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1304,6 +1311,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = ContextType, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1379,6 +1387,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = ContextType, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1440,6 +1449,7 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, ContextTypeOne>;
bar?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, SpecialContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down Expand Up @@ -1468,7 +1478,8 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;

expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType>>;
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType, {}>>;
bar?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType, {"role":"admin"}>>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand All @@ -1492,7 +1503,8 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;

expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = MyCustomCtx, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType>>;
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType, {}>>;
bar?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType, {"role":"admin"}>>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand All @@ -1516,7 +1528,8 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;

expect(result.content).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['MyType'] = ResolversParentTypes['MyType']> = {
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextTypeOne>>;
foo?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextTypeOne, {}>>;
bar?: Resolver<ResolversTypes['String'], ParentType, AuthenticatedContext<ContextType, {"role":"admin"}>>;
otherType?: Resolver<Maybe<ResolversTypes['MyOtherType']>, ParentType, ContextType>;
withArgs?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeWithArgsArgs, 'arg2'>>;
unionChild?: Resolver<Maybe<ResolversTypes['ChildUnion']>, ParentType, ContextType>;
Expand Down
Loading