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

Add a option to make deprecated input args visible #118

Merged
merged 2 commits into from
May 2, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ $ graphql-schema-diff --help
--left-schema-header Header to send to left remote schema source
--right-schema-header Header to send to right remote schema source
--sort-schema, -s Sort schemas prior to diffing
--input-value-deprecation Include deprecated input value fields when loading from URL

Examples
$ graphql-schema-diff https://example.com/graphql schema.graphql
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"diff2html": "^3.4.35",
"disparity": "^3.2.0",
"fs-extra": "^11.1.1",
"graphql": "^16.7.1",
"graphql": "^16.8.1",
"meow": "^9.0.0",
"node-fetch": "^2.6.11"
},
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from "path";
import { getDiff } from "../diff";
import { getIntrospectionQuery, parse, print } from "graphql";
import introspectionResponse from "./fixtures/introspectionResponse.json";
import { graphql, buildSchema } from "graphql";
import fs from "fs/promises";

describe("getDiff", () => {
describe("remote schema fetching", () => {
Expand Down Expand Up @@ -261,4 +263,63 @@ describe("getDiff", () => {
expect(result).toBeUndefined();
});
});

describe("input arg deprecation", () => {
it("introspection misses deprecated input args fields by default", async () => {
const localSchemaInputValueDeprecated = path.join(
__dirname,
"fixtures/localSchemaInputValueDeprecated.graphql",
);
const testRemoteSchemaLocation = "http://test/graphql";
const schema = buildSchema(
await fs.readFile(localSchemaInputValueDeprecated, "utf-8"),
);

nock(testRemoteSchemaLocation)
.post(/.*/)
.reply(200, (_uri, source: { query: string }) => {
return graphql({ schema, source: source.query, rootValue: {} });
});

const result = await getDiff(
localSchemaInputValueDeprecated,
testRemoteSchemaLocation,
);

expect(result).toBeDefined();

if (result) {
expect(result.diff).toContain("+ field1(testInput1: String): String");
}
});

it("introspection sees deprecated input args when inputValueDeprecation specified", async () => {
const localSchemaInputValueDeprecated = path.join(
__dirname,
"fixtures/localSchemaInputValueDeprecated.graphql",
);
const testRemoteSchemaLocation = "http://test/graphql";
const schema = buildSchema(
await fs.readFile(localSchemaInputValueDeprecated, "utf-8"),
);

nock(testRemoteSchemaLocation)
.post(/.*/)
.reply(200, (_uri, source: { query: string }) => {
return graphql({ schema, source: source.query, rootValue: {} });
});

const result = await getDiff(
testRemoteSchemaLocation,
localSchemaInputValueDeprecated,
{ inputValueDeprecation: true },
);

expect(result).toBeUndefined();
});

afterEach(() => {
nock.cleanAll();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Query {
field1(
testInput1: String
testInput2: Float @deprecated(reason: "test2 is deprecated")
): String
}
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const cli = meow(
--left-schema-header \t Header to send to left remote schema source
--right-schema-header \t Header to send to right remote schema source
--sort-schema, -s \t\t Sort schemas prior to diffing
--input-value-deprecation \t Include deprecated input value fields when loading from URL

Examples
$ graphql-schema-diff https://example.com/graphql schema.graphql
Expand Down Expand Up @@ -67,6 +68,9 @@ const cli = meow(
version: {
alias: "v",
},
inputValueDeprecation: {
type: "boolean",
},
},
},
);
Expand Down Expand Up @@ -124,6 +128,7 @@ getDiff(leftSchemaLocation, rightSchemaLocation, {
headers: parseHeaders(rightSchemaHeader),
},
sortSchema: cli.flags.sortSchema as boolean,
inputValueDeprecation: cli.flags.inputValueDeprecation as boolean,
})
.then(async (result) => {
if (result === undefined) {
Expand Down
8 changes: 6 additions & 2 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "graphql";
import { lexicographicSortSchema } from "graphql/utilities";
import disparity from "disparity";
import { loadSchema } from "@graphql-tools/load";
import { LoadSchemaOptions, loadSchema } from "@graphql-tools/load";
import { UrlLoader } from "@graphql-tools/url-loader";
import { JsonFileLoader } from "@graphql-tools/json-file-loader";
import { printSchemaWithDirectives } from "@graphql-tools/utils";
Expand All @@ -31,18 +31,22 @@ export interface DiffOptions {
};
headers?: Headers;
sortSchema?: boolean;
inputValueDeprecation?: boolean;
}

export async function getDiff(
leftSchemaLocation: string,
rightSchemaLocation: string,
options: DiffOptions = {},
): Promise<DiffResponse | undefined> {
const getSchemaOptions = (customHeaders?: Headers) => ({
const getSchemaOptions: (
customHeaders?: Headers,
) => Omit<LoadSchemaOptions, "loaders"> = (customHeaders) => ({
headers: { ...options.headers, ...customHeaders },
skipGraphQLImport: false,
descriptions: false,
customFetch: fetch,
inputValueDeprecation: options.inputValueDeprecation,
});
const leftSchemaOptions = getSchemaOptions(options.leftSchema?.headers);
const rightSchemaOptions = getSchemaOptions(options.rightSchema?.headers);
Expand Down