Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transformRawExample(rawExample: SingleRawExample, schema: string): RelayExample {
const schemaAst = graphql.buildASTSchema(graphql.parse(schema));
let context = new graphqlCompiler.CompilerContext(schemaAst);
// In some queries, we leverage Hermes' @static directive; but Relay doesn't
// like unknown directives. Quick hack to remove it.
const scrubbedOperation = rawExample.operation.replace(/@static/g, '');
context = context.addAll(graphqlCompiler.Parser.parse(schemaAst, scrubbedOperation));
const artifacts = relayCompiler.compileRelayArtifacts(context, TRANSFORMS) as any[];
const requests = artifacts.filter(a => a.kind !== 'Fragment');
if (requests.length !== 1) {
throw new Error(`Expected the query document to contain only one operation`);
}
const request = relayRuntime.getRequest(requests[0]);
const operation = relayRuntime.createOperationSelector(request, rawExample.variables);
return {
operation,
response: rawExample.response,
};
}
}
return cachedDir;
};
const transformedTSContext = compilerContext.applyTransforms(
RelayTSGenerator.TS_TRANSFORMS,
this._reporter,
);
const transformedQueryContext = compilerContext.applyTransforms(
[
...this._config.compilerTransforms.commonTransforms,
...this._config.compilerTransforms.queryTransforms,
],
this._reporter,
);
const artifacts = compileRelayArtifacts(
compilerContext,
this._config.compilerTransforms,
this._reporter,
);
const existingFragmentNames = new Set(
definitions.map(definition => definition.name),
);
// TODO(T22651734): improve this to correctly account for fragments that
// have generated flow types.
baseDefinitionNames.forEach(baseDefinitionName => {
existingFragmentNames.delete(baseDefinitionName);
});
const formatModule = this._config.formatModule;
function generate(
text: string,
schema: GraphQLSchema,
transforms: RelayCompilerTransforms,
moduleMap: ?{[string]: mixed},
): {[key: string]: GeneratedNode} {
const relaySchema = transformASTSchema(schema, IRTransforms.schemaExtensions);
const {definitions, schema: extendedSchema} = parseGraphQLText(
relaySchema,
text,
);
const compilerContext = new GraphQLCompilerContext(
Schema.DEPRECATED__create(schema, extendedSchema),
).addAll(definitions);
const documentMap = {};
compileRelayArtifacts(compilerContext, transforms).forEach(
([_definition, node]) => {
const transformedNode =
moduleMap != null ? CodeMarker.transform(node, moduleMap) : node;
documentMap[
node.kind === 'Request' ? node.params.name : node.name
] = transformedNode;
},
);
return documentMap;
}
transforms: RelayCompilerTransforms,
): {[key: string]: GeneratedNode} {
const {
compileRelayArtifacts,
GraphQLCompilerContext,
IRTransforms,
transformASTSchema,
} = require('relay-compiler');
const relaySchema = transformASTSchema(schema, IRTransforms.schemaExtensions);
const compilerContext = new GraphQLCompilerContext(
schema,
relaySchema,
).addAll(parseGraphQLText(relaySchema, text).definitions);
const documentMap = {};
compileRelayArtifacts(compilerContext, transforms).forEach(
([_definition, node]) => {
documentMap[
node.kind === 'Request' ? node.params.name : node.name
] = node;
},
);
return documentMap;
}