Closed jhalborg closed 5 years ago
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?
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:
It fails with the following error output:
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