Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Resolver context type - subtype can be optional",
"packageName": "@graphitation/cli",
"email": "77059398+vejrj@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Resolver context type - subtype can be optional",
"packageName": "@graphitation/ts-codegen",
"email": "77059398+vejrj@users.noreply.github.com",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/cli/src/supermassive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function supermassive(): Command {
)
.option(
"--context-type-extensions-file [contextTypeExtensionsFile]",
"Describes context types and their import paths. Used to generate resolver context type extensions. The file must be defined in the following format: { baseContextTypePath?: string, baseContextTypeName?: string, contextTypes: { [namespace: string]: { [type: string]: { importNamespaceName?: string, importPath: string, typeName: string }}}",
"Describes context types and their import paths. Used to generate resolver context type extensions. The file must be defined in the following format: { baseContextTypePath?: string, baseContextTypeName?: string, contextTypes: { [namespace: string]: { [type: string]: { importNamespaceName?: string, importPath: string, typeName: string, optional?: boolean }}}",
)
.option(
"--generate-resolver-map",
Expand Down
16 changes: 14 additions & 2 deletions packages/ts-codegen/src/__tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe(generateTS, () => {
importPath: "@package/whatever-state-machine",
typeName: "whatever",
},
optionalWhatever: {
importPath: "@package/whatever-state-machine",
typeName: "optionalWhatever",
optional: true,
},
"different-whatever": {
importNamespaceName: "DifferentWhateverStateMachineType",
importPath: "@package/different-whatever-state-machine",
Expand All @@ -31,6 +36,7 @@ describe(generateTS, () => {
importNamespaceName: "PostStateMachineType",
importPath: "@package/post-state-machine",
typeName: 'PostStateMachineType["post"]',
optional: true,
},
node: {
importNamespaceName: "NodeStateMachineType",
Expand Down Expand Up @@ -96,7 +102,10 @@ describe(generateTS, () => {
}

type User @context(uses: { managers: ["user"] }) {
id: ID! @context(uses: { managers: ["id-user", "user"] })
id: ID!
@context(
uses: { managers: ["id-user", "user", "optionalWhatever"] }
)
name: String
messagesWithAnswersNonRequired: [[Message]]
messagesWithAnswersRequired: [[Message]]!
Expand Down Expand Up @@ -149,6 +158,7 @@ describe(generateTS, () => {
"managers": [
"id-user",
"user",
"optionalWhatever",
],
},
"messagesNonRequired": {
Expand Down Expand Up @@ -245,6 +255,7 @@ describe(generateTS, () => {
import type { DefaultContextType } from "@package/default-context";
import type { MessageStateMachineType } from "@package/message-state-machine";
import type { UserStateMachineType } from "@package/user-state-machine";
import type { optionalWhatever } from "@package/whatever-state-machine";
import type { PostStateMachineType } from "@package/post-state-machine";
export declare namespace Post {
export interface Resolvers {
Expand Down Expand Up @@ -282,6 +293,7 @@ describe(generateTS, () => {
managers: {
"id-user": UserStateMachineType["id-user"];
"user": UserStateMachineType["user"];
"optionalWhatever"?: optionalWhatever;
};
}, info: ResolveInfo) => PromiseOrValue<string>;
export type name = (model: Models.User, args: {}, context: DefaultContextType & {
Expand Down Expand Up @@ -326,7 +338,7 @@ describe(generateTS, () => {
}, info: ResolveInfo) => PromiseOrValue<IterableOrAsyncIterable<Models.Message> | null | undefined>;
export type post = (model: Models.User, args: {}, context: DefaultContextType & {
managers: {
"post": PostStateMachineType["post"];
"post"?: PostStateMachineType["post"];
};
}, info: ResolveInfo) => PromiseOrValue<Models.Post | null | undefined>;
export type postRequired = (model: Models.User, args: {}, context: DefaultContextType & {
Expand Down
1 change: 1 addition & 0 deletions packages/ts-codegen/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type SubTypeItem = {
importNamespaceName?: string;
importPath: string;
typeName: string;
optional?: boolean;
};
};

Expand Down
9 changes: 6 additions & 3 deletions packages/ts-codegen/src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ export class TsCodegenContext {
factory.createIdentifier(namespace),
undefined,
factory.createTypeLiteralNode(
subTypes.map(({ subType, name }) => {
subTypes.map(({ subType, name, optional }) => {
return factory.createPropertySignature(
undefined,
factory.createIdentifier(`"${name}"`),
undefined,
optional
? factory.createToken(ts.SyntaxKind.QuestionToken)
: undefined,
factory.createTypeReferenceNode(
factory.createIdentifier(subType),
undefined,
Expand Down Expand Up @@ -236,7 +238,7 @@ export class TsCodegenContext {
}

return typeNames.reduce<
Record<string, { subType: string; name: string }[]>
Record<string, { subType: string; name: string; optional: boolean }[]>
>((acc, typeName) => {
const [namespaceName, subTypeName] = typeName.split(":");
if (!acc[namespaceName]) {
Expand Down Expand Up @@ -268,6 +270,7 @@ export class TsCodegenContext {
acc[namespaceName].push({
subType,
name: subTypeName,
optional: subTypeRawMetadata.optional || false,
});

return acc;
Expand Down