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

Generate input types (and more fixes) #476

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
2 changes: 1 addition & 1 deletion src/cmd/generators/stores/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export declare class ${storeName} extends ${queryClass}<${_data}, {}> {

export const ${globalStoreName}: ${storeName}

export declare const load_${artifactName}: (params: QueryStoreFetchParams<{}>) => Promise<${storeName}>
export declare const load_${artifactName}: (params: QueryStoreFetchParams<${_data}, {}>) => Promise<${storeName}>

export default ${storeName}
`
Expand Down
9 changes: 6 additions & 3 deletions src/cmd/generators/stores/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export const ${globalStoreName} = new ${storeName}()
export default ${globalStoreName}
`

// type definitions
AlecAivazis marked this conversation as resolved.
Show resolved Hide resolved
const typeDefs = `import type { ${artifactName}$input, ${artifactName}$result, MutationStore } from '$houdini'
const _input = `${artifactName}$input`
const _data = `${artifactName}$result`

export declare class ${storeName} extends MutationStore<${artifactName}$result | undefined, ${artifactName}$input>{
// the type definitions for the store
const typeDefs = `import type { ${_input}, ${_data}, MutationStore } from '$houdini'

export declare class ${storeName} extends MutationStore<${_data} | undefined, ${_input}>{
constructor() {}
}

Expand Down
14 changes: 7 additions & 7 deletions src/cmd/generators/stores/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('basic store', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -113,7 +113,7 @@ test('store with required variables', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -163,7 +163,7 @@ test('store with nullable variables', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -213,7 +213,7 @@ test('store with non-null variables with default value', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -269,7 +269,7 @@ test('forward cursor pagination', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -325,7 +325,7 @@ test('backwards cursor pagination', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down Expand Up @@ -377,7 +377,7 @@ test('offset pagination', async function () {

export async function load_TestQuery(params) {
const store = new TestQueryStore()

await store.fetch(params)

return {
Expand Down
21 changes: 6 additions & 15 deletions src/cmd/generators/stores/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ${storeName} extends ${queryClass} {

export async function load_${artifactName}(params) {
const store = new ${storeName}()

await store.fetch(params)

return {
Expand All @@ -65,28 +65,19 @@ export const ${globalStoreName} = new ${storeName}()
export default ${globalStoreName}
`

// look for the operation
const operations = doc.document.definitions.filter(
({ kind }) => kind === graphql.Kind.OPERATION_DEFINITION
) as graphql.OperationDefinitionNode[]
const inputs = operations[0]?.variableDefinitions
const withVariableInputs = inputs && inputs.length > 0
const variableInputsType = withVariableInputs ? `${artifactName}$input` : 'null'

const _input = `${artifactName}$input`
const _data = `${artifactName}$result`

// type definitions
const typeDefs = `import type { ${_data}, ${queryClass}, ${
variableInputsType ? `${artifactName}$input` : ''
}, QueryStoreFetchParams} from '$houdini'
// the type definitions for the store
const typeDefs = `import type { ${_input}, ${_data}, ${queryClass}, QueryStoreFetchParams} from '$houdini'

export declare class ${storeName} extends ${queryClass}<${_data}, ${variableInputsType}> {
export declare class ${storeName} extends ${queryClass}<${_data}, ${_input}> {
constructor() {}
}

export const ${globalStoreName}: ${storeName}

export declare const load_${artifactName}: (params: QueryStoreFetchParams<${_data}, ${variableInputsType}>) => Promise<{${artifactName}: ${storeName}}>
export declare const load_${artifactName}: (params: QueryStoreFetchParams<${_data}, ${_input}>) => Promise<{${artifactName}: ${storeName}}>

export default ${storeName}
`
Expand Down
14 changes: 5 additions & 9 deletions src/cmd/generators/stores/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ export const ${globalStoreName} = new ${storeName}()
export default ${globalStoreName}
`

// look for the operation
const operations = doc.document.definitions.filter(
({ kind }) => kind === graphql.Kind.OPERATION_DEFINITION
) as graphql.OperationDefinitionNode[]
const inputs = operations[0]?.variableDefinitions
const withVariableInputs = inputs && inputs.length > 0
const VariableInputsType = withVariableInputs ? `${artifactName}["input"]` : 'null'
const _input = `${artifactName}$input`
const _data = `${artifactName}$result`

// the type definitions for the store
const typeDefs = `import type { ${artifactName}, ${artifactName}$result, MutationStore } from '$houdini'
const typeDefs = `import type { ${_input}, ${_data}, SubscriptionStore } from '$houdini'

export declare class ${storeName} extends MutationStore<${artifactName}$result | undefined, ${VariableInputsType}> {
export declare class ${storeName} extends SubscriptionStore<${_data} | undefined, ${_input}> {
constructor() {}
}

Expand Down
12 changes: 7 additions & 5 deletions src/cmd/generators/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ async function generateOperationTypeDefs(
readonlyProperty(
AST.tsPropertySignature(
AST.stringLiteral('input'),
AST.tsTypeAnnotation(
hasInputs
? AST.tsTypeReference(AST.identifier(inputTypeName))
: AST.tsNullKeyword()
)
AST.tsTypeAnnotation(AST.tsTypeReference(AST.identifier(inputTypeName)))
)
),
readonlyProperty(
Expand Down Expand Up @@ -270,6 +266,12 @@ async function generateOperationTypeDefs(
)
)
)
} else {
body.push(
AST.exportNamedDeclaration(
AST.tsTypeAliasDeclaration(AST.identifier(inputTypeName), AST.tsNullKeyword())
)
)
}
}

Expand Down
48 changes: 36 additions & 12 deletions src/cmd/generators/typescript/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -256,6 +256,8 @@ describe('typescript', function () {
readonly firstName: string
} | null
};

export type Query$input = null;
`)
})

Expand All @@ -281,7 +283,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -290,6 +292,8 @@ describe('typescript', function () {
readonly firstName: string
} | null)[] | null
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -356,7 +360,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type MyTestQuery = {
readonly "input": null,
readonly "input": MyTestQuery$input,
readonly "result": MyTestQuery$result | undefined
};

Expand All @@ -369,6 +373,8 @@ describe('typescript', function () {
readonly __typename: "User"
}))
};

export type MyTestQuery$input = null;
`)
})

Expand Down Expand Up @@ -547,7 +553,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -558,6 +564,8 @@ describe('typescript', function () {
}
} | null
};

export type Query$input = null;
`)
})

Expand All @@ -583,7 +591,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -595,6 +603,8 @@ describe('typescript', function () {
}
}
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -628,7 +638,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -641,6 +651,8 @@ describe('typescript', function () {
readonly __typename: "Cat"
})))[]
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -674,7 +686,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -687,6 +699,8 @@ describe('typescript', function () {
readonly __typename: "Cat"
})) | null)[] | null
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -721,7 +735,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -736,6 +750,8 @@ describe('typescript', function () {
readonly __typename: "Cat"
})))[]
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -772,7 +788,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -786,6 +802,8 @@ describe('typescript', function () {
readonly __typename: "User"
})) | null)[] | null
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -833,7 +851,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -842,6 +860,8 @@ describe('typescript', function () {
readonly createdAt: Date
})[]
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -933,7 +953,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -943,6 +963,8 @@ describe('typescript', function () {
readonly nickname: string | null
} | null)[] | null)[]
};

export type Query$input = null;
`)
})

Expand Down Expand Up @@ -973,7 +995,7 @@ describe('typescript', function () {
})
).toMatchInlineSnapshot(`
export type Query = {
readonly "input": null,
readonly "input": Query$input,
readonly "result": Query$result | undefined
};

Expand All @@ -985,6 +1007,8 @@ describe('typescript', function () {
} | null
} | null
};

export type Query$input = null;
`)
})

Expand Down