Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions packages/apollo-codegen-core/src/utilities/__tests__/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { parse, print } from "graphql";
import {
withTypenameFieldAddedWhereNeeded,
removeConnectionDirectives,
removeClientDirectives
} from "../graphql";

describe("typename additions", () => {
it("adds typenames to selectionSets", () => {
const original = parse(`
query GetUser {
me {
firstName
friends {
firstName
}
}
}
`);

const modified = print(
parse(`
query GetUser {
me {
__typename
firstName
friends {
__typename
firstName
}
}
}
`)
);

const newQuery = withTypenameFieldAddedWhereNeeded(original);
expect(print(newQuery)).toEqual(modified);
});
});

describe("client removals", () => {
it("removes the @connection directive", () => {
const original = parse(`
query GetUser {
list @connection(key: "Value")
}
`);

const modified = print(
parse(`
query GetUser {
list
}
`)
);

const newQuery = removeConnectionDirectives(original);
expect(print(newQuery)).toEqual(modified);
});
it("removes @client id from a mixed query", () => {
const original = parse(`
query GetUser {
list @client
remote {
id
virtual @client
virtualSelectionSet @client @export(as: "id") {
name
}
}
}
`);

const modified = print(
parse(`
query GetUser {
remote {
id
}
}
`)
);

const newQuery = removeClientDirectives(original);
expect(print(newQuery)).toEqual(modified);
});
it("returns and empty string when removing all fields", () => {
const original = parse(`
query GetUser {
list @client
local @client {
id
}
}
`);

const newQuery = removeClientDirectives(original);
expect(print(newQuery).trim()).toBeFalsy();
});
});
31 changes: 30 additions & 1 deletion packages/apollo-codegen-core/src/utilities/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
GraphQLField,
GraphQLList,
GraphQLNonNull,
DocumentNode
DocumentNode,
DirectiveNode
} from "graphql";

declare module "graphql/utilities/buildASTSchema" {
Expand All @@ -50,6 +51,34 @@ export function isMetaFieldName(name: string) {
return name.startsWith("__");
}

export function removeConnectionDirectives(ast: ASTNode) {
return visit(ast, {
Directive(node: DirectiveNode): DirectiveNode | null {
if (node.name.value === "connection") return null;
return node;
}
});
}

export function removeClientDirectives(ast: ASTNode) {
return visit(ast, {
Field(node: FieldNode): FieldNode | null {
if (
node.directives &&
node.directives.find(directive => directive.name.value === "client")
)
return null;
return node;
},
OperationDefinition: {
leave(node: OperationDefinitionNode): OperationDefinitionNode | null {
if (!node.selectionSet.selections.length) return null;
return node;
}
}
});
}

const typenameField = {
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: "__typename" }
Expand Down
17 changes: 15 additions & 2 deletions packages/apollo/src/commands/queries/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export default class ExtractQueries extends Command {
description:
"Path to your GraphQL queries, can include search tokens like **"
}),
addTypename: flags.boolean({
description: "Automatically add __typename to your queries",
allowNo: true
}),
removeClientDirectives: flags.boolean({
allowNo: true,
description:
"Automatically remove @client and @connection directives and fields from operations"
}),
...engineFlags,

tagName: flags.string({
Expand All @@ -48,12 +57,16 @@ export default class ExtractQueries extends Command {
];

async run() {
const { flags, args } = this.parse(ExtractQueries);
let { flags, args } = this.parse(ExtractQueries);

// oclif doesn't let setting boolean flags to be default
const defaultFlags = { addTypename: true, removeClientDirectives: true };
flags = { ...defaultFlags, ...flags };

const tasks: Listr = new Listr([
loadConfigStep(flags, false),
...getCommonTasks({ flags, errorLogger: this.error.bind(this) }),
...getCommonManifestTasks(),
...getCommonManifestTasks({ flags }),
{
title: "Outputing extracted queries",
task: (ctx, task) => {
Expand Down
11 changes: 2 additions & 9 deletions packages/apollo/src/commands/schema/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { Command, flags } from "@oclif/command";
import chalk from "chalk";
import { table, styledJSON } from "heroku-cli-util";
import * as Listr from "listr";
import {
GraphQLError,
parse,
introspectionQuery,
execute as graphql
} from "graphql";

import { GraphQLError } from "graphql";
import { toPromise, execute } from "apollo-link";

import { VALIDATE_SCHEMA } from "../../operations/validateSchema";
Expand Down Expand Up @@ -89,8 +83,7 @@ export default class SchemaCheck extends Command {

const variables = {
id: getIdFromKey(ctx.currentSchema.engineKey),
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
schema: ctx.schema,
// XXX hardcoded for now
tag: "current",
gitContext
Expand Down
8 changes: 1 addition & 7 deletions packages/apollo/src/commands/schema/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { loadSchema } from "../../load-schema";

import { loadConfigStep } from "../../load-config";

import { execute, introspectionQuery, parse } from "graphql";

export default class SchemaDownload extends Command {
static description = "Download the schema from your GraphQL endpoint.";

Expand Down Expand Up @@ -79,11 +77,7 @@ export default class SchemaDownload extends Command {
task: async ctx => {
await promisify(fs.writeFile)(
args.output,
JSON.stringify(
(await execute(ctx.schema, parse(introspectionQuery))).data!,
null,
2
)
JSON.stringify({ __schema: ctx.schema }, null, 2)
);
}
}
Expand Down
13 changes: 2 additions & 11 deletions packages/apollo/src/commands/schema/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ import { Command, flags } from "@oclif/command";
import { table, styledJSON } from "heroku-cli-util";
import * as Listr from "listr";
import { toPromise, execute } from "apollo-link";
import {
GraphQLError,
parse,
introspectionQuery,
execute as graphql
} from "graphql";

import { GraphQLError } from "graphql";
import { UPLOAD_SCHEMA } from "../../operations/uploadSchema";
import { getIdFromKey, engineLink } from "../../engine";
import { fetchSchema } from "../../fetch-schema";
import { gitInfo } from "../../git";

import { engineFlags } from "../../engine-cli";

import { loadConfigStep } from "../../load-config";

export default class SchemaPublish extends Command {
Expand Down Expand Up @@ -84,8 +76,7 @@ export default class SchemaPublish extends Command {
)} to Apollo Engine`;
const gitContext = await gitInfo();
const variables = {
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
schema: ctx.schema,
tag,
gitContext,
id: getIdFromKey(ctx.currentSchema.engineKey)
Expand Down
13 changes: 11 additions & 2 deletions packages/apollo/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { fs, withGlobalFS } from "apollo-codegen-core/lib/localfs";

import * as fg from "glob";
import * as minimatch from "minimatch";
import { GraphQLSchema, extendSchema, visit, buildASTSchema } from "graphql";
import {
GraphQLSchema,
extendSchema,
visit,
buildASTSchema,
buildClientSchema
} from "graphql";
import { loadSchema } from "./load-schema";
import { loadQueryDocuments } from "apollo-codegen-core/lib/loading";

Expand Down Expand Up @@ -232,7 +238,10 @@ export async function resolveSchema(
)
: referredSchema.clientSide
? buildASTSchema(loadAsAST())
: await loadSchema(referredSchema, config);
: await loadSchema(referredSchema, config).then(introspectionResult => {
Comment thread
abernix marked this conversation as resolved.
Outdated
if (!introspectionResult) return;
return buildClientSchema({ __schema: introspectionResult });
});
}

export async function resolveDocumentSets(
Expand Down
Loading