Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sst build is not working with AppSync #228

Closed
romcok opened this issue Mar 11, 2021 · 8 comments
Closed

sst build is not working with AppSync #228

romcok opened this issue Mar 11, 2021 · 8 comments
Assignees

Comments

@romcok
Copy link

romcok commented Mar 11, 2021

Hi,

we have some problems with your framework to get working with AppSync. I think something is missing in the CDK/SST
synth mechanism and GraphQL schema is not copied to the .build directory during the build. It happens during sst build.
We only need to copy one file to the .build directory. Would be great if it worked with this service as well. Any ideas? 🤷‍♂️

Here is the code wich isn't working:

import * as cdk from '@aws-cdk/core'
import * as appsync from '@aws-cdk/aws-appsync'
import * as path from 'path'
import * as lambda from '@aws-cdk/aws-lambda-nodejs'
import * as _ from 'lodash'
import * as sst from "@serverless-stack/resources";

...
const GraphQLAPI = (scope: cdk.Construct) =>
  new appsync.GraphqlApi(scope, 'GraphQLAPI', {
    name: ResourceName('GraphQLAPI', scope),
    schema: appsync.Schema.fromAsset(path.join(__dirname, 'schema.gql')),
    xrayEnabled: true,
    logConfig: {
      excludeVerboseContent: false,
      fieldLogLevel: appsync.FieldLogLevel.ALL,
    }
  })

...
export const APIStack = (scope: sst.App, props: cdk.StackProps) => {
  const stack = new sst.Stack(scope, 'API', props)

  const graphqlApi = GraphQLAPI(stack)

  new cdk.CfnOutput(stack, 'GraphQLID', {
    value: graphqlApi.apiId
  })
  new cdk.CfnOutput(stack, 'GraphQLAPIURL', {
    value: graphqlApi.graphqlUrl
  })
  new cdk.CfnOutput(stack, 'GraphQLRealTimeURL', {
    value: graphqlApi.graphqlUrl.replace('api', 'realtime-api')
  })
  new cdk.CfnOutput(stack, 'GraphQLAPIKey', {
    value: graphqlApi.apiKey!
  })
}

And here is the error:

Error: ENOENT: no such file or directory, open '/Users/hello/dssp-ts/infrastructure/.build/lib/schema.gql'
    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)
    at new Schema (/Users/hello/dssp-ts/infrastructure/node_modules/@aws-cdk/aws-appsync/lib/schema.ts:64:25)
    at Function.fromAsset (/Users/hello/dssp-ts/infrastructure/node_modules/@aws-cdk/aws-appsync/lib/schema.ts:41:12)
    at GraphQLAPI (/Users/hello/dssp-ts/infrastructure/lib/api/index.ts:58:28)
    at APIStack (/Users/hello/dssp-ts/infrastructure/lib/api/index.ts:96:22)
    at Object.main (/Users/hello/dssp-ts/infrastructure/lib/index.ts:4:3)
    at Object.<anonymous> (/Users/hello/dssp-ts/infrastructure/.build/run.js:64:16)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

There was an error synthesizing your app.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! infrastructure@0.1.0 build: `sst build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the infrastructure@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/hello/.npm/_logs/2021-03-11T11_23_48_677Z-debug.log
@fwang
Copy link
Contributor

fwang commented Mar 11, 2021

Hi @romcok Thanks for reporting this!

May I bother you to share a repo with a stripped-down code that can reproduce this? I will take a look at this today.

@romcok
Copy link
Author

romcok commented Mar 11, 2021

Here is the minimal code to reproduce the problem https://github.com/romcok/sst-appsync-example. I'm not sure if it all works because I got stuck during the build process.
Many thanks for your help 🙏

@fwang
Copy link
Contributor

fwang commented Mar 11, 2021

Ah nice! Thanks for putting it together. I'm going to go get some sleep and then take a look at this. 💪

@fwang fwang self-assigned this Mar 11, 2021
@fwang fwang added this to the Ongoing issues & requests milestone Mar 11, 2021
@romcok
Copy link
Author

romcok commented Mar 12, 2021

Hi again,
I just found a temporary workaround when I use L1 Construct CfnGraphQLSchema and define the schema in code:

import * as cdk from '@aws-cdk/core'
import * as appsync from '@aws-cdk/aws-appsync'

export const GraphQLSchema = (scope: cdk.Construct, apiId: string) =>
  new appsync.CfnGraphQLSchema(scope, 'GraphQLSchema', {
    apiId,
    definition: `
      schema {
        query: Query
      }
      
      type Query {
        hello: String
        version: String
      }`
  })

So if you have trouble packing some kind of assets needed for CDK, try to find a suitable construct for this purpose.

@jayair
Copy link
Contributor

jayair commented Mar 14, 2021

@romcok I was playing around with AppSync today. I didn't run into this issue. Here's my repo for reference. It's pretty basic.

https://github.com/jayair/appsync-sst-app

I'm fairly new to AppSync. Any idea why it worked for my case?

@romcok
Copy link
Author

romcok commented Mar 14, 2021

Wow wow man, you solved it.
I don't know how, but I tried your repo and it's working. I couldn't even make work the local development. So I think there is something different in our code that spoils it when using SST. So we will do it according to your repository because this feature of local development via web sockets is ingenious.

Thank you both for your time, you saved a lot of ours and I've been keeping my fingers crossed for what you're doing. I've been watching you for a few years and your guide was my intro to the serverless world 🙏

I'll let you know if I find out where the problem was and you can add to the info that it also works with AppSync 💪

@jayair
Copy link
Contributor

jayair commented Mar 15, 2021

Wow! Really appreciate the kind words and support! Thank you!

@jayair
Copy link
Contributor

jayair commented Mar 15, 2021

I'll close this for now. Feel free to reopen it when you figure out what was breaking it for you.

@jayair jayair closed this as completed Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants