Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support referencing other packages #50

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package testapis.multipkgs.subpkg1;

message SubpkgMessage {
string body = 1;
}

enum SubpkgEnum {
SUBPKG_ENUM_UNSPECIFIED = 0;
FOO = 1;
BAR = 2;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package testapis.multipkgs.subpkg1;

import "testapis/multipkgs/subpkg1/types.proto";

message MessageWithSubpkg {
testapis.multipkgs.subpkg1.SubpkgMessage message = 1;
testapis.multipkgs.subpkg1.SubpkgEnum enum = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function itGeneratesNexusDSLsToMatchSnapshtos(
}

export function testSchemaGeneration(
name: string,
name: string | string[],
target: GenerationTarget,
opts: {
types?: Record<string, any>;
Expand All @@ -62,13 +62,21 @@ export function testSchemaGeneration(
} = {}
) {
describe(`schema generation with ${target}`, () => {
let resp: CodeGeneratorResponse;
let files: CodeGeneratorResponse.File[];
beforeEach(async () => {
resp = await generateDSLs(name, target, { withPrefix: true });
files = [];

for (const n of Array.isArray(name) ? name : [name]) {
files.push(
...(
await generateDSLs(n, target, { withPrefix: true })
).getFileList()
);
}
});

it("can make GraphQL schema from generated DSLs", async () => {
await withGeneratedSchema(resp, opts?.types ?? {}, async (dir) => {
await withGeneratedSchema(files, opts?.types ?? {}, async (dir) => {
try {
await validateGeneratedFiles(dir);
const gqlSchema = await fs.readFile(
Expand All @@ -88,7 +96,7 @@ export function testSchemaGeneration(
it.each(opts.schemaTests)("%s", async (_name, types, f) => {
try {
await withGeneratedSchema(
resp,
files,
{ ...(opts.types ?? {}), ...types },
async (_dir, schema) => {
await f(schema);
Expand Down Expand Up @@ -175,7 +183,7 @@ function getFileMap(resp: CodeGeneratorResponse): Record<string, string> {
}

async function withGeneratedResults(
resp: CodeGeneratorResponse,
files: CodeGeneratorResponse.File[],
cb: (dir: string) => Promise<void>
) {
const suffix = Math.random().toString(36).substring(7);
Expand All @@ -185,42 +193,47 @@ async function withGeneratedResults(
.then(() =>
Promise.all(
[
...new Set(
resp.getFileList().map((f) => join(dir, dirname(f.getName()!)))
),
...new Set(files.map((f) => join(dir, dirname(f.getName()!)))),
].map((p) => fs.mkdir(p, { recursive: true }))
).then(() => dir)
)
.then((dir) =>
Promise.all(
resp
.getFileList()
.map((f) =>
fs.writeFile(join(dir, f.getName()!), f.getContent()!, "utf-8")
)
files.map((f) =>
fs.writeFile(join(dir, f.getName()!), f.getContent()!, "utf-8")
)
).then(() => dir)
)
.then(cb)
.finally(() => fs.rm(dir, { recursive: true, force: true, maxRetries: 3 }));
}

async function withGeneratedSchema(
resp: CodeGeneratorResponse,
files: CodeGeneratorResponse.File[],
queries: Record<string, NexusExtendTypeDef<"Query">>,
cb: (dir: string, schema: NexusGraphQLSchema) => Promise<void>
) {
await withGeneratedResults(resp, async (dir) => {
const paths = resp.getFileList().map((f) => join(dir, f.getName()!));
const types = paths.reduce((o, p) => ({ ...o, ...require(p) }), {} as any);
const schema = makeSchema({
types: { ...types, ...queries },
outputs: {
schema: join(dir, "schema.graphql"),
typegen: join(dir, "typings.ts"),
},
shouldGenerateArtifacts: true,
});
await cb(dir, schema);
await withGeneratedResults(files, async (dir) => {
const paths = files.map((f) => join(dir, f.getName()!));
try {
const types = paths.reduce(
(o, p) => ({ ...o, ...require(p) }),
{} as any
);
const schema = makeSchema({
types: { ...types, ...queries },
outputs: {
schema: join(dir, "schema.graphql"),
typegen: join(dir, "typings.ts"),
},
shouldGenerateArtifacts: true,
});
await cb(dir, schema);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
throw e;
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`schema generation with native protobuf can make GraphQL schema from generated DSLs: schema.graphql 1`] = `
"### This file was generated by Nexus Schema
### Do not make changes to this file directly
\\"\\"\\"\\"\\"\\"
type MessageWithSubpkg {
\\"\\"\\"\\"\\"\\"
enum: SubpkgEnum
\\"\\"\\"\\"\\"\\"
message: SubpkgMessage
}
\\"\\"\\"\\"\\"\\"
input MessageWithSubpkgInput {
\\"\\"\\"\\"\\"\\"
enum: SubpkgEnum
\\"\\"\\"\\"\\"\\"
message: SubpkgMessageInput
}
type Query {
ok: Boolean!
}
\\"\\"\\"\\"\\"\\"
enum SubpkgEnum {
BAR
FOO
}
\\"\\"\\"\\"\\"\\"
type SubpkgMessage {
\\"\\"\\"\\"\\"\\"
body: String!
}
\\"\\"\\"\\"\\"\\"
input SubpkgMessageInput {
\\"\\"\\"\\"\\"\\"
body: String!
}
"
`;

exports[`schema generation with protobufjs can make GraphQL schema from generated DSLs: schema.graphql 1`] = `
"### This file was generated by Nexus Schema
### Do not make changes to this file directly
\\"\\"\\"\\"\\"\\"
type MessageWithSubpkg {
\\"\\"\\"\\"\\"\\"
enum: SubpkgEnum
\\"\\"\\"\\"\\"\\"
message: SubpkgMessage
}
\\"\\"\\"\\"\\"\\"
input MessageWithSubpkgInput {
\\"\\"\\"\\"\\"\\"
enum: SubpkgEnum
\\"\\"\\"\\"\\"\\"
message: SubpkgMessageInput
}
type Query {
ok: Boolean!
}
\\"\\"\\"\\"\\"\\"
enum SubpkgEnum {
BAR
FOO
}
\\"\\"\\"\\"\\"\\"
type SubpkgMessage {
\\"\\"\\"\\"\\"\\"
body: String!
}
\\"\\"\\"\\"\\"\\"
input SubpkgMessageInput {
\\"\\"\\"\\"\\"\\"
body: String!
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { testSchemaGeneration } from "../__helpers__/process.test.helper";

testSchemaGeneration(["multipkgs/subpkg1", "multipkgs/subpkg2"], "protobufjs");
testSchemaGeneration(
["multipkgs/subpkg1", "multipkgs/subpkg2"],
"native protobuf"
);
Loading