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 relay-config library #4780

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PROJECT_ROOT>/compiler/.*
<PROJECT_ROOT>/.*/node_modules/.*
.*/node_modules/resolve/test/resolver/malformed_package_json/package.json
<PROJECT_ROOT>/.*/__tests__/.*/package.json

[options]
module.system=haste
Expand Down
58 changes: 58 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const babel = require('gulp-babel');
const header = require('gulp-header');
const rename = require('gulp-rename');
const gulpUtil = require('gulp-util');
const jsonSchemaToTypescript = require('json-schema-to-typescript');
const path = require('path');
const stream = require('stream');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');

Expand Down Expand Up @@ -211,8 +213,63 @@ const builds = [
},
],
},
{
package: 'relay-config',
exports: {
index: 'index.js',
},
bundles: [
{
entry: 'index.js',
output: 'relay-config',
libraryName: 'RelayConfig',
libraryTarget: 'commonjs2',
target: 'node',
noMinify: true, // Note: uglify can't yet handle modern JS
},
],
},
];

const relayConfig = gulp.parallel(
function copyCompilerConfigSchema() {
return gulp
.src(['relay-compiler-config-schema.json'], {
cwd: path.join('compiler/crates/relay-compiler'),
})
.pipe(gulp.dest(path.join(DIST, 'relay-config')));
},
function copyTypescriptTypes() {
return gulp
.src(['index.d.ts'], {
cwd: path.join(PACKAGES, 'relay-config'),
})
.pipe(gulp.dest(path.join(DIST, 'relay-config')));
},
function generateConfigTypescriptTypes() {
return gulp
.src(['relay-compiler-config-schema.json'], {
cwd: path.join('compiler/crates/relay-compiler'),
})
.pipe(
new stream.Transform({
objectMode: true,
transform(file, _, callback) {
jsonSchemaToTypescript
.compile(JSON.parse(file.contents.toString()))
.then(data => {
file.contents = Buffer.from(data);
callback(null, file);
})
.catch(error => callback(error, null));
},
}),
)
.pipe(rename('RelayConfig.d.ts'))
.pipe(gulp.dest(path.join(DIST, 'relay-config')));
},
);

const modules = gulp.parallel(
...builds.map(
build =>
Expand All @@ -225,6 +282,7 @@ const modules = gulp.parallel(
.pipe(gulp.dest(path.join(DIST, build.package, 'lib')));
},
),
relayConfig,
);

const flowDefs = gulp.parallel(
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@babel/traverse": "^7.14.0",
"@babel/types": "^7.0.0",
"@jest/create-cache-key-function": "^29.7.0",
"@rushstack/node-core-library": "^5.8.0",
"babel-eslint": "^10.1.0",
"babel-plugin-macros": "^2.0.0",
"babel-plugin-syntax-hermes-parser": "0.23.1",
Expand All @@ -47,6 +48,7 @@
"del": "6.0.0",
"eslint": "^8.57.0",
"eslint-config-fbjs": "4.0.0",
"json-schema-to-typescript": "^13.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually use this to generate typescript types, or just for json-schema runtime validation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using in a gulp task in the build to automatically generate from the schema file so we don't need to manually keep the two in sync

"eslint-plugin-babel": "5.3.1",
"eslint-plugin-ft-flow": "2.0.1",
"eslint-plugin-import": "^2.26.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/relay-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# relay-config

A utility library for loading and saving Relay compiler config files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "my-app",
"version": "1.0.0",
"relay": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-app",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {language: 'typescript', schema: 'schema.graphql'};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-app",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"language": "typescript",
"schema": "schema.graphql"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-app",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "my-app",
"version": "1.0.0",
"relay": {
"language": "typescript",
"schema": "schema.graphql"
}
}
52 changes: 52 additions & 0 deletions packages/relay-config/__tests__/loadConfig-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/

'use strict';

const loadConfig = require('../loadConfig');
const path = require('path');

describe('loadConfig', () => {
it('Returns undefined if there is no config available', () => {
expect(
loadConfig(path.join(__dirname, 'fixtures/no-config')),
).toBeUndefined();
});

it('Fails if the config is invalid', () => {
expect(() =>
loadConfig(path.join(__dirname, 'fixtures/invalid-config')),
).toThrow();
});

it('Properly loads relay.config.js files', () => {
expect(loadConfig(path.join(__dirname, 'fixtures/js-config'))).toEqual({
language: 'typescript',
schema: 'schema.graphql',
});
});

it('Properly loads relay.config.json files', () => {
expect(loadConfig(path.join(__dirname, 'fixtures/json-config'))).toEqual({
language: 'typescript',
schema: 'schema.graphql',
});
});

it('Properly loads package.json config', () => {
expect(
loadConfig(path.join(__dirname, 'fixtures/package-json-config')),
).toEqual({
language: 'typescript',
schema: 'schema.graphql',
});
});
});
21 changes: 21 additions & 0 deletions packages/relay-config/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall relay
*/

import { ConfigFile as RelayConfig } from './RelayConfig';

declare function loadConfig(): Promise<RelayConfig>;

declare function saveConfig(
config: RelayConfig,
folder: string,
configType: 'relay.config.js' | 'relay.config.json' | 'package.json',
): Promise<void>;

export { loadConfig, saveConfig, RelayConfig };
16 changes: 16 additions & 0 deletions packages/relay-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/

'use strict';

const loadConfig = require('./loadConfig');

module.exports = {loadConfig};
32 changes: 32 additions & 0 deletions packages/relay-config/loadConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/

'use strict';

const loadSchema = require('./loadSchema');
const cosmiconfig = require('cosmiconfig');

function loadConfig(folder?: string): any | void {
const schema = loadSchema();

const result = cosmiconfig('relay', {
searchPlaces: ['relay.config.js', 'relay.config.json', 'package.json'],
}).searchSync(folder);
if (!result) {
return;
}
const {config, filepath} = result;

schema.validateObject(config, filepath);
return config;
}

module.exports = loadConfig;
35 changes: 35 additions & 0 deletions packages/relay-config/loadSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/

'use strict';

const {JsonSchema} = require('@rushstack/node-core-library');
const path = require('path');

function loadSchema(): any {
return JsonSchema.fromFile(
path.join(__dirname, 'relay-compiler-config-schema.json'),
{
customFormats: {
uint8: {
type: 'number',
validate: data => data >= 0 && data <= 255,
},
uint: {
type: 'number',
validate: data => data >= 0 && data <= Number.MAX_SAFE_INTEGER,
},
},
},
);
}

module.exports = loadSchema;
26 changes: 26 additions & 0 deletions packages/relay-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "relay-config",
"description": "A utility library for loading and saving Relay compiler config files.",
"version": "17.0.0",
"keywords": [
"graphql",
"relay"
],
"license": "MIT",
"homepage": "https://relay.dev",
"bugs": "https://github.com/facebook/relay/issues",
"repository": {
"type": "git",
"url": "https://github.com/facebook/relay.git",
"directory": "packages/relay-config"
},
"dependencies": {
"@rushstack/node-core-library": "^5.8.0",
"cosmiconfig": "^5.0.5"
},
"directories": {
"": "./"
},
"main": "index.js",
"types": "index.d.ts"
}
Loading