Search code examples
amazon-web-servicesgraphqlaws-cdkaws-config

How can I break apart a GraphQL Schema into multiple files in AWS-CDK (SST -> AppSyncApi)


In the SST AppSyncApi it is clearly mentioned that schema can have type of string | string[], but when i am useing an array of graphql schema source as string i am getting this error

Getting this error becouse of multiple graphql source in Stack Configuration.

Is there any way i can use seperate graphql schema for each section so that it will be easy to manage large applicatios.

Project Link

AWS_Backend Project

Error

 TypeError: mergeTypeDefs is not a function
    at AppSyncApi.createGraphApi (file:///E:/aws_projects/backend/node_modules/@serverless-stack/resources/dist/AppSyncApi.js:240:42)
    at new AppSyncApi (file:///E:/aws_projects/backend/node_modules/@serverless-stack/resources/dist/AppSyncApi.js:48:14)
    at EmptyStack.MyStack (file:///E:/aws_projects/backend/.build/lib/index.js:3386:15)
    at stack (file:///E:/aws_projects/backend/node_modules/@serverless-stack/resources/dist/FunctionalStack.js:15:35)
    at App.stack (file:///E:/aws_projects/backend/node_modules/@serverless-stack/resources/dist/App.js:336:16)
    at Module.main (file:///E:/aws_projects/backend/.build/lib/index.js:3437:7)
    at file:///E:/aws_projects/backend/.build/run.mjs:99:22 

folder structure

.
├── lib
│   ├── MyStack
│   ├── Resolvers
│   ├── Schemas
│   └── DataSources
├── src
│   ├── feature1
│   |  ├── feature1.graphql
│   |  ├── feature1.handler
│   |  ├── feature1.resolver
│   ├── feature2
│   |  ├── feature2.graphql
│   |  ├── ...
│   ├── ...
│   └── ...
└── ...

this is my stack configuration

import { StackContext, AppSyncApi, Cognito } from '@serverless-stack/resources';
import dataSources from './dataSources';
import resolvers from './resolvers';
import { AuthorizationType, UserPoolDefaultAction } from '@aws-cdk/aws-appsync-alpha';
import { Duration, Expiration } from 'aws-cdk-lib';

export function MyStack({ stack }: StackContext) {
  // Create the AppSync GraphQL API
  const auth = new Cognito(stack, 'Auth');

  const api = new AppSyncApi(stack, 'AppSyncApi', {
    schema: ['src/feature1/feature1.graphql','src/feature1/feature1.graphql'],
    defaults: {
      function: {
        timeout: 20,
        environment: {
          DATABASE:
            process.env.DATABASE ||
            `mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@atlascluster.sr2q4hg.mongodb.net/${process.env.DATABASE_NAME}?retryWrites=true&w=majority`,
          GRAPHQL_API_URL: process.env.GRAPHQL_API_URL || '',
          GRAPHQL_API_KEY: process.env.GRAPHQL_API_KEY || '',
        },
      },
    },
    cdk: {
      graphqlApi: {
        authorizationConfig: {
          defaultAuthorization: {
            authorizationType: AuthorizationType.USER_POOL,
            userPoolConfig: {
              userPool: auth.cdk.userPool,
              defaultAction: UserPoolDefaultAction.ALLOW,
            },
          },
          additionalAuthorizationModes: [
            {
              authorizationType: AuthorizationType.API_KEY,
              apiKeyConfig: {
                expires: Expiration.after(Duration.days(365)),
              },
            },
          ],
        },
      },
    },
    dataSources: dataSources,
    resolvers: { ...resolvers },
  });
  api.attachPermissions(['s3']);

  // Show the AppSync API Id in the output
  stack.addOutputs({
    ApiId: api.apiId,
    APiUrl: api.url,
    UserPoolId: auth.userPoolId,
    UserPoolClientId: auth.userPoolClientId,
  });
}


Solution

  • This is most likely because "@graphql-tools/merge": "^8.2.12", is a devDependency in the package and is not available by default.

    Not sure if this is intended to reduce package size for people not using it.

    Anyway if you want a quick fix you can always add it to your package.json.

    Solution : -

    yarn add --dev @graphql-tools/merge
    

    or

    npm install -D @graphql-tools/merge
    

    this will solve the problem.