How to use the relay-compiler.compileRelayArtifacts function in relay-compiler

To help you get started, weโ€™ve selected a few relay-compiler examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github convoyinc / graphql-client-benchmarks / clients / relay.ts View on Github external
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,
    };
  }
github relay-tools / relay-compiler-language-typescript / src / codegen / RelayFileWriter.ts View on Github external
}
			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;
github facebook / relay / packages / relay-test-utils-internal / TestCompiler.js View on Github external
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;
}
github facebook / relay / packages / relay-test-utils / RelayModernTestUtils.js View on Github external
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;
}