Skip to content

Commit

Permalink
refactor: use @aws-amplify/graphql-generator in model-generator (#243)
Browse files Browse the repository at this point in the history
* refactor: use @aws-amplify/graphql-generator in model-generator

* chore: refresh lockfile
  • Loading branch information
dpilch authored Sep 21, 2023
1 parent 6b5fd14 commit 1a6dd46
Show file tree
Hide file tree
Showing 10 changed files with 772 additions and 278 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-zebras-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/model-generator': patch
---

refactor: use @aws-amplify/graphql-generator in model-generator
907 changes: 733 additions & 174 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/model-generator/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
```ts

import { StatementsTarget } from '@aws-amplify/graphql-generator';

// @public
export const createGraphqlDocumentGenerator: ({ apiId, }: GraphqlDocumentGeneratorFactoryParams) => GraphqlDocumentGenerator;

Expand All @@ -29,7 +31,7 @@ export type GraphqlDocumentGeneratorFactoryParams = {
};

// @public (undocumented)
export type TargetLanguage = 'typescript';
export type TargetLanguage = StatementsTarget;

// (No @packageDocumentation comment for this package)

Expand Down
5 changes: 2 additions & 3 deletions packages/model-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"update:api": "api-extractor run --local"
},
"dependencies": {
"@aws-amplify/graphql-docs-generator": "^4.1.0",
"@aws-sdk/client-appsync": "^3.398.0",
"prettier": "^3.0.3"
"@aws-amplify/graphql-generator": "^0.1.0",
"@aws-sdk/client-appsync": "^3.398.0"
}
}
20 changes: 20 additions & 0 deletions packages/model-generator/src/appsync_schema_fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from 'node:assert';
import { describe, it, mock } from 'node:test';
import { AppSyncClient } from '@aws-sdk/client-appsync';
import { AppSyncIntrospectionSchemaFetcher } from './appsync_schema_fetcher.js';

describe('AppSyncIntrospectionSchemaFetcher', () => {
it('fetches schema from AppSync', async () => {
const schema = Buffer.from([1, 2, 3, 4, 5]);
const send = mock.fn(() => {
return { schema };
});
const appSyncClient = {
send,
} as unknown as AppSyncClient;
const schemaFetcher = new AppSyncIntrospectionSchemaFetcher(appSyncClient);
const apiId = 'mock-api-id';
const res = await schemaFetcher.fetch(apiId);
assert.equal(res, '\x01\x02\x03\x04\x05');
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { AppSyncClient } from '@aws-sdk/client-appsync';
import { AppsyncGraphqlDocumentGenerationResult } from './appsync_graphql_document_generation_result.js';
import { AppSyncIntrospectionSchemaFetcher } from './appsync_schema_fetcher.js';
import {
AppSyncGraphqlDocumentGenerator,
Statements,
} from './graphql_document_generator.js';
import { GraphQLStatementsFormatter } from './graphql_statements_formatter.js';
import { GraphqlDocumentGenerator, TargetLanguage } from './model_generator.js';
import { AppSyncGraphqlDocumentGenerator } from './graphql_document_generator.js';
import { GraphqlDocumentGenerator } from './model_generator.js';

export type GraphqlDocumentGeneratorFactoryParams = {
apiId: string;
Expand All @@ -24,8 +20,6 @@ export const createGraphqlDocumentGenerator = ({
return new AppSyncGraphqlDocumentGenerator(
() =>
new AppSyncIntrospectionSchemaFetcher(new AppSyncClient()).fetch(apiId),
(_: TargetLanguage, statements: Statements) =>
new GraphQLStatementsFormatter().format(statements),
(fileMap) => new AppsyncGraphqlDocumentGenerationResult(fileMap)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('client generator', () => {
it('if `fetchSchema` returns null, it should throw an error', async () => {
const generator = new AppSyncGraphqlDocumentGenerator(
async () => null as unknown as string,
async () => '',
() => ({ writeToDirectory: () => Promise.resolve() })
);
await assert.rejects(() =>
Expand Down
38 changes: 5 additions & 33 deletions packages/model-generator/src/graphql_document_generator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { generateGraphQLDocuments } from '@aws-amplify/graphql-docs-generator';
import { generateStatements } from '@aws-amplify/graphql-generator';
import {
DocumentGenerationParameters,
DocumentGenerationResult,
GraphqlDocumentGenerator,
TargetLanguage,
} from './model_generator.js';

export type Statements = Map<string, string>;
/**
* Generates GraphQL documents for a given AppSync API
*/
Expand All @@ -18,50 +16,24 @@ export class AppSyncGraphqlDocumentGenerator
*/
constructor(
private fetchSchema: () => Promise<string>,
private format: (
language: TargetLanguage,
statements: Statements
) => Promise<string>,
private resultBuilder: (
fileMap: Record<string, string>
) => DocumentGenerationResult
) {}
private static languageExtensions: Record<TargetLanguage, string> = {
typescript: 'ts',
};
generateModels = async ({ language }: DocumentGenerationParameters) => {
const schema = await this.fetchSchema();

if (!schema) {
throw new Error('Invalid schema');
}

const generatedStatements = generateGraphQLDocuments(schema, {
const generatedStatements = generateStatements({
schema,
target: language,
maxDepth: 3,
typenameIntrospection: true,
});

const clientOps: Array<keyof typeof generatedStatements> = [
'queries',
'mutations',
'subscriptions',
];

const formattedFiles = await Promise.all(
clientOps.map(async (op) => {
const ops = generatedStatements[op];
const content = await this.format(language, ops as Map<string, string>);
const fileName = `${op}.${AppSyncGraphqlDocumentGenerator.languageExtensions[language]}`;
return { fileName, content };
})
);
const fileMap = formattedFiles.reduce<Record<string, string>>(
(prev: Record<string, string>, { content, fileName }) => {
prev[fileName] = content;
return prev;
},
{}
);
return this.resultBuilder(fileMap);
return this.resultBuilder(generatedStatements);
};
}
57 changes: 0 additions & 57 deletions packages/model-generator/src/graphql_statements_formatter.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/model-generator/src/model_generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type TargetLanguage = 'typescript';
import { StatementsTarget } from '@aws-amplify/graphql-generator';
export type TargetLanguage = StatementsTarget;

export type DocumentGenerationParameters = {
language: TargetLanguage;
Expand Down

0 comments on commit 1a6dd46

Please sign in to comment.