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

Fix generated type name prefix #37

Merged
merged 2 commits into from
Aug 31, 2022
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
7 changes: 5 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.231.3/containers/typescript-node/.devcontainer/base.Dockerfile

# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}
# Every allowed variants: https://mcr.microsoft.com/v2/vscode/devcontainers/javascript-node/tags/list
# Doc of each tags: https://github.com/microsoft/vscode-dev-containers/tree/main/containers/typescript-node/history
# Node v16.15.1
ARG VARIANT="0.204.1-16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'yarn'

- name: Check - Yarn dedupe
run: yarn dedupe -c
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'yarn'

- name: Build
run: yarn build
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v16.15.1
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-gql-plugin",
"version": "1.4.0",
"version": "1.4.1",
"packageManager": "yarn@3.2.1",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
9 changes: 7 additions & 2 deletions src/cached/cached-literal-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { ErrorCatcher } from '../create-error-catcher';
import { generateTypeFromLiteral } from '../generators/generate-type-from-literal';
import { DocumentInfos } from '../generators/generate-bottom-content';
import { createCacheSystem } from '../utils/cache-system';
import { CachedSchemaLoader, defaultProjectName } from './cached-schema-loader';
import {
CachedSchemaLoader,
defaultProjectName,
getProjectNameIfNotDefault,
} from './cached-schema-loader';

type CreateCachedLiteralParserOptions = {
cachedSchemaLoader: CachedSchemaLoader;
Expand Down Expand Up @@ -44,7 +48,7 @@ export const createCachedLiteralParser = ({
throw new Error(`Project not defined for name "${projectName}"`);
}

return project;
return { ...project, projectName };
};

const parser = createCacheSystem<
Expand All @@ -60,6 +64,7 @@ export const createCachedLiteralParser = ({
documentInfos: await generateTypeFromLiteral(
literal,
project.schemaDocument,
getProjectNameIfNotDefault(project.projectName),
project.extension.codegenConfig
),
staticGlobals: project.staticGlobals,
Expand Down
5 changes: 4 additions & 1 deletion src/cached/cached-schema-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type CachedSchemaLoader = ReturnType<typeof createCachedSchemaLoader>;

export const defaultProjectName = 'default';

export const getProjectNameIfNotDefault = (projectName: string) =>
projectName === defaultProjectName ? undefined : projectName;

export const createCachedSchemaLoader = ({
cachedGraphQLConfigLoader,
errorCatcher,
Expand Down Expand Up @@ -61,7 +64,7 @@ export const createCachedSchemaLoader = ({
schemaDocument,
staticGlobals: await generateTypeFromSchema(
schemaDocument,
projectName === defaultProjectName ? undefined : projectName,
getProjectNameIfNotDefault(projectName),
extension.codegenConfig
),
extension,
Expand Down
2 changes: 2 additions & 0 deletions src/generators/generate-type-from-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const plugins = [typescriptOperationsPlugin];
export const generateTypeFromLiteral = async (
literal: string,
schema: DocumentNode,
projectName?: string,
codegenConfig: typescriptOperationsPlugin.TypeScriptDocumentsPluginConfig = {}
): Promise<DocumentInfos> => {
const document = parse(literal);
Expand All @@ -22,6 +23,7 @@ export const generateTypeFromLiteral = async (
);

const config: typescriptOperationsPlugin.TypeScriptDocumentsPluginConfig = {
typesPrefix: projectName,
...codegenConfig,
};

Expand Down
11 changes: 10 additions & 1 deletion src/source-update/create-source-updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ describe('Create source updater', () => {
user(id: $id) {
id
name
provider
}
users {
id
Expand Down Expand Up @@ -401,7 +402,12 @@ describe('Create source updater', () => {
export interface DocumentMap {
[${query1.slice(4, -1)}]: TypedDocumentNode<{
__typename?: "Query";
user: { __typename?: "User"; id: string; name: string };
user: {
__typename?: "User";
id: string;
name: string;
provider?: CatalogProvider | null;
};
users: Array<{ __typename?: "User"; id: string; email: string }>;
}, Exact<{
id: Scalars["ID"];
Expand Down Expand Up @@ -440,6 +446,7 @@ describe('Create source updater', () => {
email: Scalars["String"];
name: Scalars["String"];
picture?: Maybe<Scalars["String"]>;
provider?: Maybe<CatalogProvider>;
};

export interface CatalogQuery {
Expand All @@ -452,6 +459,8 @@ describe('Create source updater', () => {
id: Scalars["ID"];
};

export type CatalogProvider = "GOOGLE" | "FACEBOOK";

/** All built-in and custom scalars, mapped to their actual values */
export interface Scalars {
ID: string;
Expand Down
6 changes: 6 additions & 0 deletions src/test-files/multi-project/catalog-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ type User {
email: String!
name: String!
picture: String
provider: Provider
}

type Query {
users: [User!]!
user(id: ID!): User!
}

enum Provider {
GOOGLE
FACEBOOK
}