Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit a240803

Browse files
committed
[Transform] Ported lots more code
Moving closer and closer to classic/compat mode being supported. Also fixed some minor stylistic issues in the generated code.
1 parent 9bd30da commit a240803

15 files changed

+2318
-29
lines changed

transform/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"devDependencies": {
2828
"@types/jest": "^22.0.1",
2929
"@types/node": "^9.3.0",
30-
"async-file": "^2.0.2",
3130
"jest": "^22.1.4",
3231
"ts-jest": "^22.0.1"
3332
},
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { buildSchema } from "graphql";
2+
3+
export const SCHEMA_EXTENSION = `directive @relay(
4+
# Marks this fragment spread as being deferrable such that it loads after
5+
# other portions of the view.
6+
deferrable: Boolean,
7+
8+
# Marks a connection field as containing nodes without 'id' fields.
9+
# This is used to silence the warning when diffing connections.
10+
isConnectionWithoutNodeID: Boolean,
11+
12+
# Marks a fragment as intended for pattern matching (as opposed to fetching).
13+
# Used in Classic only.
14+
pattern: Boolean,
15+
16+
# Marks a fragment as being backed by a GraphQLList.
17+
plural: Boolean,
18+
19+
# Marks a fragment spread which should be unmasked if provided false
20+
mask: Boolean = true,
21+
22+
# Selectively pass variables down into a fragment. Only used in Classic.
23+
variables: [String!],
24+
) on FRAGMENT_DEFINITION | FRAGMENT_SPREAD | INLINE_FRAGMENT | FIELD`;
25+
26+
export const GraphQLRelayDirective = buildSchema(
27+
SCHEMA_EXTENSION + '\ntype Query { x: String }',
28+
).getDirective('relay');
29+
30+
if (!GraphQLRelayDirective) {
31+
throw new Error('Failed to create GraphQLRelayDirective.');
32+
}

transform/src/Options.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphQLSchema, buildClientSchema, buildASTSchema, parse } from "graphql";
2+
import * as fs from 'fs';
13

24
export interface Options {
35
artifactDirectory?: string;
@@ -7,3 +9,39 @@ export interface Options {
79
buildCommand?: string;
810
isDevelopment?: boolean;
911
}
12+
13+
export interface NormalizedOptions {
14+
artifactDirectory?: string;
15+
compat?: boolean;
16+
schema?: GraphQLSchema;
17+
isDevVariable?: string;
18+
buildCommand?: string;
19+
isDevelopment?: boolean;
20+
}
21+
22+
const dotJsonLength = '.json'.length;
23+
const dotGraphQLLength = '.graphql'.length;
24+
25+
export function readGraphQLSchema(schemaPath: string): GraphQLSchema {
26+
const contents = fs.readFileSync(schemaPath, { encoding: 'utf8' });
27+
if (schemaPath.substring(schemaPath.length - dotJsonLength) === '.json') {
28+
const json = JSON.parse(contents);
29+
if (json.__schema) {
30+
return buildClientSchema(json);
31+
}
32+
if (json.data && json.data.__schema) {
33+
return buildClientSchema(json.data);
34+
}
35+
throw new Error('Expected data file to contain a JSON encoded GraphQLSchema');
36+
} else if (schemaPath.substring(schemaPath.length - dotGraphQLLength) === '.graphql') {
37+
return buildASTSchema(parse(contents));
38+
}
39+
throw new Error('Unsupported file. schema option only supports json and graphql file extensions');
40+
}
41+
42+
export function normalizeOptions(options: Options): NormalizedOptions {
43+
return {
44+
...options,
45+
schema: options.schema ? readGraphQLSchema(options.schema) : undefined,
46+
};
47+
}

0 commit comments

Comments
 (0)