How to use the relay-compiler.Parser.transform 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 n1ru4l / graphql-codegen-relay-plugins / packages / graphql-codegen-relay-optimizer-plugin / src / index.ts View on Github external
// Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
  // transformASTSchema creates a new schema instance instead of mutating the old one
  const adjustedSchema = transformASTSchema(schema, [
    /* GraphQL */ `
      directive @connection(key: String!) on FIELD
      directive @client on FIELD
    `
  ]);
  const documentAsts = documents.reduce(
    (prev, v) => {
      return [...prev, ...v.content.definitions];
    },
    [] as DefinitionNode[]
  );

  const relayDocuments = RelayParser.transform(adjustedSchema, documentAsts);

  const fragmentCompilerContext = new GraphQLCompilerContext(
    adjustedSchema
  ).addAll(relayDocuments);

  const fragmentDocuments = fragmentCompilerContext
    .applyTransforms([
      RelayApplyFragmentArgumentTransform.transform,
      FlattenTransform.transformWithOptions({ flattenAbstractTypes: false }),
      SkipRedundantNodesTransform.transform
    ])
    .documents()
    .filter(doc => doc.kind === "Fragment");

  const queryCompilerContext = new GraphQLCompilerContext(adjustedSchema)
    .addAll(relayDocuments)
github dotansimha / graphql-code-generator / packages / plugins / other / relay-operation-optimizer / src / index.ts View on Github external
// transformASTSchema creates a new schema instance instead of mutating the old one
  const adjustedSchema = RelayCreate.create(
    printSchemaWithDirectives(
      transformASTSchema(schema, [
        /* GraphQL */ `
          directive @connection(key: String!) on FIELD
          directive @client on FIELD
        `,
      ])
    )
  );
  const documentAsts = documents.reduce((prev, v) => {
    return [...prev, ...v.content.definitions];
  }, [] as DefinitionNode[]);

  const relayDocuments = RelayParser.transform(adjustedSchema, documentAsts);

  const fragmentCompilerContext = new GraphQLCompilerContext(adjustedSchema).addAll(relayDocuments);

  const fragmentDocuments = fragmentCompilerContext
    .applyTransforms([ApplyFragmentArgumentTransform.transform, FlattenTransform.transformWithOptions({ flattenAbstractTypes: false }), SkipRedundantNodesTransform.transform])
    .documents()
    .filter(doc => doc.kind === 'Fragment');

  const queryCompilerContext = new GraphQLCompilerContext(adjustedSchema)
    .addAll(relayDocuments)
    .applyTransforms([ApplyFragmentArgumentTransform.transform, InlineFragmentsTransform.transform, FlattenTransform.transformWithOptions({ flattenAbstractTypes: false }), SkipRedundantNodesTransform.transform]);

  const newQueryDocuments = queryCompilerContext.documents().map(doc => ({
    filePath: 'optimized by relay',
    content: parse(print(adjustedSchema, doc)),
  }));
github dotansimha / graphql-code-generator / packages / plugins / other / visitor-plugin-common / src / optimize-operations.ts View on Github external
export function optimizeOperations(schema: GraphQLSchema, documents: Types.DocumentFile[]): Types.DocumentFile[] {
  const documentAsts = documents.reduce((prev, v) => {
    return [...prev, ...v.content.definitions];
  }, [] as DefinitionNode[]);
  const adjustedSchema = RelayCreate.create(
    printSchemaWithDirectives(schema)
  );
  const relayDocuments = RelayParser.transform(adjustedSchema, documentAsts);

  const queryCompilerContext = new GraphQLCompilerContext(adjustedSchema)
    .addAll(relayDocuments)
    .applyTransforms([ApplyFragmentArgumentTransform.transform, InlineFragmentsTransform.transform, FlattenTransform.transformWithOptions({ flattenAbstractTypes: false }), SkipRedundantNodesTransform.transform]);

    const newQueryDocuments = queryCompilerContext.documents().map(doc => ({
      filePath: 'optimized by relay',
      content: parse(print(adjustedSchema, doc)),
    }));

  return newQueryDocuments;
}