diff --git a/src/visitor.ts b/src/visitor.ts index 6c80500..607feaf 100644 --- a/src/visitor.ts +++ b/src/visitor.ts @@ -67,6 +67,7 @@ export class SWRVisitor extends ClientSideBaseVisitor< public get sdkContent(): string { const allPossibleActions = this._operationsToInclude + .filter((o) => o.operationType === 'Query') .map((o) => { const optionalVariables = !o.node.variableDefinitions || diff --git a/tests/swr.spec.ts b/tests/swr.spec.ts index c2ae6b2..4764e6f 100644 --- a/tests/swr.spec.ts +++ b/tests/swr.spec.ts @@ -155,6 +155,86 @@ async function test() { } }; } +export type SdkWithHooks = ReturnType;`) + }) + + it('Should generate the output from which mutation operation has been removed', async () => { + const config: PluginsConfig = {} + const document = parse(/* GraphQL */ ` + query feed { + feed { + id + commentCount + repository { + owner { + avatar_url + } + } + } + } + query feed2($v: String!) { + feed { + id + } + } + query feed3($v: String) { + feed { + id + } + } + query feed4($v: String! = "TEST") { + feed { + id + } + } + mutation submitComment( + $repoFullName: String! + $commentContent: String! + ) { + submitComment( + repoFullName: $repoFullName + commentContent: $commentContent + ) { + ...CommentsPageComment + } + } + fragment CommentsPageComment on Comment { + id + postedBy { + login + html_url + } + createdAt + content + } + `) + const docs = [{ location: '', document }] + + const content = (await plugin(schema, docs, config, { + outputFile: 'graphql.ts', + })) as Types.ComplexPluginOutput + + const usage = basicUsage + const output = await validate(content, config, docs, schema, usage) + expect(output) + .toContain(`export type Sdk = ReturnType;export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { + const sdk = getSdk(client, withWrapper); + return { + ...sdk, + useFeed(key: SWRKeyInterface, variables?: FeedQueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed(variables), config); + }, + useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed2(variables), config); + }, + useFeed3(key: SWRKeyInterface, variables?: Feed3QueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed3(variables), config); + }, + useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface) { + return useSWR(key, () => sdk.feed4(variables), config); + } + }; +} export type SdkWithHooks = ReturnType;`) }) })