Hey all - how have you all solved the multiple sch...
# help
c
Hey all - how have you all solved the multiple schema file problem with sst? I’ve looked into
graphql-tools
and also see there are a few experimental methods in the appsync sdk for creating fields, types, etc… but I was hoping to just do some sort of simple concatenation (without being too low level) and pass it into my AppSyncApi config.
Copy code
graphqlApi: {
  schema:  'src/schema/schema.graphql',
}
Any ideas here? Hoping to have a structure something like
Copy code
src/schema/
  -- project.schema
  -- other-entity.schema
f
Hey @colin, does AppSync support multiple schema files? I’m asking b/c if AppSync supports it and SST doesn’t, we should add support for it.
c
Heya @Frank - yeah our old serverless app did support multiple files. it looked something like this in our
serverless.yml
file
Copy code
appSync:
  name:  apinamehere
  schema:
    - types/base.graphql
    - types/employer.graphql
    - types/projects.graphql
    ...
f
I see. Do you know if SLS is merging them into a single schema? Or AppSync takes multiple natively?
Hey @colin, I created a beta release with multi-schema support. You can do:
Copy code
new sst.AppSyncApi(this, "AppSyncApi", {
  graphqlApi: {
    schema: [
      "src/appsync/schema.graphql",
      "src/appsync/schema2.graphql",
    ],
  },
  ...
})
And internally they get merged using @graphql-tools/merge.
To update, run
Copy code
npm install --save --save-exact @serverless-stack/cli@0.32.2-next.0+723d9cb3 @serverless-stack/resources@0.32.2-next.0+723d9cb3
Give it and let me know if it works for you. I will cut an official release with this.
a
We use
@graphql-tools/merge
to generate one schema atm. Happy to share a snippet if you need
c
@Frank Awesome! fwiw I think SLS is just doing a big concat on those files so the graphql-tools seems good to me. Ill check this out today!
@Alex Price rad - going to test the beta release but thank you! this I think will be my backup plan
f
Lemme share what I did, maybe @Alex Price can spot some holes 😁
Copy code
import { print } from "graphql";
import { mergeTypeDefs } from "@graphql-tools/merge";
import { loadFilesSync } from "@graphql-tools/load-files";

// Schemas is an array of file names
const mergedSchema = mergeTypeDefs(
  loadFilesSync(schemas)
);

// Write the merged schema to a file
fs.writeFileSync("merged.graphql", print(mergedSchema));

// Create the AppSync schema using the file
appsync.Schema.fromAsset("merged.graphql");
c
Worked well for me @Frank, thank you! What are your thoughts on adding a call to
graphql/validate
for the schema before it gets set as
mainSchema
. I had a bad path and it took me until the CloudFormation error from
sst start
to realize it. something like below. I can add some tests for this if its something you’d want to do.
Copy code
const errors = validate(mergedSchema)
if (errors.length > 0) throw new Error(`Invalid schema: ${errors}`)
f
Ah I can see that being useful! Can you show me an example of a bad path?
c
yep, I accidentally left off my
src
directory when initializing the AppSyncApi.
Copy code
// Single file schema (worked)
{
  schema: 'src/schema/base.graphql'
}

// Array schema (broken)
{
  schema: ['schema/base.graphql', 'schema/project.graphql']
}
I’m thinking this gave me a “blank” schema but took me a second to realize.
f
Ah got it. It should at least catch missing files.
Do you think
validate
would be useful if we checked for missing files?
c
Thats a good question, even though they are two slightly different problems. I think it could be useful to have both some sort of error for missing files (via a node
fs.access()
call? not sure here) as well as a graphql validation call to catch small typos and things… but its a little more overhead on the build process
f
Yup that makes sense. Let me put in the missing files check first.
Hey @colin, just a headsup, the “@graphql-tools” modules require Node.js 12+. I just annouced in #sst that we will deprecate Node.js 10 support starting next week. If no objections from the community, I will be able to release this next week.
c
Sweet!
f
Hey @colin, v0.39.0 now officially supports merging schemas 🎉
c
sweet!