Skip to content

Commit

Permalink
Support jumping to definitions in schema (#508)
Browse files Browse the repository at this point in the history
* Support jumping to definitions in schema

* Fix failing unit tests

* Drop localSchema alias
  • Loading branch information
shadaj authored Jul 25, 2018
1 parent 5c0448f commit 143b540
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 95 deletions.
92 changes: 53 additions & 39 deletions packages/apollo-cli/src/commands/schema/__tests__/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
jest.mock("apollo-codegen-core/lib/localfs", () => {
return require("../../../__mocks__/localfs");
});

// this is because of herkou-cli-utils hacky mocking system on their console logger
import { stdout, mockConsole } from "heroku-cli-util";
import * as path from "path";
Expand All @@ -8,27 +12,29 @@ import gql from "graphql-tag";
import { ENGINE_URI } from "../../../engine";
import { VALIDATE_SCHEMA } from "../../../operations/validateSchema";

import { vol, fs as mockFS } from "apollo-codegen-core/lib/localfs";

const test = setup.do(() => mockConsole());
const ENGINE_API_KEY = "service:test:1234";
const hash = "12345";
const localSchema = { __schema: { fakeSchema: true } };
const fullSchema = execute(
buildSchema(
fs.readFileSync(path.resolve(__dirname, "./fixtures/schema.graphql"), {
encoding: "utf-8",
})
),
gql(introspectionQuery)
).data;
const schemaContents = fs.readFileSync(
path.resolve(__dirname, "./fixtures/schema.graphql"),
{
encoding: "utf-8"
}
);

const fullSchema = execute(buildSchema(schemaContents), gql(introspectionQuery))
.data;

const localSuccess = nock => {
nock
.post("/graphql", {
query: print(gql(introspectionQuery)),
operationName: "IntrospectionQuery",
variables: {},
variables: {}
})
.reply(200, { data: localSchema });
.reply(200, { data: fullSchema });
};

const engineSuccess = ({ schema, tag, results } = {}) => nock => {
Expand All @@ -38,15 +44,15 @@ const engineSuccess = ({ schema, tag, results } = {}) => nock => {
operationName: "CheckSchema",
variables: {
id: "test",
schema: schema || localSchema.__schema,
schema: schema || fullSchema.__schema,
tag: tag || "current",
gitContext: {
commit: /.+/i,
remoteUrl: /apollo-cli/i,
committer: /@/i,
},
committer: /@/i
}
},
query: print(VALIDATE_SCHEMA),
query: print(VALIDATE_SCHEMA)
})
.reply(200, {
data: {
Expand All @@ -57,33 +63,40 @@ const engineSuccess = ({ schema, tag, results } = {}) => nock => {
{
type: "NOTICE",
code: "DEPRECATION_ADDED",
description: "Field `User.lastName` was deprecated",
description: "Field `User.lastName` was deprecated"
},
{
type: "WARNING",
code: "FIELD_REMOVED",
description: "Field `User.firstName` removed",
description: "Field `User.firstName` removed"
},
{
type: "FAILURE",
code: "ARG_CHANGE_TYPE",
description: "Argument id on `Query.user` changed to ID!",
description: "Argument id on `Query.user` changed to ID!"
},
{
type: "NOTICE",
code: "FIELD_ADDED",
description: "Field `User.fullName` was added",
},
],
},
},
},
},
description: "Field `User.fullName` was added"
}
]
}
}
}
}
});
};

jest.setTimeout(25000);

beforeEach(() => {
vol.reset();
vol.fromJSON({
__blankFileSoDirectoryExists: ""
});
});

