Skip to content

Commit

Permalink
Don't fail on non existing fragments
Browse files Browse the repository at this point in the history
[changelog:fixed]
  • Loading branch information
cdupuis committed Sep 24, 2020
1 parent d545b27 commit dbcb33b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/definition/subscription/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ export function inlineFragments(q: string, cwd: string): string {

if (FragmentExpression.test(q)) {
// Load all fragments
const fragments = fs
.readdirSync(fragmentDir)
.filter(f => f.endsWith(".graphql"))
.map(f => {
const content = fs
.readFileSync(p.join(fragmentDir, f))
.toString();
const graphql = gql(content);
return {
name: (graphql.definitions[0] as any).name.value,
kind: graphql.definitions[0].kind,
body: content.slice(
content.indexOf("{") + 1,
content.lastIndexOf("}") - 1,
),
};
})
.filter(f => f.kind === "FragmentDefinition");
const fragments = fs.pathExistsSync(fragmentDir)
? fs
.readdirSync(fragmentDir)
.filter(f => f.endsWith(".graphql"))
.map(f => {
const content = fs
.readFileSync(p.join(fragmentDir, f))
.toString();
const graphql = gql(content);
return {
name: (graphql.definitions[0] as any).name.value,
kind: graphql.definitions[0].kind,
body: content.slice(
content.indexOf("{") + 1,
content.lastIndexOf("}") - 1,
),
};
})
.filter(f => f.kind === "FragmentDefinition")
: [];

FragmentExpression.lastIndex = 0;
let result;
Expand Down
68 changes: 68 additions & 0 deletions test/definition/subscription/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from "assert";
import * as path from "path";
import { inlineFragments } from "../../../lib/definition/subscription/util";

describe("util", () => {
it("should not inline query spreads", () => {
const query = `query NpmRegistryProvider {
NpmRegistryProvider {
__typename
scope
url
... on NpmJSRegistryProvider {
_typenames
id
name
credential {
... on Password {
secret
}
owner {
login
}
id
}
}
... on GitHubNpmRegistryProvider {
_typenames
id
name
credential {
... on OAuthToken {
owner {
name
}
scopes
secret
}
}
gitHubAppInstallation {
id
owner
}
}
}
}`;
const result = inlineFragments(
query,
path.join(__dirname, "..", "..", "..", "graphql"),
);
assert.strictEqual(result, query);
});
});

0 comments on commit dbcb33b

Please sign in to comment.