How to use the @graphql-codegen/visitor-plugin-common.getPossibleTypes function in @graphql-codegen/visitor-plugin-common

To help you get started, we’ve selected a few @graphql-codegen/visitor-plugin-common 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 dotansimha / graphql-code-generator / packages / presets / near-operation-file / src / index.ts View on Github external
const fragmentNameToFile: FragmentNameToFile = options.documents.reduce((prev: FragmentNameToFile, documentRecord) => {
      const fragments: FragmentDefinitionNode[] = documentRecord.content.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[];

      if (fragments.length > 0) {
        for (const fragment of fragments) {
          const schemaType = schemaObject.getType(fragment.typeCondition.name.value);

          if (!schemaType) {
            throw new Error(`Fragment "${fragment.name.value}" is set on non-existing type "${fragment.typeCondition.name.value}"!`);
          }

          const possibleTypes = getPossibleTypes(schemaObject, schemaType);
          const fragmentSuffix = options.config.dedupeOperationSuffix && fragment.name.value.toLowerCase().endsWith('fragment') ? '' : 'Fragment';
          const filePath = appendExtensionToFilePath(documentRecord.filePath, extension);
          const importsNames = getAllFragmentSubTypes(
            possibleTypes.map(t => t.name),
            fragment.name.value,
            fragmentSuffix
          );

          if (prev[fragment.name.value]) {
            duplicateFragmentNames.push(fragment.name.value);
          }

          prev[fragment.name.value] = { filePath, importsNames, onType: fragment.typeCondition.name.value, node: fragment };
        }
      }