diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..462deea --- /dev/null +++ b/.prettierignore @@ -0,0 +1,34 @@ +# Add files here to ignore them from prettier formatting +dist/**/* +out/**/* +generated/**/* +**/*.gen.* +node_modules/**/* +coverage/**/* +*.snap + +*.sh +*.png +*.gif +*.ico +*.icns +*.svg +*.txt +*.mp3 + +**/.git* +**/.DS_Store +**/.prettier* +**/.env* +**/.nvm +**/*.conf +**/browserslist + +# custom +.generated/* +capacitor/* +tools/* +**/assets/* +.editorconfig +apple-app-site-association +yarn.lock diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..e69ad43 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,12 @@ +{ + "singleQuote": true, + "tabWidth": 2, + "useTabs": false, + "endOfLine": "auto", + "semi": false, + "printWidth": 70, + "arrowParens": "avoid", + "bracketSpacing": true, + "quoteProps": "as-needed", + "trailingComma": "all" +} diff --git a/src/commands/graphql-client/generator/generate/generateSubscriptionMethod.ts b/src/commands/graphql-client/generator/generate/generateSubscriptionMethod.ts index 460f693..8ae7e22 100644 --- a/src/commands/graphql-client/generator/generate/generateSubscriptionMethod.ts +++ b/src/commands/graphql-client/generator/generate/generateSubscriptionMethod.ts @@ -12,68 +12,89 @@ import renderApolloCall from '../render/renderApolloCall' import renderFragment from '../render/renderFragment' import renderMethod from '../render/renderMethod' import renderOptions from '../render/renderOptions' +import renderReturnDocument from '../render/renderReturnDocument' import renderSubscription from '../render/renderSubscription' -export default function (field: IntrospectionField, types: IntrospectionType[], generateDefaultFragments: boolean) { +export default function ( + field: IntrospectionField, + types: IntrospectionType[], + generateDefaultFragments: boolean, + onlyDocument: boolean, +) { + const queryName = field.name + const propsType = getTypescriptPropsTypeName( + 'Subscription', + queryName, + ) + const inputs = field.args || [] + const methodName = uncapitalizeFirstLetter(queryName) + const hasInputs = !!inputs.length - const queryName = field.name - const propsType = getTypescriptPropsTypeName('Subscription', queryName) - const inputs = field.args || [] - const methodName = uncapitalizeFirstLetter(queryName) - const hasInputs = !!inputs.length + const returnClassFullname = getTypescriptTypeString({ + type: field.type, + }) - const returnClassFullname = getTypescriptTypeString({ - type: field.type, - }) + const returnClassName = getGraphqlTypeString({ + type: field.type, + capitalizeName: true, + onlyName: true, + }) - const returnClassName = getGraphqlTypeString({ - type: field.type, - capitalizeName: true, - onlyName: true, - }) + const returnGraphqlTypeName = returnClassName + const fragmentName = returnClassName + 'Data' - const returnGraphqlTypeName = returnClassName - const fragmentName = returnClassName + 'Data' + const props = generatePropsType(propsType, inputs) - const props = generatePropsType(propsType, inputs) + const resultTypeFields = generateResultTypeFields(field.type, types) + const hasResultType = !!resultTypeFields - const resultTypeFields = generateResultTypeFields(field.type, types) - const hasResultType = !!resultTypeFields + const variablesDeclarationString = buildVariablesDeclarationString( + inputs, + ) + const variablesString = buildVariablesPassString(inputs) - const variablesDeclarationString = buildVariablesDeclarationString(inputs) - const variablesString = buildVariablesPassString(inputs) + const type = ( + types.find(x => x.name === returnGraphqlTypeName) + ) - const type = types.find(x => x.name === returnGraphqlTypeName) + const method = renderMethod({ + rootType: RootType.Subscription, + methodName, + generateDefaultFragments, + hasProps: hasInputs, + propsType, + hasResultType, + renderContent: () => + // Render Query + renderOptions(fragmentName, hasResultType) + + (hasResultType + ? renderFragment( + type, + generateDefaultFragments, + returnGraphqlTypeName, + ) + : '') + + renderSubscription({ + hasFragment: hasResultType, + queryName, + variablesDeclarationString, + variablesString, + }) + + (onlyDocument + ? renderReturnDocument({ + rootType: RootType.Subscription, + hasVariables: hasInputs, + }) + : renderApolloCall({ + rootType: RootType.Subscription, + hasVariables: hasInputs, + queryName, + returnType: returnClassFullname, + })), + }) - const method = renderMethod({ - rootType: RootType.Subscription, - methodName, - generateDefaultFragments, - hasProps: hasInputs, - propsType, - hasResultType, - renderContent: () => - // Render Query - renderOptions(fragmentName, hasResultType) + - (hasResultType - ? renderFragment(type, generateDefaultFragments, returnGraphqlTypeName) - : '') + - renderSubscription({ - hasFragment: hasResultType, - queryName, - variablesDeclarationString, - variablesString, - }) + - renderApolloCall({ - rootType: RootType.Subscription, - hasVariables: hasInputs, - queryName, - returnType: returnClassFullname, - }), - }) - - return { - method, - props, - } + return { + method, + props, + } } diff --git a/src/commands/graphql-client/generator/generateProxy.ts b/src/commands/graphql-client/generator/generateProxy.ts index 0a1fb1d..4a63829 100644 --- a/src/commands/graphql-client/generator/generateProxy.ts +++ b/src/commands/graphql-client/generator/generateProxy.ts @@ -6,85 +6,126 @@ import renderProxy from './render/renderProxy' import renderQueryTypesEnum from './render/renderQueryTypesEnum' export default function ( - introspectionSchema: IntrospectionSchema, - generateDefaultFragments: boolean, + introspectionSchema: IntrospectionSchema, + generateDefaultFragments: boolean, ) { - const { - queryType: { name: queryTypeName }, - types, - }: IntrospectionSchema = introspectionSchema - - const mutationTypeName = introspectionSchema.mutationType - ? introspectionSchema.mutationType.name - : null - - const subscriptionTypeName = introspectionSchema.subscriptionType - ? introspectionSchema.subscriptionType.name - : null - - const queryType = types.find(x => x.name === queryTypeName) - const mutationType = types.find(x => x.name === mutationTypeName) - const subscriptionType = types.find(x => x.name === subscriptionTypeName) - - const otherTypes = types.filter(x => - (x !== queryType) && - (x !== mutationType) && - (x !== subscriptionType), - ) - - const objectTypes = otherTypes.filter(x => x.kind === 'OBJECT') - - // start generation - const generatedQuery = generateRootType(RootType.Query, otherTypes, generateDefaultFragments)(queryType) - const generatedWatchQuery = generateRootType('watchQuery', otherTypes, generateDefaultFragments)(queryType) - const generatedRefetchQuery = generateRootType('refetchQuery', otherTypes, generateDefaultFragments)(queryType) - const generatedCacheWriteQuery = generateRootType('cacheWriteQuery', otherTypes, generateDefaultFragments)(queryType) - const generatedMutation = generateRootType(RootType.Mutation, otherTypes, generateDefaultFragments)(mutationType) - const generatedSubscription = - generateRootType(RootType.Subscription, otherTypes, generateDefaultFragments)(subscriptionType) - - const generatedOtherTypes = otherTypes - .sort(sortTypesByKind) - .map(generateType) - .filter(x => !!x) - .join('\n') - - const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes) - - return renderProxy({ - generatedQuery, - generatedWatchQuery, - generatedRefetchQuery, - generatedCacheWriteQuery, - generatedMutation, - generatedSubscription, - generatedOtherTypes, - generatedQueryTypesEnum, - }) + const { + queryType: { name: queryTypeName }, + types, + }: IntrospectionSchema = introspectionSchema + + const mutationTypeName = introspectionSchema.mutationType + ? introspectionSchema.mutationType.name + : null + + const subscriptionTypeName = introspectionSchema.subscriptionType + ? introspectionSchema.subscriptionType.name + : null + + const queryType = ( + types.find(x => x.name === queryTypeName) + ) + const mutationType = ( + types.find(x => x.name === mutationTypeName) + ) + const subscriptionType = ( + types.find(x => x.name === subscriptionTypeName) + ) + + const otherTypes = types.filter( + x => + x !== queryType && x !== mutationType && x !== subscriptionType, + ) + + const objectTypes = otherTypes.filter(x => x.kind === 'OBJECT') + + // start generation + const generatedQuery = generateRootType( + RootType.Query, + otherTypes, + generateDefaultFragments, + )(queryType) + + const generatedWatchQuery = generateRootType( + 'watchQuery', + otherTypes, + generateDefaultFragments, + )(queryType) + + const generatedRefetchQuery = generateRootType( + 'refetchQuery', + otherTypes, + generateDefaultFragments, + )(queryType) + + const generatedCacheWriteQuery = generateRootType( + 'cacheWriteQuery', + otherTypes, + generateDefaultFragments, + )(queryType) + + const generatedMutation = generateRootType( + RootType.Mutation, + otherTypes, + generateDefaultFragments, + )(mutationType) + + const generatedSubscription = generateRootType( + RootType.Subscription, + otherTypes, + generateDefaultFragments, + )(subscriptionType) + + const generatedSubscriptionDocument = generateRootType( + RootType.Subscription, + otherTypes, + generateDefaultFragments, + true, + )(subscriptionType) + + const generatedOtherTypes = otherTypes + .sort(sortTypesByKind) + .map(generateType) + .filter(x => !!x) + .join('\n') + + const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes) + + return renderProxy({ + generatedQuery, + generatedWatchQuery, + generatedRefetchQuery, + generatedCacheWriteQuery, + generatedMutation, + generatedSubscription, + generatedSubscriptionDocument, + generatedOtherTypes, + generatedQueryTypesEnum, + }) } function sortTypesByKind(a: IntrospectionType, b: IntrospectionType) { - return getSortValue(b) - getSortValue(a) + return getSortValue(b) - getSortValue(a) } function getSortValue(x: IntrospectionType) { - switch (x.kind) { - case 'SCALAR': - return 10 + switch (x.kind) { + case 'SCALAR': + return 10 - case 'UNION': - return 9 + case 'UNION': + return 9 - case 'ENUM': - return 8 + case 'ENUM': + return 8 - case 'INPUT_OBJECT': - return 7 + case 'INPUT_OBJECT': + return 7 - case 'OBJECT': - return 6 + case 'OBJECT': + return 6 - default: - return 0 - } + default: + return 0 + } } diff --git a/src/commands/graphql-client/generator/generateRootType.ts b/src/commands/graphql-client/generator/generateRootType.ts index 256dba3..6a3fcb8 100644 --- a/src/commands/graphql-client/generator/generateRootType.ts +++ b/src/commands/graphql-client/generator/generateRootType.ts @@ -10,45 +10,57 @@ import generateWatchQueryMethod from './generate/generateWatchQueryMethod' import renderRootTypeClass from './render/renderRootTypeClass' export default ( - typeName: RootType | 'watchQuery' | 'refetchQuery' | 'cacheWriteQuery', - otherTypes: IntrospectionType[], - generateDefaultFragments: boolean, + typeName: + | RootType + | 'watchQuery' + | 'refetchQuery' + | 'cacheWriteQuery', + otherTypes: IntrospectionType[], + generateDefaultFragments: boolean, + onlyDocument = false, ) => (queryType: IntrospectionType) => { + if ( + !queryType || + queryType.kind !== 'OBJECT' || + !queryType.fields || + !queryType.fields.length + ) { + return null + } - if (!queryType || - (queryType.kind !== 'OBJECT') || - !queryType.fields || - !queryType.fields.length - ) { - return null - } - - const generateOptions = { - [RootType.Query.toString()]: generateQueryMethod, - ['watchQuery']: generateWatchQueryMethod, - ['refetchQuery']: generateRefetchQueryMethod, - ['cacheWriteQuery']: generateCacheWriteQueryMethod, - [RootType.Mutation.toString()]: generateMutationMethod, - [RootType.Subscription.toString()]: generateSubscriptionMethod, - } - - const generateMethod = generateOptions[typeName] - - const methodsAndProps = queryType.fields - .map(x => generateMethod(x, otherTypes, generateDefaultFragments)) - - const methodProps = methodsAndProps - .map(x => x.props) - .filter(x => !!x) - - const methods = methodsAndProps - .map(x => x.method) - - const className = capitalizeFirstLetter(typeName) - - return renderRootTypeClass({ - className, - renderPropTypes: () => methodProps.join('\n'), - renderMethods: () => methods.join('\n'), - }) + const generateOptions = { + [RootType.Query.toString()]: generateQueryMethod, + ['watchQuery']: generateWatchQueryMethod, + ['refetchQuery']: generateRefetchQueryMethod, + ['cacheWriteQuery']: generateCacheWriteQueryMethod, + [RootType.Mutation.toString()]: generateMutationMethod, + [RootType.Subscription.toString()]: generateSubscriptionMethod, + } + + const generateMethod = generateOptions[typeName] + + const methodsAndProps = queryType.fields.map(x => + generateMethod( + x, + otherTypes, + generateDefaultFragments, + onlyDocument, + ), + ) + + const methodProps = onlyDocument + ? [] + : methodsAndProps.map(x => x.props).filter(x => !!x) + + const methods = methodsAndProps.map(x => x.method) + + const className = capitalizeFirstLetter(typeName) + + return renderRootTypeClass({ + className, + renderPropTypes: () => methodProps.join('\n'), + renderMethods: () => methods.join('\n'), + isWatchQuery: typeName === 'watchQuery', + onlyDocument, + }) } diff --git a/src/commands/graphql-client/generator/render/renderApolloCall.ts b/src/commands/graphql-client/generator/render/renderApolloCall.ts index a27e0f2..da4d982 100644 --- a/src/commands/graphql-client/generator/render/renderApolloCall.ts +++ b/src/commands/graphql-client/generator/render/renderApolloCall.ts @@ -1,63 +1,85 @@ import { RootType } from '../../utils/rootType' export default function ({ - rootType, - hasVariables, - queryName, - returnType, - isWatchQuery = false, + rootType, + hasVariables, + queryName, + returnType, + isWatchQuery = false, }: Props) { + let methodName = { + [RootType.Query]: 'query', + [RootType.Mutation]: 'mutate', + [RootType.Subscription]: 'subscribe', + }[rootType] - let methodName = { - [RootType.Query]: 'query', - [RootType.Mutation]: 'mutate', - [RootType.Subscription]: 'subscribe', - }[rootType] + if (isWatchQuery && rootType === RootType.Query) { + methodName = 'watchQuery' + } - if (isWatchQuery && (rootType === RootType.Query)) { - methodName = 'watchQuery' - } - - if (rootType === RootType.Subscription) { - return ` + if (rootType === RootType.Subscription) { + return ` // apollo call const zenObs = this.client.${methodName}<${returnType}>({ ...mergedOptions, - query: ${rootType},${hasVariables ? ` - variables: props,` : ''} + query: ${rootType},${ + hasVariables + ? ` + variables: props,` + : '' + } }) return from(fixObservable(zenObs)).pipe( map(result => getResultData<${returnType}>(result, '${queryName}')) )` - } + } - if (isWatchQuery) { - return ` + if (isWatchQuery) { + return ` const zenObs = this.client.watchQuery<${returnType}>({ ...mergedOptions, - query,${hasVariables ? ` - variables: props,` : ''} + query,${ + hasVariables + ? ` + variables: props,` + : '' + } + }) + + const { subscribeToMore = [] } = mergedOptions + + subscribeToMore.forEach(x => { + zenObs.subscribeToMore({ + ...(typeof x.graphqlDocument === 'function' + ? x.graphqlDocument(this.subscriptionDocument) + : x.graphqlDocument), + updateQuery: x.updateQuery, + }) }) return from(fixObservable(zenObs)).pipe( map(result => getResultData<${returnType}>(result, '${queryName}')) )` - } + } - return ` + return ` // apollo call return this.client.${methodName}({ ...mergedOptions, - ${rootType},${hasVariables ? ` - variables: props,` : ''} + ${rootType},${ + hasVariables + ? ` + variables: props,` + : '' + } }).then(result => getResultData<${returnType}>(result, '${queryName}'))` } interface Props { - rootType: RootType - hasVariables: boolean - queryName: string - returnType: any - isWatchQuery?: boolean + rootType: RootType + hasVariables: boolean + queryName: string + returnType: any + isWatchQuery?: boolean } diff --git a/src/commands/graphql-client/generator/render/renderImports.ts b/src/commands/graphql-client/generator/render/renderImports.ts index 3caa6c1..769a7aa 100644 --- a/src/commands/graphql-client/generator/render/renderImports.ts +++ b/src/commands/graphql-client/generator/render/renderImports.ts @@ -1,5 +1,5 @@ export default function () { - return ` + return ` import ApolloClient, { ErrorPolicy, FetchPolicy, @@ -13,6 +13,7 @@ import ApolloClient, { // gql2 - to ignore apollo extention validation // for now there is no better way import gql2 from 'graphql-tag' +import { UpdateQueryFn } from 'apollo-client/core/watchQueryOptions' // rx library import { from, observable } from 'rxjs' diff --git a/src/commands/graphql-client/generator/render/renderMethod.ts b/src/commands/graphql-client/generator/render/renderMethod.ts index 5255fbf..0ba3046 100644 --- a/src/commands/graphql-client/generator/render/renderMethod.ts +++ b/src/commands/graphql-client/generator/render/renderMethod.ts @@ -1,52 +1,45 @@ import { RootType } from '../../utils/rootType' export default function ({ - rootType, - methodName, - generateDefaultFragments, - hasProps, - propsType, - renderContent, - hasResultType, + rootType, + methodName, + generateDefaultFragments, + hasProps, + propsType, + renderContent, + hasResultType, }) { - const omittedOptionsType = { - [RootType.Query]: 'OmittedQueryOptions', - watchQuery: 'OmittedWatchQueryOptions', - cacheWriteQuery: 'OmittedQueryOptions', - [RootType.Mutation]: 'OmittedMutationOptions', - [RootType.Subscription]: 'OmittedSubscriptionOptions', - }[rootType] + const omittedOptionsType = { + [RootType.Query]: 'OmittedQueryOptions', + watchQuery: `OmittedWatchQueryOptions & SubscribeToMoreOptions`, + cacheWriteQuery: 'OmittedQueryOptions', + [RootType.Mutation]: 'OmittedMutationOptions', + [RootType.Subscription]: 'OmittedSubscriptionOptions', + }[rootType] - const isCacheWriteQueryMode = rootType === 'cacheWriteQuery' + const isCacheWriteQueryMode = rootType === 'cacheWriteQuery' - const fragmentRequiredSymbol = generateDefaultFragments ? '?' : '' + const fragmentRequiredSymbol = generateDefaultFragments ? '?' : '' - const fragmentProp = hasResultType - ? `fragment${fragmentRequiredSymbol}: string | any,` - : '' + const fragmentProp = hasResultType + ? `fragment${fragmentRequiredSymbol}: string | any,` + : '' - const fragmentType = hasResultType ? 'FragmentOptions' : '' + const fragmentType = hasResultType ? 'FragmentOptions' : '' - // if (!hasProps) { - // return ` - // ${methodName}( - // ${fragmentProp} - // options?: GraphqlCallOptions ${fragmentType} & ${omittedOptionsType}, - // ) { - // ${renderContent()} - // }` - // } - - return ` + return ` ${methodName}( ${hasProps ? `props: ${propsType},` : ``} - ${isCacheWriteQueryMode - ? `data: any, + ${ + isCacheWriteQueryMode + ? `data: any, ${fragmentProp}` - : ` + : ` ${fragmentProp} - options?: GraphqlCallOptions ${fragmentType ? `& ${fragmentType}` : ''} & ${omittedOptionsType},` - } + options?: GraphqlCallOptions ${ + fragmentType ? `& ${fragmentType}` : '' + } & ${omittedOptionsType},` + } ) { ${renderContent()} }` diff --git a/src/commands/graphql-client/generator/render/renderProxy.ts b/src/commands/graphql-client/generator/render/renderProxy.ts index 12e6f0f..a7715cd 100644 --- a/src/commands/graphql-client/generator/render/renderProxy.ts +++ b/src/commands/graphql-client/generator/render/renderProxy.ts @@ -1,16 +1,17 @@ import renderImports from './renderImports' export default function ({ - generatedOtherTypes, - generatedQuery, - generatedWatchQuery, - generatedRefetchQuery, - generatedCacheWriteQuery, - generatedMutation, - generatedSubscription, - generatedQueryTypesEnum, + generatedOtherTypes, + generatedQuery, + generatedWatchQuery, + generatedRefetchQuery, + generatedCacheWriteQuery, + generatedMutation, + generatedSubscription, + generatedSubscriptionDocument, + generatedQueryTypesEnum, }) { - return `${renderImports()} + return `${renderImports()} // tslint:disable @@ -26,29 +27,39 @@ ${generatedRefetchQuery || ''} ${generatedCacheWriteQuery || ''} ${generatedMutation || ''} ${generatedSubscription || ''} +${generatedSubscriptionDocument || 'class SubscriptionDocument { }'} // helper types type Omit = Pick> ${ - generatedQuery - ? ` + generatedQuery + ? ` type OmittedQueryOptions = Omit>, 'query' | 'variables'> -type OmittedWatchQueryOptions = Omit>, 'variables' | 'query'>` - : '' - } +type OmittedWatchQueryOptions = Omit>, 'variables' | 'query'> + +type SubscribeToMoreOptions = { + subscribeToMore: + { + graphqlDocument: { document: any, variables?: any } | ((subscription: SubscriptionDocument) => { document: any, variables?: any }), + updateQuery: UpdateQueryFn + }[] +} + +` + : '' +} ${ - generatedMutation - ? ` + generatedMutation + ? ` type OmittedMutationOptions = Omit>, 'mutation' | 'variables'>` - : '' - } + : '' +} ${ - generatedSubscription - ? ` + generatedSubscription + ? ` type OmittedSubscriptionOptions = Omit>, 'query' | 'variables'>` - : '' - } - + : '' +} interface FragmentOptions { @@ -63,12 +74,16 @@ interface GraphqlCallOptions { interface DefaultOptions { ${generatedQuery ? 'query?: GraphqlCallOptions' : ''} ${generatedWatchQuery ? 'watchQuery?: GraphqlCallOptions' : ''} - ${generatedMutation ? `mutation?: Omit` : ''} ${ - generatedSubscription - ? `subscription?: Omit` - : '' - } + generatedMutation + ? `mutation?: Omit` + : '' + } + ${ + generatedSubscription + ? `subscription?: Omit` + : '' + } } export interface Client { @@ -81,33 +96,46 @@ export interface Client { } export default function (client: ApolloClient, defaultOptions: DefaultOptions = {}): Client { + ${ + generatedSubscriptionDocument + ? 'const subscriptionDocument = new SubscriptionDocument(client, defaultOptions.subscription || {})' + : '' + } return { - ${generatedQuery ? 'query: new Query(client, defaultOptions.query || {}),' : ''} ${ - generatedWatchQuery - ? 'watchQuery: new WatchQuery(client, defaultOptions.query || {}),' - : '' - } + generatedQuery + ? 'query: new Query(client, defaultOptions.query || {}),' + : '' + } + ${ + generatedWatchQuery + ? `watchQuery: new WatchQuery(client, defaultOptions.query || {}, ${ + generatedSubscriptionDocument + ? 'subscriptionDocument' + : 'null' + }),` + : '' + } ${ - generatedRefetchQuery - ? 'refetchQuery: new RefetchQuery(client, defaultOptions.query || {}),' - : '' - } + generatedRefetchQuery + ? 'refetchQuery: new RefetchQuery(client, defaultOptions.query || {}),' + : '' + } ${ - generatedCacheWriteQuery - ? 'cacheWriteQuery: new CacheWriteQuery(client, defaultOptions.query || {}),' - : '' - } + generatedCacheWriteQuery + ? 'cacheWriteQuery: new CacheWriteQuery(client, defaultOptions.query || {}),' + : '' + } ${ - generatedMutation - ? 'mutation: new Mutation(client, defaultOptions.mutation || {}),' - : '' - } + generatedMutation + ? 'mutation: new Mutation(client, defaultOptions.mutation || {}),' + : '' + } ${ - generatedSubscription - ? 'subscription: new Subscription(client, defaultOptions.subscription || {}),' - : '' - } + generatedSubscription + ? 'subscription: new Subscription(client, defaultOptions.subscription || {}),' + : '' + } } } diff --git a/src/commands/graphql-client/generator/render/renderReturnDocument.ts b/src/commands/graphql-client/generator/render/renderReturnDocument.ts new file mode 100644 index 0000000..291a1a3 --- /dev/null +++ b/src/commands/graphql-client/generator/render/renderReturnDocument.ts @@ -0,0 +1,15 @@ +import { RootType } from '../../utils/rootType' + +export default function ({ rootType, hasVariables }: Props) { + return ` + return { + document: ${rootType}, + ${hasVariables ? 'variables: props,' : ''} + } + ` +} + +interface Props { + rootType: RootType + hasVariables: boolean +} diff --git a/src/commands/graphql-client/generator/render/renderRootTypeClass.ts b/src/commands/graphql-client/generator/render/renderRootTypeClass.ts index 6661ac9..23fa069 100644 --- a/src/commands/graphql-client/generator/render/renderRootTypeClass.ts +++ b/src/commands/graphql-client/generator/render/renderRootTypeClass.ts @@ -1,16 +1,22 @@ export default function ({ - className, - renderPropTypes, - renderMethods, + className, + renderPropTypes, + renderMethods, + isWatchQuery, + onlyDocument, }) { - return ` + return ` // ${className} props ----------------------------------- ${renderPropTypes()} // ${className} apis ------------------------------------ -class ${className} { +class ${className}${onlyDocument ? 'Document' : ''} { - constructor(private client: ApolloClient, private defaultOptions: GraphqlCallOptions = {}) { } + constructor(private client: ApolloClient, private defaultOptions: GraphqlCallOptions = {} ${ + isWatchQuery + ? ', private subscriptionDocument: SubscriptionDocument = null' + : '' + }) { } ${renderMethods()} } `