graphql#TypeDefinitionNode TypeScript Examples

The following examples show how to use graphql#TypeDefinitionNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: preset.ts    From amplify-codegen with Apache License 2.0 6 votes vote down vote up
generateJavaPreset = (
  options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>,
  models: TypeDefinitionNode[],
): Types.GenerateOptions[] => {
  const config: Types.GenerateOptions[] = [];
  const baseOutputDir = [options.baseOutputDir, ...GENERATED_PACKAGE_NAME.split('.')];
  models.forEach(model => {
    const modelName = model.name.value;
    config.push({
      ...options,
      filename: join(...baseOutputDir, `${modelName}.java`),
      config: {
        ...options.config,
        scalars: { ...JAVA_SCALAR_MAP, ...options.config.scalars },
        selectedType: modelName,
      },
    });
  });

  // Class loader
  config.push({
    ...options,
    filename: join(...baseOutputDir, `${LOADER_CLASS_NAME}.java`),
    config: {
      ...options.config,
      scalars: { ...JAVA_SCALAR_MAP, ...options.config.scalars },
      generate: 'loader',
    },
  });

  return config;
}
Example #2
Source File: preset.ts    From amplify-codegen with Apache License 2.0 6 votes vote down vote up
generateTypeScriptPreset = (
  options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>,
  models: TypeDefinitionNode[],
): Types.GenerateOptions[] => {
  const config: Types.GenerateOptions[] = [];
  const modelFolder = join(options.baseOutputDir, 'models');
  config.push({
    ...options,
    filename: join(modelFolder, 'index.ts'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      metadata: false,
    },
  });
  // metadata
  config.push({
    ...options,
    filename: join(modelFolder, 'schema.ts'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      target: 'metadata',
      metadataTarget: 'typescript',
    },
  });
  return config;
}
Example #3
Source File: preset.ts    From amplify-codegen with Apache License 2.0 6 votes vote down vote up
generateDartPreset = (
  options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>,
  models: TypeDefinitionNode[],
): Types.GenerateOptions[] => {
  const config: Types.GenerateOptions[] = [];
  models.forEach(model => {
    const modelName = model.name.value;
    config.push({
      ...options,
      filename: join(options.baseOutputDir, `${modelName}.dart`),
      config: {
        ...options.config,
        scalars: { ...DART_SCALAR_MAP, ...options.config.scalars },
        selectedType: modelName,
      },
    });
  });
  // Class loader
  config.push({
    ...options,
    filename: join(options.baseOutputDir, `ModelProvider.dart`),
    config: {
      ...options.config,
      scalars: { ...DART_SCALAR_MAP, ...options.config.scalars },
      generate: 'loader',
    },
  });
  return config;
}
Example #4
Source File: preset.ts    From amplify-codegen with Apache License 2.0 6 votes vote down vote up
generateManyToManyModelStubs = (options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>) : TypeDefinitionNode[] => {
  let models = new Array<TypeDefinitionNode>();
  let manyToManySet = new Set<string>();
  options.schema.definitions.forEach(def => {
    if (def.kind === 'ObjectTypeDefinition') {
      def?.fields?.forEach(field => {
        field?.directives?.forEach(dir => {
          if (dir?.name?.value === 'manyToMany') {
            dir?.arguments?.forEach(arg => {
              if(arg.name.value === 'relationName' && arg.value.kind === 'StringValue') {
                manyToManySet.add(graphqlName(toUpper(arg.value.value)));
              }
            });
          }
        });
      });
    }
  });
  manyToManySet.forEach(modelName => {
    models.push({
      kind: 'ObjectTypeDefinition',
      name: {
        kind: 'Name',
        value: modelName
      }
    })
  });
  return models;
}
Example #5
Source File: preset.ts    From amplify-codegen with Apache License 2.0 6 votes vote down vote up
preset: Types.OutputPreset<AppSyncModelCodeGenPresetConfig> = {
  buildGeneratesSection: (options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>): Types.GenerateOptions[] => {
    const codeGenTarget = options.config.target;
    const typesToSkip: string[] = ['Query', 'Mutation', 'Subscription'];
    const models: TypeDefinitionNode[] = options.schema.definitions.filter(
      t =>
        (t.kind === 'ObjectTypeDefinition' && !typesToSkip.includes(t.name.value)) ||
        (t.kind === 'EnumTypeDefinition' && !t.name.value.startsWith('__')),
    ) as any;
    if (options.config.usePipelinedTransformer || options.config.transformerVersion === 2) {
      models.push(...generateManyToManyModelStubs(options));
    }

    switch (codeGenTarget) {
      case 'java':
        return generateJavaPreset(options, models);
      case 'swift':
        return generateSwiftPreset(options, models);
      case 'javascript':
        return generateJavasScriptPreset(options, models);
      case 'typescript':
        return generateTypeScriptPreset(options, models);
      case 'dart':
        return generateDartPreset(options, models);
      default:
        throw new Error(
          `amplify-codegen-appsync-model-plugin not support language target ${codeGenTarget}. Supported codegen targets arr ${APPSYNC_DATA_STORE_CODEGEN_TARGETS.join(
            ', ',
          )}`,
        );
    }
  },
}
Example #6
Source File: preset.ts    From amplify-codegen with Apache License 2.0 5 votes vote down vote up
generateSwiftPreset = (
  options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>,
  models: TypeDefinitionNode[],
): Types.GenerateOptions[] => {
  const config: Types.GenerateOptions[] = [];
  models.forEach(model => {
    const modelName = model.name.value;
    config.push({
      ...options,
      filename: join(options.baseOutputDir, `${modelName}.swift`),
      config: {
        ...options.config,
        scalars: { ...SWIFT_SCALAR_MAP, ...options.config.scalars },
        generate: 'code',
        selectedType: modelName,
      },
    });
    if (model.kind !== Kind.ENUM_TYPE_DEFINITION) {
      config.push({
        ...options,
        filename: join(options.baseOutputDir, `${modelName}+Schema.swift`),
        config: {
          ...options.config,
          target: 'swift',
          scalars: { ...SWIFT_SCALAR_MAP, ...options.config.scalars },
          generate: 'metadata',
          selectedType: modelName,
        },
      });
    }
  });

  // class loader
  config.push({
    ...options,
    filename: join(options.baseOutputDir, `AmplifyModels.swift`),
    config: {
      ...options.config,
      scalars: { ...SWIFT_SCALAR_MAP, ...options.config.scalars },
      target: 'swift',
      generate: 'loader',
    },
  });
  return config;
}
Example #7
Source File: preset.ts    From amplify-codegen with Apache License 2.0 5 votes vote down vote up
generateJavasScriptPreset = (
  options: Types.PresetFnArgs<AppSyncModelCodeGenPresetConfig>,
  models: TypeDefinitionNode[],
): Types.GenerateOptions[] => {
  const config: Types.GenerateOptions[] = [];
  const modelFolder = join(options.baseOutputDir, 'models');
  config.push({
    ...options,
    filename: join(modelFolder, 'index.js'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      metadata: false,
    },
  });

  //indx.d.ts
  config.push({
    ...options,
    filename: join(modelFolder, 'index.d.ts'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      metadata: false,
      isDeclaration: true,
    },
  });
  // metadata schema.js
  config.push({
    ...options,
    filename: join(modelFolder, 'schema.js'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      target: 'metadata',
      metadataTarget: 'javascript',
    },
  });

  // schema.d.ts
  config.push({
    ...options,
    filename: join(modelFolder, 'schema.d.ts'),
    config: {
      ...options.config,
      scalars: { ...TYPESCRIPT_SCALAR_MAP, ...options.config.scalars },
      target: 'metadata',
      metadataTarget: 'typeDeclaration',
    },
  });
  return config;
}