Skip to content

Commit 6f1ea63

Browse files
committed
disable validation of directives in SDL (#301)
1 parent 6ec9910 commit 6f1ea63

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/components/IntrospectionModal.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import TextField from '@mui/material/TextField';
1212
import Tooltip from '@mui/material/Tooltip';
1313
import Typography from '@mui/material/Typography';
1414
import Grid from '@mui/material/Unstable_Grid2';
15-
import { buildSchema, introspectionFromSchema } from 'graphql/utilities';
1615
import { useState } from 'react';
1716

1817
import { voyagerIntrospectionQuery } from '../utils/introspection-query';
18+
import { sdlToIntrospection } from '../utils/sdl-to-introspection';
1919

2020
enum InputType {
2121
Presets = 'Presets',
@@ -109,7 +109,7 @@ export function IntrospectionModal(props: IntrospectionModalProps) {
109109
onChange(JSON.parse(jsonText).data);
110110
break;
111111
case InputType.SDL:
112-
onChange(introspectionFromSchema(buildSchema(sdlText)));
112+
onChange(sdlToIntrospection(sdlText));
113113
break;
114114
}
115115
setSubmitted({ inputType, sdlText, jsonText, activePreset });

src/utils/sdl-to-introspection.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { parse } from 'graphql/language';
2+
import { buildSchema, introspectionFromSchema } from 'graphql/utilities';
3+
import { KnownDirectivesRule } from 'graphql/validation/rules/KnownDirectivesRule';
4+
import { specifiedSDLRules } from 'graphql/validation/specifiedRules';
5+
import { validateSDL } from 'graphql/validation/validate';
6+
7+
const validationRules = specifiedSDLRules.filter(
8+
// Many consumes/produces SDL files with custom directives and without defining them.
9+
// This practice is contradict spec but is very widespread at the same time.
10+
(rule) => rule !== KnownDirectivesRule,
11+
);
12+
13+
export function sdlToIntrospection(sdl: string) {
14+
const documentAST = parse(sdl);
15+
const errors = validateSDL(documentAST, null, validationRules);
16+
if (errors.length !== 0) {
17+
throw new Error(errors.map((error) => error.message).join('\n\n'));
18+
}
19+
20+
const schema = buildSchema(sdl, { assumeValidSDL: true });
21+
return introspectionFromSchema(schema);
22+
}

tests/PageObjectModel.ts

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ class PlaywrightVoyagerPage {
4040
async waitForGraphToBeLoaded() {
4141
await this.graphLoadingAnimation.waitFor({ state: 'hidden' });
4242
}
43+
44+
async submitSDL(sdl: string) {
45+
const { changeSchemaDialog } = this;
46+
const { sdlTab } = changeSchemaDialog;
47+
48+
await changeSchemaDialog.openButton.click();
49+
await sdlTab.tab.click();
50+
await sdlTab.sdlTextArea.fill(sdl);
51+
await changeSchemaDialog.displayButton.click();
52+
await this.waitForGraphToBeLoaded();
53+
}
4354
}
4455

4556
class PlaywrightChangeSchemaDialog {

tests/demo.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ test('use custom SDL', async ({ page }) => {
7575
await expect(voyagerPage.page).toHaveScreenshot('display-sdl.png');
7676
});
7777

78+
test.only('use custom SDL with custom directives', async ({ page }) => {
79+
const voyagerPage = await gotoVoyagerPage(page);
80+
await voyagerPage.submitSDL('type Query @foo { bar: String @baz }');
81+
82+
await expect(voyagerPage.page).toHaveScreenshot(
83+
'display-sdl-with-unknown-directives.png',
84+
);
85+
});
86+
7887
test('use custom introspection', async ({ page }) => {
7988
const voyagerPage = await gotoVoyagerPage(page);
8089
const { changeSchemaDialog } = voyagerPage;
Loading

0 commit comments

Comments
 (0)