Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
childFragmentSpread.map(spread => ({
name: camelCase(spread.name),
type: spread.name,
annotations: ['Nonnull'],
})),
[],
* you can use to render your UI.`;
const queryExample = `
* const { data, loading, error } = use${operationName}({
* variables: {${variableString}
* },
* });`;
const mutationDescription = `
* To run a mutation, you first call \`use${operationName}\` within a React component and pass it any options that fit your needs.
* When your component renders, \`use${operationName}\` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution`;
const mutationExample = `
* const [${camelCase(operationName)}, { data, loading, error }] = use${operationName}({
* variables: {${variableString}
* },
* });`;
return `
/**
* __use${operationName}__
*${operationType === 'Mutation' ? mutationDescription : queryDescription}
*
* @param baseOptions options that will be passed into the ${operationType.toLowerCase()}, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#${operationType === 'Mutation' ? 'options-2' : 'options'};
*
* @example${operationType === 'Mutation' ? mutationExample : queryExample}
*/`;
}
const injectString = (service: string) => `private ${camelCase(service)}: ${service}`;
const injections = this._operationsToInclude
public changeCase (pattern?: 'pascalcase' | 'camelcase' | 'snakecase'): this {
switch (pattern) {
case 'camelcase':
this.input = camelCase(this.input)
return this
case 'pascalcase':
this.input = pascalCase(this.input)
return this
case 'snakecase':
this.input = snakeCase(this.input)
return this
default:
return this
}
}
this._imports.add(Imports.Utils);
return `this.${varName} = Utils.checkNotNull(${varName}, "${varName} == null");`;
})
.join('\n'),
childFragmentSpread.map(spread => ({
name: camelCase(spread.name),
type: spread.name,
annotations: ['Nonnull'],
})),
[],
'public'
);
for (const spread of childFragmentSpread) {
const fragmentVarName = camelCase(spread.name);
fragmentsClass.addClassMember(fragmentVarName, spread.name, null, ['Nonnull'], 'private', { final: true });
fragmentsClass.addClassMethod(fragmentVarName, spread.name, `return this.${fragmentVarName};`, [], ['Nonnull'], 'public', {}, []);
fragmentMapperClass.addClassMember(`${fragmentVarName}FieldMapper`, `${spread.name}.Mapper`, `new ${spread.name}.Mapper()`, [], 'private', { final: true });
}
fragmentMapperClass.addClassMethod(
'map',
fragmentsClassName,
`
${childFragmentSpread
.map(spread => {
const fragmentVarName = camelCase(spread.name);
return `${spread.name} ${fragmentVarName} = null;
if (${spread.name}.POSSIBLE_TYPES.contains(conditionalType)) {
${fragmentVarName} = ${fragmentVarName}FieldMapper.map(reader);
private _buildOperationHoc(node: OperationDefinitionNode, documentVariableName: string, operationResultType: string, operationVariablesTypes: string): string {
this.imports.add(this.getApolloReactCommonImport());
this.imports.add(this.getApolloReactHocImport());
const operationName: string = this.convertName(node.name.value, { useTypesPrefix: false });
const propsTypeName: string = this.convertName(node.name.value, { suffix: 'Props' });
const propsVar = `export type ${propsTypeName} = ${this._buildHocProps(node.name.value, node.operation)} | TChildProps;`;
const hocString = `export function with${operationName}(operationOptions?: ApolloReactHoc.OperationOption<
TProps,
${operationResultType},
${operationVariablesTypes},
${propsTypeName}>) {
return ApolloReactHoc.with${pascalCase(node.operation)}>(${this.getDocumentNodeVariable(node, documentVariableName)}, {
alias: '${camelCase(operationName)}',
...operationOptions
});
};`;
return [propsVar, hocString].filter(a => a).join('\n');
}
function toCamelCase(str) {
let prefixLength = -1;
while (str[++prefixLength] === '_');
if (!prefixLength) {
return camelCase(str);
}
return str.substring(0, prefixLength) + camelCase(str.substring(prefixLength));
}
.map(spread => {
const varName = camelCase(spread.name);
return indentMultiline(`h *= 1000003;\nh ^= ${varName}.hashCode();`, 1);
})
.join('\n')}
.map(o => {
const optionalVariables = !o.node.variableDefinitions || o.node.variableDefinitions.length === 0 || o.node.variableDefinitions.every(v => v.type.kind !== Kind.NON_NULL_TYPE || !!v.defaultValue);
const options = o.operationType === 'Mutation' ? `${o.operationType}OptionsAlone<${o.operationResultType}, ${o.operationVariablesTypes}>` : `${o.operationType}OptionsAlone<${o.operationVariablesTypes}>`;
const method = `
${camelCase(o.node.name.value)}(variables${optionalVariables ? '?' : ''}: ${o.operationVariablesTypes}, options?: ${options}) {
return this.${camelCase(o.serviceName)}.${actionType(o.operationType)}(variables, options)
}`;
let watchMethod;
if (o.operationType === 'Query') {
watchMethod = `
${camelCase(o.node.name.value)}Watch(variables${optionalVariables ? '?' : ''}: ${o.operationVariablesTypes}, options?: WatchQueryOptionsAlone<${o.operationVariablesTypes}>) {
return this.${camelCase(o.serviceName)}.watch(variables, options)
}`;
}
return [method, watchMethod].join('');
})
.map(s => indentMultiline(s, 2));