Skip to content

Commit

Permalink
Support generating based on .graphql schema files (#483)
Browse files Browse the repository at this point in the history
* Support generating based on .graphql schema files

Fixes #259

* Remove unused import
  • Loading branch information
shadaj committed Jul 9, 2018
1 parent 9fb0cae commit 55d3727
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,47 @@ public final class SimpleQueryQuery: GraphQLQuery {
}"
`;

exports[`successful codegen infers Swift target and writes types when schema is a GraphQL file 1`] = `
"// This file was automatically generated and should not be edited.
import Apollo
public final class SimpleQueryQuery: GraphQLQuery {
public let operationDefinition =
\\"query SimpleQuery {\\\\n hello\\\\n}\\"
public init() {
}
public struct Data: GraphQLSelectionSet {
public static let possibleTypes = [\\"Query\\"]
public static let selections: [GraphQLSelection] = [
GraphQLField(\\"hello\\", type: .nonNull(.scalar(String.self))),
]
public private(set) var resultMap: ResultMap
public init(unsafeResultMap: ResultMap) {
self.resultMap = unsafeResultMap
}
public init(hello: String) {
self.init(unsafeResultMap: [\\"__typename\\": \\"Query\\", \\"hello\\": hello])
}
public var hello: String {
get {
return resultMap[\\"hello\\"]! as! String
}
set {
resultMap.updateValue(newValue, forKey: \\"hello\\")
}
}
}
}"
`;

exports[`successful codegen infers TypeScript target and writes types 1`] = `
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import gql from "graphql-tag";
import { fs as mockFS, vol } from "apollo-codegen-core/lib/localfs";

const test = setup.do(() => mockConsole());

const graphQLSchema = fs.readFileSync(
path.resolve(__dirname, "../../schema/__tests__/fixtures/schema.graphql"),
{
encoding: "utf-8"
}
);

const fullSchema = execute(
buildSchema(
fs.readFileSync(
path.resolve(__dirname, "../../schema/__tests__/fixtures/schema.graphql"),
{
encoding: "utf-8"
}
)
),
buildSchema(graphQLSchema),
gql(introspectionQuery)
).data;

Expand Down Expand Up @@ -87,6 +88,18 @@ describe("successful codegen", () => {
expect(mockFS.readFileSync("API.swift").toString()).toMatchSnapshot();
});

test
.do(() => {
vol.fromJSON({
"schema.graphql": graphQLSchema,
"queryOne.graphql": simpleQuery.toString()
});
})
.command(["codegen:generate", "--schema=schema.graphql", "API.swift"])
.it("infers Swift target and writes types when schema is a GraphQL file", () => {
expect(mockFS.readFileSync("API.swift").toString()).toMatchSnapshot();
});

test
.do(() => {
vol.fromJSON({
Expand Down
26 changes: 14 additions & 12 deletions packages/apollo-cli/src/commands/codegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { promisify } from "util";
import { loadSchemaStep } from "../../load-schema";

import { engineFlags } from "../../engine-cli";
import { fromFile } from '../../fetch-schema';
import { fetchSchema } from '../../fetch-schema';
import { loadQueryDocuments } from 'apollo-codegen-core/lib/loading';

export default class Generate extends Command {
Expand All @@ -32,7 +32,7 @@ export default class Generate extends Command {
default: "**/*.graphql"
}),
schema: flags.string({
description: "Path to your GraphQL schema introspection result"
description: "Path to your GraphQL schema (either .graphql or .json)"
}),
clientSchema: flags.string({
description: "Path to your client-side GraphQL schema file for `apollo-link-state` (either .graphql or .json)"
Expand Down Expand Up @@ -182,7 +182,15 @@ export default class Generate extends Command {
);
});
task.title = `Scanning for GraphQL queries (${paths.length} found)`;
ctx.queryPaths = paths.filter(p => path.resolve(p) != (flags.clientSchema ? path.resolve(flags.clientSchema) : undefined));

const excludedPaths = [
flags.clientSchema ? path.resolve(flags.clientSchema) : undefined,
flags.schema ? path.resolve(flags.schema) : undefined
];

ctx.queryPaths = paths.filter(p =>
!excludedPaths.some(v => v == path.resolve(p))
);
}
},
loadSchemaStep(
Expand All @@ -193,15 +201,9 @@ export default class Generate extends Command {
"Loading GraphQL schema",
async ctx => {
if (flags.schema) {
const schemaFileContent = await promisify(fs.readFile)(
path.resolve(flags.schema as string)
);
const schemaData = JSON.parse((schemaFileContent as any) as string);
ctx.schema = schemaData.data
? schemaData.data.__schema
: schemaData.__schema
? schemaData.__schema
: schemaData;
ctx.schema = await fetchSchema({
endpoint: flags.schema
});
} else {
this.log("Not loading because no path was provided (you should have a client-side schema)");
}
Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-cli/src/fetch-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const fromFile = async ({ endpoint }: { endpoint: string }) => {
const ext = path.extname(endpoint);

// an actual introspectionQuery result
if (ext === ".json") return JSON.parse(result).data.__schema;
if (ext === ".json") {
const parsed = JSON.parse(result)
return parsed.data ? parsed.data.__schema :
parsed.__schema ? parsed.__schema : parsed;
};

if (ext === ".graphql" || ext === ".graphqls" || ext === ".gql") {
const schema = buildSchema(result);
Expand Down

0 comments on commit 55d3727

Please sign in to comment.