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

Support multiple schema files with localSchemaFile #1500

Merged
merged 7 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Upcoming

- `apollo`
- <First `apollo` related entry goes here>
- Support multiple `localSchemaFile`s [#1500](https://github.com/apollographql/apollo-tooling/pull/1500)
- `apollo-codegen-core`
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-codegen-flow`
Expand All @@ -21,6 +21,7 @@
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- Support multiple `localSchemaFile`s [#1500](https://github.com/apollographql/apollo-tooling/pull/1500)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
49 changes: 17 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/apollo-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@endemolshinegroup/cosmiconfig-typescript-loader": "^1.0.0",
"apollo-datasource": "^0.6.0",
"apollo-env": "file:../apollo-env",
"apollo-graphql": "file:../apollo-graphql",
"apollo-link": "^1.2.3",
"apollo-link-context": "^1.0.9",
"apollo-link-error": "^1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface RemoteServiceConfig {

export interface LocalServiceConfig {
name: ServiceID;
localSchemaFile: string;
localSchemaFile: string | string[];
}

export interface EngineConfig {
Expand Down
51 changes: 42 additions & 9 deletions packages/apollo-language-server/src/providers/schema/file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// FileSchemaProvider (FileProvider (SDL || IntrospectionResult) => schema)
import { GraphQLSchema, buildClientSchema, Source, buildSchema } from "graphql";
import {
GraphQLSchema,
buildClientSchema,
Source,
buildSchema,
printSchema,
parse
} from "graphql";
import { readFileSync } from "fs";
import { extname, resolve } from "path";
import { GraphQLSchemaProvider, SchemaChangeUnsubscribeHandler } from "./base";
import { NotificationHandler } from "vscode-languageserver";
import { buildSchemaFromSDL } from "apollo-graphql";

export interface FileSchemaProviderConfig {
path: string;
path?: string;
paths?: string[];
}
// XXX file subscription
export class FileSchemaProvider implements GraphQLSchemaProvider {
Expand All @@ -16,7 +25,30 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {

async resolveSchema() {
if (this.schema) return this.schema;
const { path } = this.config;
const { path, paths } = this.config;

// load each path and get sdl string from each, if a list, concatenate them all
const documents = path
? [this.loadFileAndGetDocument(path)]
: paths
? paths.map(this.loadFileAndGetDocument)
: undefined;

if (!documents)
throw new Error(
`Schema could not be loaded for [${
path ? path : paths ? paths.join(", ") : "undefined"
}]`
);

this.schema = buildSchemaFromSDL(documents);

if (!this.schema) throw new Error(`Schema could not be loaded for ${path}`);
return this.schema;
}

// load a graphql file or introspection result and return the GraphQL DocumentNode
loadFileAndGetDocument(path: string) {
let result;
try {
result = readFileSync(path, {
Expand All @@ -28,7 +60,7 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {

const ext = extname(path);

// an actual introspectionQuery result
// an actual introspectionQuery result, convert to DocumentNode
if (ext === ".json") {
const parsed = JSON.parse(result);
const __schema = parsed.data
Expand All @@ -37,13 +69,14 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {
? parsed.__schema
: parsed;

this.schema = buildClientSchema({ __schema });
const schema = buildClientSchema({ __schema });
return parse(printSchema(schema));
} else if (ext === ".graphql" || ext === ".graphqls" || ext === ".gql") {
const uri = `file://${resolve(path)}`;
this.schema = buildSchema(new Source(result, uri));
return parse(result);
}
if (!this.schema) throw new Error(`Schema could not be loaded for ${path}`);
return this.schema;
throw new Error(
"File Type not supported for schema loading. Must be a .json, .graphql, .gql, or .graphqls file"
);
}

onSchemaChange(
Expand Down
13 changes: 10 additions & 3 deletions packages/apollo-language-server/src/providers/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ export function schemaProviderFromConfig(

if (config.client.service) {
if (isLocalServiceConfig(config.client.service)) {
return new FileSchemaProvider({
path: config.client.service.localSchemaFile
});
const isListOfSchemaFiles = Array.isArray(
config.client.service.localSchemaFile
);
return new FileSchemaProvider(
isListOfSchemaFiles
? { paths: config.client.service.localSchemaFile as string[] }
: {
path: config.client.service.localSchemaFile as string
}
);
}

return new IntrospectionSchemaProvider(config.client.service);
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"],
"references": [{ "path": "../apollo-tools" }]
"references": [{ "path": "../apollo-tools" }, { "path": "../apollo-graphql" }]
}
3 changes: 2 additions & 1 deletion packages/apollo/src/commands/client/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default class Generate extends ClientCommand {
}),
localSchemaFile: flags.string({
description:
"Path to your local GraphQL schema file (introspection result or SDL)"
"Path to your local GraphQL schema file (introspection result or SDL)",
multiple: true
}),
addTypename: flags.boolean({
description:
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo/src/commands/service/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ export default class ServiceCheck extends ProjectCommand {
}),
localSchemaFile: flags.string({
description:
"Path to your local GraphQL schema file (introspection result or SDL)"
"Path to your local GraphQL schema file (introspection result or SDL)",
multiple: true
}),
markdown: flags.boolean({
description: "Output result in markdown.",
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo/src/commands/service/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export default class ServicePush extends ProjectCommand {
}),
localSchemaFile: flags.string({
description:
"Path to your local GraphQL schema file (introspection result or SDL)"
"Path to your local GraphQL schema file (introspection result or SDL)",
multiple: true
}),
federated: flags.boolean({
char: "f",
Expand Down