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

Config does not support multiple schemas #588

Closed
3 tasks done
jhalborg opened this issue Sep 10, 2018 · 4 comments
Closed
3 tasks done

Config does not support multiple schemas #588

jhalborg opened this issue Sep 10, 2018 · 4 comments
Labels
📝 documentation 🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository

Comments

@jhalborg
Copy link

  • 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

@ghost ghost added blocking 📝 documentation 🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository labels Sep 10, 2018
@WaldoJeffers
Copy link
Contributor

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

@peeter-tomberg
Copy link

peeter-tomberg commented Nov 21, 2018

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,

@mbinic
Copy link

mbinic commented Feb 12, 2019

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 :)

@mbinic
Copy link

mbinic commented Feb 12, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📝 documentation 🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository
Projects
None yet
Development

No branches or pull requests

5 participants