apollographql / apollo-tooling

✏️ Apollo CLI for client tooling (Mostly replaced by Rover)

Home Page:https://apollographql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Config does not support multiple schemas

jhalborg opened this issue · comments

  • has-reproduction
  • docs
  • blocking

It seems that it's not possible to have multiple schemas in the Apollo Config, even though the docs says so.

Following the docs, I created the following setup:

"schemas": {
      "serverSchema": {
        "schema": "downloadedSchema.json",
        "endpoint": "https://my.staging.url.com/graphql",
      },
      "clientSchema": {
        "extends": "serverSchema",
        "clientSide": true,
        "schema": "mergedSchema.json",
        "endpoint": "./src/localState/clientSchema.graphql",
      }
    },
    "queries": [
      {
        "schema": "clientSchema",
        "includes": [
          "**/*.tsx",
          "src/**/*.{ts,tsx}"
        ],
        "excludes": [
          "node_modules/**"
        ]
      }
    ]

It fails with the following error output:

$ npx apollo schema:download schema.json --config=package.json
 ✔ Loading Apollo config
 ✖ Fetching current schema
   → More than one schema found.
   Saving schema to schema.json
 ›   Error: More than one schema found.
error Command failed with exit code 2.

Furthermore, I'm in doubt wether the schema prop is for where to ouput the resulting .json. schema, or something else? A more complete example under the readme would be helpful

Having the same issue after having been misled by the docs :/ I resorted to using as many configuration files as schemas...

After a bit of abuse, I got it working.

Package.json:

  "apollo": {
    "schemas": {
      "serverSchema": {
        "schema": "./data/schema.json"
      },
      "clientSchema": {
        "schema": "./data/schema.local.graphql",
        "clientSide": true,
        "extends": "serverSchema"
      }
    },
    "queries": [
      {
        "schema": "clientSchema",
        "includes": [
          "./src/client/**/*.tsx",
          "./src/client/**/*.ts"
        ]
      }
    ]
  },

This is the script I use to generate data/schema.json:

const fs = require('fs');
const request = require('request');

const { buildClientSchema, introspectionQuery, printSchema } = require('graphql/utilities');

module.exports = (graphEndpoint, schemaPath) => {
  const options = {
    uri: graphEndpoint,
    agentOptions: {
      keepAlive: true,
      keepAliveMsecs: 15000
    },
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ query: introspectionQuery }),
  };
  const requestPromise = new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error);
      }
      else {
        resolve(JSON.parse(body));
      }
    })
  });
  return requestPromise
    .then(schemaJSON => {
      fs.writeFileSync(`${schemaPath}.json`, JSON.stringify(schemaJSON, null, 2), { mode: '777' });
      const graphQLSchema = buildClientSchema(schemaJSON.data);
      fs.writeFileSync(`${schemaPath}.graphqls`, printSchema(graphQLSchema), { mode: '777' });
    });
};

Also how I run the actual apollo codegen CLI:

apollo codegen:generate --config package.json --addTypename --tagName graphqlTag --target typescript --outputFlat ./src/client/GeneratedQueryTypes.d.ts,

You could also use graphql-code-generator's schema ast plugin to stitch the schemas into one for the config. Of course, this wouldn't help you when editing one of those schema files in VS Code.

With frameworks like Loona, or just plain splitting the client schema into multiple files, using wildcards/glob would be very useful.

As a suggestion to make the config (mostly for the VS Code extension) less frustrating to set up - the tolling could introduce some sensible defaults, e.g. consider *.schema.{graphql,gql} files as part of the schema, and others as queries without even having the config. This is just my humble opinion though, haven't been in this for too long, so I might just be talking nonsense :)

Oops, now that I look at it, it seems #841 is more relevant, since the configuration format has changed (https://www.apollographql.com/docs/references/apollo-config.html). Maybe close this issue then?