describe("successful checks", () => {
test
.nock("http://localhost:4000", localSuccess)
Expand Down Expand Up @@ -161,17 +174,17 @@ describe("successful checks", () => {
.post("/graphql", {
query: print(gql(introspectionQuery)),
operationName: "IntrospectionQuery",
variables: {},
variables: {}
})
.reply(200, { data: localSchema });
.reply(200, { data: fullSchema });
})
.nock(ENGINE_URI, engineSuccess())
.env({ ENGINE_API_KEY })
.command([
"schema:check",
"--endpoint=https://staging.example.com/graphql",
"--header=Authorization: 1234",
"--header=Hello: World",
"--header=Hello: World"
])
.exit(1)
.it(
Expand All @@ -184,16 +197,15 @@ describe("successful checks", () => {
);

test
.do(() =>
vol.fromJSON({
"introspection-result.json": JSON.stringify({ data: fullSchema })
})
)
.stdout()
.nock(ENGINE_URI, engineSuccess())
.env({ ENGINE_API_KEY })
.command([
"schema:check",
`--endpoint=${path.resolve(
__dirname,
"./fixtures/introspection-result.json"
)}`,
])
.command(["schema:check", "--endpoint=introspection-result.json"])
.exit(1)
.it(
"calls engine with a schema from an introspection result on the filesystem",
Expand All @@ -205,13 +217,15 @@ describe("successful checks", () => {
);

test
.do(() =>
vol.fromJSON({
"schema.graphql": schemaContents
})
)
.stdout()
.nock(ENGINE_URI, engineSuccess({ schema: fullSchema.__schema }))
.env({ ENGINE_API_KEY })
.command([
"schema:check",
`--endpoint=${path.resolve(__dirname, "./fixtures/schema.graphql")}`,
])
.command(["schema:check", "--endpoint=schema.graphql"])
.exit(1)
.it(
"calls engine with a schema from a schema file on the filesystem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { fs as mockFS, vol } from "apollo-codegen-core/lib/localfs";
const test = setup.do(() => mockConsole());
const ENGINE_API_KEY = "service:test:1234";
const hash = "12345";
const localSchema = { __schema: { fakeSchema: true } };
const schemaSource = fs.readFileSync(
path.resolve(__dirname, "./fixtures/schema.graphql"),
{
Expand All @@ -28,9 +27,7 @@ const schemaSource = fs.readFileSync(
const fullSchema = execute(buildSchema(schemaSource), gql(introspectionQuery))
.data;

const introspectionResult = fs.readFileSync(
path.resolve(__dirname, "./fixtures/introspection-result.json")
);
const introspectionResult = JSON.stringify({ data: fullSchema });

const localSuccess = nock => {
nock
Expand All @@ -39,7 +36,7 @@ const localSuccess = nock => {
operationName: "IntrospectionQuery",
variables: {}
})
.reply(200, { data: localSchema });
.reply(200, { data: fullSchema });
};

const engineSuccess = ({ schema, tag, result } = {}) => nock => {
Expand All @@ -48,7 +45,7 @@ const engineSuccess = ({ schema, tag, result } = {}) => nock => {
.post("/", {
operationName: "UploadSchema",
variables: {
schema: schema || localSchema.__schema,
schema: schema || fullSchema.__schema,
id: "test",
tag: tag || "current",
gitContext: {
Expand Down Expand Up @@ -166,7 +163,7 @@ describe("successful uploads", () => {
operationName: "IntrospectionQuery",
variables: {}
})
.reply(200, { data: localSchema });
.reply(200, { data: fullSchema });
})
.nock(ENGINE_URI, engineSuccess())
.env({ ENGINE_API_KEY })
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-cli/src/commands/schema/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Command, flags } from "@oclif/command";
import chalk from "chalk";
import { table, styledJSON } from "heroku-cli-util";
import * as Listr from "listr";
import { GraphQLError } from "graphql";
import {
GraphQLError,
parse,
introspectionQuery,
execute as graphql
} from "graphql";

import { toPromise, execute } from "apollo-link";

Expand Down Expand Up @@ -84,7 +89,8 @@ export default class SchemaCheck extends Command {

const variables = {
id: getIdFromKey(ctx.currentSchema.engineKey),
schema: ctx.schema,
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
// XXX hardcoded for now
tag: "current",
gitContext
Expand Down
7 changes: 6 additions & 1 deletion packages/apollo-cli/src/commands/schema/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { loadSchema } from "../../load-schema";

import { loadConfigStep } from "../../load-config";

import { execute, introspectionQuery, parse } from "graphql";

export default class SchemaDownload extends Command {
static description = "Download the schema from your GraphQL endpoint.";

Expand Down Expand Up @@ -73,7 +75,10 @@ export default class SchemaDownload extends Command {
task: async ctx => {
await promisify(fs.writeFile)(
args.output,
JSON.stringify(ctx.schema)
JSON.stringify(
(await execute(ctx.schema, parse(introspectionQuery))).data!
.__schema
)
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-cli/src/commands/schema/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Command, flags } from "@oclif/command";
import { table, styledJSON } from "heroku-cli-util";
import * as Listr from "listr";
import { toPromise, execute } from "apollo-link";
import { GraphQLError } from "graphql";
import {
GraphQLError,
parse,
introspectionQuery,
execute as graphql
} from "graphql";

import { UPLOAD_SCHEMA } from "../../operations/uploadSchema";
import { getIdFromKey, engineLink } from "../../engine";
Expand Down Expand Up @@ -79,7 +84,8 @@ export default class SchemaPublish extends Command {
)} to Apollo Engine`;
const gitContext = await gitInfo();
const variables = {
schema: ctx.schema,
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
tag,
gitContext,
id: getIdFromKey(ctx.currentSchema.engineKey)
Expand Down
12 changes: 2 additions & 10 deletions packages/apollo-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { fs, withGlobalFS } from "apollo-codegen-core/lib/localfs";

import * as fg from "glob";
import * as minimatch from "minimatch";
import {
GraphQLSchema,
buildClientSchema,
extendSchema,
visit,
buildASTSchema
} from "graphql";
import { GraphQLSchema, extendSchema, visit, buildASTSchema } from "graphql";
import { loadSchema } from "./load-schema";
import { loadQueryDocuments } from "apollo-codegen-core/lib/loading";

Expand Down Expand Up @@ -221,9 +215,7 @@ export async function resolveSchema(
)
: referredSchema.clientSide
? buildASTSchema(loadAsAST())
: buildClientSchema({
__schema: await loadSchema(referredSchema, config)
});
: await loadSchema(referredSchema, config);
}

export async function resolveDocumentSets(
Expand Down
Loading

0 comments on commit 143b540

Please sign in to comment.