AWS Amplify GraphQL Transformer Core

9.0.7 · active · verified Tue Apr 21

This package, `@aws-amplify/graphql-transformer-core`, serves as the foundational framework within AWS Amplify for converting GraphQL Schema Definition Language (SDL) into AWS CloudFormation templates. It is integral to how Amplify provisions serverless GraphQL APIs on AWS AppSync, DynamoDB, Lambda, and OpenSearch. Currently at version 9.0.7, it is actively maintained as part of the broader AWS Amplify monorepo, with releases tied to the Amplify CLI and API category updates. Its key differentiators include simplifying complex cloud infrastructure setup through GraphQL directives like `@model`, `@auth`, `@connection`, and `@searchable`, abstracting away much of the underlying AWS service configuration. A significant architectural shift from Transformer V1 to V2 introduced deny-by-default authorization, explicit data modeling directives (`@primaryKey`, `@index`, `@hasOne`, `@hasMany`), and a new AppSync Pipeline Resolver architecture.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `graphql-transformer-core` to take a GraphQL schema with Amplify directives and generate corresponding AWS CloudFormation templates and an expanded GraphQL schema. It shows instantiation of `GraphQLTransform` with a set of specialized transformers, typically imported from other `@aws-amplify/graphql-*` packages, and then executing the `transform()` method to produce infrastructure code.

import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { print, parse } from 'graphql';

async function generateCloudFormation() {
  const schema = `
    type Post @model @auth(rules: [{ allow: public, operations: [read] }, { allow: owner, ownerField: "owner" }]) {
      id: ID!
      title: String!
      content: String
      owner: String
    }

    type Comment @model {
      id: ID!
      post: Post @hasOne
      text: String!
    }
  `;

  const transformers = [
    new ModelTransformer(),
    new AuthTransformer(),
    // You would include other transformers (e.g., ConnectionTransformer) here
  ];

  const transform = new GraphQLTransform({
    schema,
    transformers,
  });

  try {
    const output = await transform.transform();
    console.log('Generated CloudFormation template (truncated):');
    // In a real scenario, you'd save `output.stacks.Root.json` to a file.
    const cfnTemplate = JSON.stringify(output.stacks.Root.json, null, 2);
    console.log(cfnTemplate.substring(0, 500) + '\n...(truncated for brevity)');

    console.log('\nGenerated GraphQL Schema (truncated):');
    // The transformed schema with additional types and directives can also be accessed.
    const transformedSchemaSDL = print(parse(output.schema));
    console.log(transformedSchemaSDL.substring(0, 500) + '\n...(truncated for brevity)');

  } catch (e) {
    console.error('Error during transformation:', e);
  }
}

generateCloudFormation();

view raw JSON →