1 | import { ClientSideBaseVisitor, DocumentMode, getConfigValue, indentMultiline, } from '@graphql-codegen/visitor-plugin-common';
|
2 | import autoBind from 'auto-bind';
|
3 | import { Kind, print } from 'graphql';
|
4 | export class GenericSdkVisitor extends ClientSideBaseVisitor {
|
5 | constructor(schema, fragments, rawConfig) {
|
6 | super(schema, fragments, rawConfig, {
|
7 | usingObservableFrom: rawConfig.usingObservableFrom,
|
8 | rawRequest: getConfigValue(rawConfig.rawRequest, false),
|
9 | });
|
10 | this._operationsToInclude = [];
|
11 | autoBind(this);
|
12 | if (this.config.usingObservableFrom) {
|
13 | this._additionalImports.push(this.config.usingObservableFrom);
|
14 | }
|
15 | const importType = this.config.useTypeImports ? 'import type' : 'import';
|
16 | if (this.config.documentMode !== DocumentMode.string) {
|
17 | this._additionalImports.push(`${importType} { DocumentNode${this.config.rawRequest ? ', ExecutionResult' : ''} } from 'graphql';`);
|
18 | }
|
19 | else if (this.config.rawRequest) {
|
20 | this._additionalImports.push(`${importType} { ExecutionResult } from 'graphql';`);
|
21 | }
|
22 | }
|
23 | buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {
|
24 | if (node.name == null) {
|
25 | throw new Error("Plugin 'generic-sdk' cannot generate SDK for unnamed operation.\n\n" + print(node));
|
26 | }
|
27 | else {
|
28 | this._operationsToInclude.push({
|
29 | node,
|
30 | documentVariableName,
|
31 | operationType,
|
32 | operationResultType,
|
33 | operationVariablesTypes,
|
34 | });
|
35 | }
|
36 | return null;
|
37 | }
|
38 | get sdkContent() {
|
39 | const usingObservable = !!this.config.usingObservableFrom;
|
40 | const allPossibleActions = this._operationsToInclude
|
41 | .map(o => {
|
42 | const optionalVariables = !o.node.variableDefinitions ||
|
43 | o.node.variableDefinitions.length === 0 ||
|
44 | o.node.variableDefinitions.every(v => v.type.kind !== Kind.NON_NULL_TYPE || v.defaultValue);
|
45 | const returnType = usingObservable && o.operationType === 'Subscription' ? 'Observable' : 'Promise';
|
46 | const resultData = this.config.rawRequest
|
47 | ? `ExecutionResult<${o.operationResultType}, E>`
|
48 | : o.operationResultType;
|
49 | return `${o.node.name.value}(variables${optionalVariables ? '?' : ''}: ${o.operationVariablesTypes}, options?: C): ${returnType}<${resultData}> {
|
50 | return requester<${o.operationResultType}, ${o.operationVariablesTypes}>(${o.documentVariableName}, variables, options);
|
51 | }`;
|
52 | })
|
53 | .map(s => indentMultiline(s, 2));
|
54 | const documentNodeType = this.config.documentMode === DocumentMode.string ? 'string' : 'DocumentNode';
|
55 | const resultData = this.config.rawRequest ? 'ExecutionResult<R, E>' : 'R';
|
56 | const returnType = usingObservable
|
57 | ? `Promise<${resultData}> & Observable<${resultData}>`
|
58 | : `Promise<${resultData}>`;
|
59 | return `export type Requester<C = {}, E = unknown> = <R, V>(doc: ${documentNodeType}, vars?: V, options?: C) => ${returnType}
|
60 | export function getSdk<C, E>(requester: Requester<C, E>) {
|
61 | return {
|
62 | ${allPossibleActions.join(',\n')}
|
63 | };
|
64 | }
|
65 | export type Sdk = ReturnType<typeof getSdk>;`;
|
66 | }
|
67 | }
|