All Downloads are FREE. Search and download functionalities are using the official Maven repository.

node_modules.apollo-codegen.src.compiler.visitors.generateOperationId.ts Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
import { Operation, Fragment } from '../';
import { collectFragmentsReferenced } from './collectFragmentsReferenced';
import { createHash } from 'crypto';

export function generateOperationId(
  operation: Operation,
  fragments: { [fragmentName: string]: Fragment },
  fragmentsReferenced?: Iterable
) {
  if (!fragmentsReferenced) {
    fragmentsReferenced = collectFragmentsReferenced(operation.selectionSet, fragments);
  }

  const sourceWithFragments = [
    operation.source,
    ...Array.from(fragmentsReferenced).map(fragmentName => {
      const fragment = fragments[fragmentName];
      if (!fragment) {
        throw new Error(`Cannot find fragment "${fragmentName}"`);
      }
      return fragment.source;
    })
  ].join('\n');

  const hash = createHash('sha256');
  hash.update(sourceWithFragments);
  const operationId = hash.digest('hex');

  return { operationId, sourceWithFragments };
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy