Skip to content
Closed
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
2 changes: 1 addition & 1 deletion packages/relay-compiler/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ foo {
}
```

- `GenerateRequisiteFieldTransform`: This optional, Relay-specific transform inserts `id` fields for globally identifiable objects and `__typename` fields wherever the type cannot be statically determined (e.g. for unions).
- `GenerateRequisiteFieldTransform`: This optional, Relay-specific transform inserts object identification fields (which is either an `ID!` field on the `Node` interface or defaults to `id`) and `__typename` fields wherever the type cannot be statically determined (e.g. for unions).
5 changes: 3 additions & 2 deletions packages/relay-compiler/codegen/RelayFileWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const writeLegacyFlowFile = require('./writeLegacyFlowFile');
const writeRelayGeneratedFile = require('./writeRelayGeneratedFile');

const {isOperationDefinitionAST} = require('GraphQLSchemaUtils');
const {generate} = require('RelayCodeGenerator');
const RelayCodeGenerator = require('RelayCodeGenerator');
const {Map: ImmutableMap} = require('immutable');

import type {RelayGeneratedNode} from 'RelayCodeGenerator';
Expand Down Expand Up @@ -144,11 +144,12 @@ class RelayFileWriter implements FileWriterInterface {
);

const compilerContext = new RelayCompilerContext(extendedSchema);
const codeGenerator = new RelayCodeGenerator(compilerContext.getNodeIDFieldName());
const compiler = new RelayCompiler(
this._baseSchema,
compilerContext,
this._config.compilerTransforms,
generate,
codeGenerator.generate.bind(codeGenerator),
);

const getGeneratedDirectory = definitionName => {
Expand Down
328 changes: 168 additions & 160 deletions packages/relay-compiler/core/RelayCodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
} from 'RelayConcreteNode';
import type {GeneratedNode} from 'RelayConcreteNode';
import type {Fragment, Root} from 'RelayIR';
import type {NodeVisitor} from 'RelayIRVisitor';
const {getRawType, isAbstractType, getNullableType} = GraphQLSchemaUtils;

/* eslint-disable no-redeclare */
Expand All @@ -40,185 +41,192 @@ declare function generate(node: Fragment): ConcreteFragment;
export type CompiledDocumentMap = Map<string, GeneratedNode>;
export type RelayGeneratedNode = ConcreteRoot | ConcreteFragment;

/**
* @public
*
* Converts a Relay IR node into a plain JS object representation that can be
* used at runtime.
*/
function generate(node: Root | Fragment): ConcreteRoot | ConcreteFragment {
invariant(
['Root', 'Fragment'].indexOf(node.kind) >= 0,
'RelayCodeGenerator: Unknown AST kind `%s`. Source: %s.',
node.kind,
getErrorMessage(node),
);
return RelayIRVisitor.visit(node, RelayCodeGenVisitor);
}
/* eslint-enable no-redeclare */
class RelayCodeGenerator {
_codeGeneratorVisitor: NodeVisitor;

const RelayCodeGenVisitor = {
leave: {
Root(node): ConcreteRoot {
return {
argumentDefinitions: node.argumentDefinitions,
kind: 'Root',
name: node.name,
operation: node.operation,
selections: flattenArray(node.selections),
};
},
constructor(idField: string) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Rather than having to pass the ‘Node ID’ field in, I’m wondering if I can revert this change and instead encode the ‘Node ID’ into the metadata of the selection 🤔

this._codeGeneratorVisitor = {
leave: {
Root(node): ConcreteRoot {
return {
argumentDefinitions: node.argumentDefinitions,
kind: 'Root',
name: node.name,
operation: node.operation,
selections: flattenArray(node.selections),
};
},

Fragment(node): ConcreteFragment {
return {
argumentDefinitions: node.argumentDefinitions,
kind: 'Fragment',
metadata: node.metadata || null,
name: node.name,
selections: flattenArray(node.selections),
type: node.type.toString(),
};
},
Fragment(node): ConcreteFragment {
return {
argumentDefinitions: node.argumentDefinitions,
kind: 'Fragment',
metadata: node.metadata || null,
name: node.name,
selections: flattenArray(node.selections),
type: node.type.toString(),
};
},

LocalArgumentDefinition(node): ConcreteArgumentDefinition {
return {
kind: 'LocalArgument',
name: node.name,
type: node.type.toString(),
defaultValue: node.defaultValue,
};
},
LocalArgumentDefinition(node): ConcreteArgumentDefinition {
return {
kind: 'LocalArgument',
name: node.name,
type: node.type.toString(),
defaultValue: node.defaultValue,
};
},

RootArgumentDefinition(node): ConcreteArgumentDefinition {
return {
kind: 'RootArgument',
name: node.name,
type: node.type ? node.type.toString() : null,
};
},
RootArgumentDefinition(node): ConcreteArgumentDefinition {
return {
kind: 'RootArgument',
name: node.name,
type: node.type ? node.type.toString() : null,
};
},

Condition(node, key, parent, ancestors): ConcreteSelection {
invariant(
node.condition.kind === 'Variable',
'RelayCodeGenerator: Expected static `Condition` node to be ' +
'pruned or inlined. Source: %s.',
getErrorMessage(ancestors[0]),
);
return {
kind: 'Condition',
passingValue: node.passingValue,
condition: node.condition.variableName,
selections: flattenArray(node.selections),
};
},
Condition(node, key, parent, ancestors): ConcreteSelection {
invariant(
node.condition.kind === 'Variable',
'RelayCodeGenerator: Expected static `Condition` node to be ' +
'pruned or inlined. Source: %s.',
getErrorMessage(ancestors[0]),
);
return {
kind: 'Condition',
passingValue: node.passingValue,
condition: node.condition.variableName,
selections: flattenArray(node.selections),
};
},

FragmentSpread(node): ConcreteSelection {
return {
kind: 'FragmentSpread',
name: node.name,
args: valuesOrNull(sortByName(node.args)),
};
},
FragmentSpread(node): ConcreteSelection {
return {
kind: 'FragmentSpread',
name: node.name,
args: valuesOrNull(sortByName(node.args)),
};
},

InlineFragment(node): ConcreteSelection {
return {
kind: 'InlineFragment',
type: node.typeCondition.toString(),
selections: flattenArray(node.selections),
};
},
InlineFragment(node): ConcreteSelection {
return {
kind: 'InlineFragment',
type: node.typeCondition.toString(),
selections: flattenArray(node.selections),
};
},

LinkedField(node): Array<ConcreteSelection> {
const handles =
(node.handles &&
node.handles.map(handle => {
return {
kind: 'LinkedHandle',
LinkedField(node): Array<ConcreteSelection> {
const handles =
(node.handles &&
node.handles.map(handle => {
return {
kind: 'LinkedHandle',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
handle: handle.name,
name: node.name,
key: handle.key,
filters: handle.filters,
};
})) ||
[];
const type = getRawType(node.type);
return [
{
kind: 'LinkedField',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
handle: handle.name,
concreteType: !isAbstractType(type) ? type.toString() : null,
name: node.name,
key: handle.key,
filters: handle.filters,
};
})) ||
[];
const type = getRawType(node.type);
return [
{
kind: 'LinkedField',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
concreteType: !isAbstractType(type) ? type.toString() : null,
name: node.name,
plural: isPlural(node.type),
selections: flattenArray(node.selections),
storageKey: getStorageKey(node.name, node.args),
plural: isPlural(node.type),
selections: flattenArray(node.selections),
storageKey: getStorageKey(node.name, node.args),
idField,
},
...handles,
];
},
...handles,
];
},

ScalarField(node): Array<ConcreteSelection> {
const handles =
(node.handles &&
node.handles.map(handle => {
return {
kind: 'ScalarHandle',
ScalarField(node): Array<ConcreteSelection> {
const handles =
(node.handles &&
node.handles.map(handle => {
return {
kind: 'ScalarHandle',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
handle: handle.name,
name: node.name,
key: handle.key,
filters: handle.filters,
};
})) ||
[];
return [
{
kind: 'ScalarField',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
handle: handle.name,
name: node.name,
key: handle.key,
filters: handle.filters,
};
})) ||
[];
return [
{
kind: 'ScalarField',
alias: node.alias,
args: valuesOrNull(sortByName(node.args)),
name: node.name,
selections: valuesOrUndefined(flattenArray(node.selections)),
storageKey: getStorageKey(node.name, node.args),
selections: valuesOrUndefined(flattenArray(node.selections)),
storageKey: getStorageKey(node.name, node.args),
},
...handles,
];
},
...handles,
];
},

Variable(node, key, parent): ConcreteArgument {
return {
kind: 'Variable',
name: parent.name,
variableName: node.variableName,
type: parent.type ? parent.type.toString() : null,
};
},
Variable(node, key, parent): ConcreteArgument {
return {
kind: 'Variable',
name: parent.name,
variableName: node.variableName,
type: parent.type ? parent.type.toString() : null,
};
},

Literal(node, key, parent): ConcreteArgument {
return {
kind: 'Literal',
name: parent.name,
value: node.value,
type: parent.type ? parent.type.toString() : null,
};
},

Literal(node, key, parent): ConcreteArgument {
return {
kind: 'Literal',
name: parent.name,
value: node.value,
type: parent.type ? parent.type.toString() : null,
};
},
Argument(node, key, parent, ancestors): ?ConcreteArgument {
invariant(
['Variable', 'Literal'].indexOf(node.value.kind) >= 0,
'RelayCodeGenerator: Complex argument values (Lists or ' +
'InputObjects with nested variables) are not supported, argument ' +
'`%s` had value `%s`. Source: %s.',
node.name,
prettyStringify(node.value),
getErrorMessage(ancestors[0]),
);
return node.value.value !== null ? node.value : null;
},
},
};
}

Argument(node, key, parent, ancestors): ?ConcreteArgument {
invariant(
['Variable', 'Literal'].indexOf(node.value.kind) >= 0,
'RelayCodeGenerator: Complex argument values (Lists or ' +
'InputObjects with nested variables) are not supported, argument ' +
'`%s` had value `%s`. Source: %s.',
node.name,
prettyStringify(node.value),
getErrorMessage(ancestors[0]),
);
return node.value.value !== null ? node.value : null;
},
},
};
/**
* @public
*
* Converts a Relay IR node into a plain JS object representation that can be
* used at runtime.
*/
generate(node: Root | Fragment): ConcreteRoot | ConcreteFragment {
invariant(
['Root', 'Fragment'].indexOf(node.kind) >= 0,
'RelayCodeGenerator: Unknown AST kind `%s`. Source: %s.',
node.kind,
getErrorMessage(node),
);
return RelayIRVisitor.visit(node, this._codeGeneratorVisitor);
}
}
/* eslint-enable no-redeclare */

function isPlural(type: any): boolean {
return getNullableType(type) instanceof GraphQLList;
Expand Down Expand Up @@ -274,4 +282,4 @@ function getStorageKey(
return isLiteral ? formatStorageKey(fieldName, preparedArgs) : null;
}

module.exports = {generate};
module.exports = RelayCodeGenerator;
Loading