How to use apollo-codegen-core - 10 common examples

To help you get started, we’ve selected a few apollo-codegen-core 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 apollographql / apollo-tooling / packages / apollo-codegen-flow-legacy / src / codeGeneration.ts View on Github external
function enumerationDeclaration(
  generator: CodeGenerator<LegacyCompilerContext>,
  type: GraphQLEnumType
) {
  const { name, description } = type;
  const values = type.getValues();

  generator.printNewlineIfNeeded();
  if (description) {
    description.split("\n").forEach(line => {
      generator.printOnNewline(`// ${line.trim()}`);
    });
  }
  generator.printOnNewline(`export type ${name} =`);
  const nValues = values.length;
  sortEnumValues(values).forEach((value, i) => {
    if (!value.description || value.description.indexOf("\n") === -1) {
      generator.printOnNewline(
        `  "${value.value}"${i === nValues - 1 ? ";" : " |"}${wrap(
          " // ",
          value.description || undefined
        )}`
      );
    } else {
      if (value.description) {
        value.description.split("\n").forEach(line => {
          generator.printOnNewline(`  // ${line.trim()}`);
        });
      }
      generator.printOnNewline(
        `  "${value.value}"${i === nValues - 1 ? ";" : " |"}`
      );
github apollographql / apollo-tooling / packages / apollo-codegen-swift / src / helpers.ts View on Github external
propertiesForSelectionSet(
    selectionSet: SelectionSet,
    namespace?: string
  ): (Field & Property & Struct)[] | undefined {
    const properties = collectAndMergeFields(selectionSet, true)
      .filter(field => field.name !== "__typename")
      .map(field => this.propertyFromField(field, namespace));

    // If we're not merging in fields from fragment spreads, there is no guarantee there will a generated
    // type for a composite field, so to avoid compiler errors we skip the initializer for now.
    if (
      selectionSet.selections.some(
        selection => selection.kind === "FragmentSpread"
      ) &&
      properties.some(property => isCompositeType(getNamedType(property.type)))
    ) {
      return undefined;
    }

    return properties;
  }
github apollographql / apollo-tooling / packages / apollo-codegen-typescript-legacy / src / codeGeneration.ts View on Github external
export function generateSource(context: LegacyCompilerContext) {
  const generator = new CodeGenerator<LegacyCompilerContext>(context);

  generator.printOnNewline("/* tslint:disable */");
  generator.printOnNewline(
    "//  This file was automatically generated and should not be edited."
  );

  context.typesUsed.forEach(type =>
    typeDeclarationForGraphQLType(generator, type)
  );
  Object.values(context.operations).forEach(operation => {
    interfaceVariablesDeclarationForOperation(generator, operation);
    interfaceDeclarationForOperation(generator, operation);
  });
  Object.values(context.fragments).forEach(operation =>
    interfaceDeclarationForFragment(generator, operation)
  );
github apollographql / apollo-tooling / packages / apollo-codegen-flow-legacy / src / codeGeneration.ts View on Github external
export function generateSource(context: LegacyCompilerContext) {
  const generator = new CodeGenerator(context);

  generator.printOnNewline("/* @flow */");
  generator.printOnNewline("/* eslint-disable */");
  generator.printOnNewline(
    "//  This file was automatically generated and should not be edited."
  );
  context.typesUsed.forEach(type =>
    typeDeclarationForGraphQLType(generator, type)
  );
  Object.values(context.operations).forEach(operation => {
    interfaceVariablesDeclarationForOperation(generator, operation);
    typeDeclarationForOperation(generator, operation);
  });
  Object.values(context.fragments).forEach(fragment => {
    typeDeclarationForFragment(generator, fragment);
  });
github apollographql / apollo-tooling / packages / apollo-codegen-scala / src / codeGeneration.ts View on Github external
export function generateSource(context: LegacyCompilerContext) {
  const generator = new CodeGenerator(context);

  generator.printOnNewline(
    "//  This file was automatically generated and should not be edited."
  );
  generator.printNewline();

  if (context.options.namespace) {
    packageDeclaration(generator, context.options.namespace);
  }

  context.typesUsed.forEach(type => {
    typeDeclarationForGraphQLType(generator, type);
  });

  Object.values(context.operations).forEach(operation => {
    classDeclarationForOperation(generator, operation);
github apollographql / apollo-tooling / packages / apollo-codegen-typescript / src / language.ts View on Github external
t.TSTypeAnnotation(type)
      );

      // TODO: Check if this works
      propertySignatureType.optional =
        keyInheritsNullability && this.isNullableType(type);

      if (this.options.useReadOnlyTypes) {
        propertySignatureType.readonly = true;
      }

      if (description) {
        propertySignatureType.leadingComments = [
          {
            type: "CommentBlock",
            value: commentBlockContent(description)
          } as t.CommentBlock
        ];
      }

      return propertySignatureType;
    });
  }
github apollographql / apollo-tooling / packages / apollo-codegen-flow / src / language.ts View on Github external
);

        // Nullable fields on input objects do not have to be defined
        // as well, so allow these fields to be "undefined"
        objectTypeProperty.optional =
          keyInheritsNullability &&
          annotation.type === "NullableTypeAnnotation";
        if (this.options.useReadOnlyTypes) {
          objectTypeProperty.variance = { kind: "plus" };
        }

        if (description) {
          objectTypeProperty.leadingComments = [
            {
              type: "CommentBlock",
              value: commentBlockContent(description)
            } as t.CommentBlock
          ];
        }

        return objectTypeProperty;
      })
    );
github apollographql / apollo-tooling / packages / apollo / src / fetch-schema.ts View on Github external
async function fromFile(
  file: string
): Promise<IntrospectionSchema | undefined> {
  let result;
  try {
    result = fs.readFileSync(file, {
      encoding: "utf-8"
    });
  } catch (err) {
    throw new Error(`Unable to read file ${file}. ${err.message}`);
  }

  const ext = path.extname(file);

  // an actual introspectionQuery result
  if (ext === ".json") {
    const parsed = JSON.parse(result);
    const schemaData = parsed.data
      ? parsed.data.__schema
      : parsed.__schema
        ? parsed.__schema
        : parsed;
github apollographql / apollo-tooling / packages / apollo / src / helpers / commands / queries / commonTasks.ts View on Github external
task: (ctx, task) => {
    // Make sure the expectations of our context are correct.
    assert.strictEqual(typeof ctx.queryDocuments, "undefined");

    ctx.queryDocuments = loadQueryDocuments(
      ctx.documentSets[0].documentPaths,
      flags.tagName
    );
    task.title = `Scanning for GraphQL queries (${
      ctx.queryDocuments.length
    } found)`;
  }
});
github apollographql / apollo-tooling / packages / apollo-codegen-flow / src / __tests__ / codeGeneration.ts View on Github external
test("handles multiline graphql comments", () => {
    const miscSchema = loadSchema(
      require.resolve("../../../../__fixtures__/misc/schema.json")
    );

    const document = parse(`
      query CustomScalar {
        commentTest {
          multiLine
        }
      }
    `);

    const output = generateSource(
      compileToIR(miscSchema, document, {
        mergeInFieldsFromFragmentSpreads: true,
        addTypename: true
      })
    );

    expect(output).toMatchSnapshot();
  });
});