Skip to content

Commit 0ddc2bd

Browse files
committed
Add tests and changelog entries
1 parent c66b7bb commit 0ddc2bd

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Upcoming
44

55
- `apollo`
6-
- <First `apollo` related entry goes here>
6+
- Add support for `localSchemaFile` for federated service commands [#1489](https://github.com/apollographql/apollo-tooling/pull/1489)
77
- `apollo-codegen-core`
88
- <First `apollo-codegen-core` related entry goes here>
99
- `apollo-codegen-flow`
@@ -19,7 +19,8 @@
1919
- `apollo-graphql`
2020
- <First `apollo-graphql` related entry goes here>
2121
- `apollo-language-server`
22-
- <First `apollo-language-server` related entry goes here>
22+
- Remove old federation-info provider [#1489](https://github.com/apollographql/apollo-tooling/pull/1489)
23+
- Support using local schema files for checks/pushes of federated services [#1489](https://github.com/apollographql/apollo-tooling/pull/1489)
2324
- `apollo-tools`
2425
- <First `apollo-tools` related entry goes here>
2526
- `vscode-apollo`
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { FileSchemaProvider } from "../file";
2+
import * as path from "path";
3+
import * as fs from "fs";
4+
import { Debug } from "../../../utilities";
5+
import { isDone } from "nock";
6+
7+
const makeNestedDir = dir => {
8+
if (fs.existsSync(dir)) return;
9+
10+
try {
11+
fs.mkdirSync(dir);
12+
} catch (err) {
13+
if (err.code == "ENOENT") {
14+
makeNestedDir(path.dirname(dir)); //create parent dir
15+
makeNestedDir(dir); //create dir
16+
}
17+
}
18+
};
19+
20+
const deleteFolderRecursive = path => {
21+
// don't delete files on azure CI
22+
if (process.env.AZURE_HTTP_USER_AGENT) return;
23+
24+
if (fs.existsSync(path)) {
25+
fs.readdirSync(path).forEach(function(file, index) {
26+
var curPath = path + "/" + file;
27+
if (fs.lstatSync(curPath).isDirectory()) {
28+
// recurse
29+
deleteFolderRecursive(curPath);
30+
} else {
31+
// delete file
32+
fs.unlinkSync(curPath);
33+
}
34+
});
35+
fs.rmdirSync(path);
36+
}
37+
};
38+
39+
const writeFilesToDir = (dir: string, files: Record<string, string>) => {
40+
Object.keys(files).forEach(key => {
41+
if (key.includes("/")) makeNestedDir(path.dirname(key));
42+
fs.writeFileSync(`${dir}/${key}`, files[key]);
43+
});
44+
};
45+
46+
describe("FileSchemaProvider", () => {
47+
let dir, dirPath;
48+
49+
// set up a temp dir
50+
beforeEach(() => {
51+
dir = fs.mkdtempSync("__tmp__");
52+
dirPath = `${process.cwd()}/${dir}`;
53+
});
54+
55+
// clean up our temp dir
56+
afterEach(() => {
57+
if (dir) deleteFolderRecursive(dir);
58+
dir = dirPath = undefined;
59+
});
60+
61+
it("finds and loads sdl from graphql file", async () => {
62+
const writtenSDL = `
63+
extend type Query {
64+
myProduct: Product
65+
}
66+
67+
type Product @key(fields: "id") {
68+
id: ID
69+
sku: ID
70+
name: string
71+
}
72+
`;
73+
writeFilesToDir(dir, {
74+
"schema.graphql": writtenSDL
75+
});
76+
77+
const provider = new FileSchemaProvider({ path: dir + "/schema.graphql" });
78+
const sdl = await provider.resolveFederatedServiceSDL();
79+
expect(sdl).toEqual(writtenSDL);
80+
});
81+
82+
it("errors when sdl file is not a graphql file", async () => {
83+
const toWrite = `
84+
module.exports = \`
85+
extend type Query {
86+
myProduct: Product
87+
}
88+
89+
type Product @key(fields: "id") {
90+
id: ID
91+
sku: ID
92+
name: string
93+
}\`
94+
`;
95+
writeFilesToDir(dir, {
96+
"schema.js": toWrite
97+
});
98+
99+
const errorSpy = jest.spyOn(Debug, "error");
100+
101+
// noop -- just silence the error
102+
errorSpy.mockImplementationOnce(() => {});
103+
104+
const provider = new FileSchemaProvider({ path: dir + "/schema.js" });
105+
const sdl = await provider.resolveFederatedServiceSDL();
106+
expect(errorSpy).toBeCalledTimes(1);
107+
});
108+
});

0 commit comments

Comments
 (0)