aheissenberger / serverless-appsync-offline

Serverless AWS AppSync Offline Plugin - Allow to run AppSync locally for serverless framework
MIT License
90 stars 25 forks source link

Multiple schema files isn't working #39

Closed davidclochard closed 5 years ago

davidclochard commented 5 years ago

Hello,

i try to use multiple schema files, which have been implemented here to serverless-appsync-plugin : https://github.com/sid88in/serverless-appsync-plugin/pull/227

But it doesn't work, it logs this : Serverless: ERROR: Error: schema file: /home/dclochard/workspace/cleo-api/schema.graphql must exist

Globally, when i just try to override schema file path (even when i use only one), i got the same error.

Any solution ?

Thanks, David

barendb commented 5 years ago

@davidclochard we concat all the graphql files into a single schema.graphql file to get around this problem, and do this during build.

example: rm -rf schema.graphql && find ./src/api/schema -name "*.graphql" -o -name "schema.graphql" | xargs cat > ./schema.graphql

Then just ignore that schema.graphql in .gitignore

and in serverless.yml

schema:
  - 'schema.graphql'
barendb commented 5 years ago

FIY this is not a bug in serverless-appsync-offline, this package is merely a wrapper around https://github.com/ConduitVC/aws-utils

davidclochard commented 5 years ago

Thanks for the workaround @barendb !

inventionlabsSydney commented 5 years ago

Great work around @barendb 👍 saved my day!

stevearoonie commented 5 years ago

The above work-around did not work for me because I also had Queries and Mutations defined in each schema file. My solution was to create the following script "mergeSchema.js":``

const path = require("path");
const fs = require("fs");
const mergeGraphqlSchemas = require("merge-graphql-schemas");
const fileLoader = mergeGraphqlSchemas.fileLoader;
const mergeTypes = mergeGraphqlSchemas.mergeTypes;

const currentDir = process.cwd();
const typesArray = fileLoader(path.join(currentDir, "schema"), { recursive: true });
const schema = mergeTypes(typesArray, { all: true });
fs.writeFileSync(path.join(currentDir, "schema.graphql"), schema);

and then call it from npm:

...
  "scripts": {
    "merge": "node ./scripts/mergeSchema",
    ...
  }
...
inventionlabsSydney commented 5 years ago

@stevearoonie I ended up doing the following: package.json

"scripts": {
    "watch-graph": "npm-watch recompileGraphQL",
    "recompileGraphQL": "./build_scripts/recompile_schema.sh"
 ..... 
},
....
  "devDependencies": {
    "npm-watch": "^0.6.0",
....
},
"watch": {
    "recompileGraphQL": {
      "patterns": [
        "graphql"
      ],
      "extensions": "graphql",
      "quiet": false,
      "legacyWatch": true,
      "runOnChangeOnly": true
    }
  }

Then I have a build scripts directory that has recompile_schema.sh and it will recompile all my schema files when a change happens:

#!/usr/bin/env bash
echo "Compiling Schema from Discovery"
echo "Removing current schema"
rm -rf schema.graphql
echo "Discoverying Schema..."
find ./graphql -name "*.graphql" -o -name "schema.graphql" | xargs cat > ./schema.graphql
echo "Attaching Extends to duplicate schema types... this is only in dev"
sed -i.bak "s/type Query/extend type Query/g" schema.graphql
awk '!x{x=sub("extend type Query","type Query")}7' schema.graphql > ./schema.graphql.temp
sed -i.bak "s/type Mutation/extend type Mutation/g" schema.graphql.temp
awk '!x{x=sub("extend type Mutation","type Mutation")}7' schema.graphql.temp > ./schema.graphql
rm schema.graphql.temp
rm schema.graphql.bak
rm schema.graphql.temp.bak
FILESIZE=$(stat -f%z "schema.graphql")
echo "Succesfully build schema file, which is now size of: $FILESIZE bytes"

This allows me to have a "schema.graphql" on my local system that recompiles and adds "extend" to the relevant things...

Then my serverless just uploads all the differences to appsync 👍

cyberwombat commented 5 years ago

@stevearoonie you can have multiple Query,Mutations and Subs in separate files by doing this:

// Main schema.graphql
type Query {
  _empty: String
}
type Mutation {
  _empty: String
}
type Subscription {
  _empty: String
}
schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}

// foo.graphql
extend type Query {
  myQuery...
}

Then they will concatenate properly.