Skip to content

Commit

Permalink
fix(cli): use correct TS types for scalars in the artifacts (#4451)
Browse files Browse the repository at this point in the history
* fix(cli): use correct TS types for scalars in the artifacts

* Better changeset

* Add File and upload scalars

* chore(dependencies): updated changesets for modified dependencies

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Sep 6, 2022
1 parent 24afabe commit a56ebce
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changeset/@graphql-mesh_cli-4451-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-mesh/cli": patch
---

dependencies updates:

- Added dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/graphql-scalars/v/1.18.0) (to `dependencies`)
14 changes: 14 additions & 0 deletions .changeset/chatty-melons-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@graphql-mesh/cli': patch
'@graphql-mesh/types': patch
---

Fix TypeScript typings for additional scalars;

For example;

- `BigInt` should be `bigint`
- `PositiveInt` should be `int`
- `File` should be `File`
- `NonEmptyString` should be `string`
- `DateTime` should be `Date`
2 changes: 1 addition & 1 deletion examples/graphql-file-upload-example/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sources:
endpoint: http://localhost:3002/graphql

additionalTypeDefs: |
extend type File {
extend type FileResult {
thumbnailImage(width: Int, height: Int): String @resolveTo(
requiredSelectionSet: "{ thumbnailImage }",
sourceName: "ResizeImages",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const GET_FILES = gql`
`;

export const UPLOAD_FILE = gql`
mutation uploadFile($upload: Upload!) {
mutation uploadFile($upload: File!) {
uploadFile(upload: $upload) {
filename
thumbnailImage(width: 320, height: 240)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Upload Example', () => {
const file = new File([Buffer.from('CONTENT')], 'test.txt');
const result = await execute(
/* GraphQL */ `
mutation UploadFile($upload: Upload!) {
mutation UploadFile($upload: File!) {
uploadFile(upload: $upload) {
filename
}
Expand Down
8 changes: 4 additions & 4 deletions examples/graphql-file-upload-example/upload-files/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ if (!existsSync(FILES_DIR)) {
module.exports = createServer({
schema: {
typeDefs: /* GraphQL */ `
scalar Upload
scalar File
type Query {
files: [File]
files: [FileResult]
}
type Mutation {
uploadFile(upload: Upload!): File!
uploadFile(upload: File!): FileResult!
deleteFile(filename: String): Boolean
}
type File {
type FileResult {
filename: String
base64: String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ type Stat {
population: ApiPopulation
}
"""The \`Upload\` scalar type represents a file upload."""
scalar Upload
"""The \`File\` scalar type represents a file upload."""
scalar File
type query_population_records_items_fields {
country_name: String
Expand Down
5 changes: 4 additions & 1 deletion examples/openapi-javascript-wiki/additional-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export const resolvers: Resolvers = {
}
);

if (result == null || !('items' in result)) {
return null;
}

if (result != null && 'items' in result) {
return result?.items?.[0]?.views || 0n;
}
return 0;
},
},
};
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@whatwg-node/server": "0.1.2",
"@whatwg-node/fetch": "0.3.2",
"graphql-yoga": "3.0.0-alpha-20220905163021-e923bb34",
"graphql-scalars": "1.18.0",
"itty-router": "2.6.1",
"itty-router-extras": "0.4.2",
"dotenv": "16.0.2",
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/commands/ts-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { pathExists, writeFile, writeJSON } from '@graphql-mesh/utils';
import { generateOperations } from './generate-operations';
import { GraphQLMeshCLIParams } from '..';
import JSON5 from 'json5';
import { resolvers as scalarResolvers } from 'graphql-scalars';

const unifiedContextIdentifier = 'MeshContext';

Expand Down Expand Up @@ -63,6 +64,7 @@ async function generateTypesForApi(options: {
schema: GraphQLSchema;
name: string;
contextVariables: Record<string, string>;
codegenScalarsConfig: Record<string, string>;
}) {
const baseTypes = await codegen({
filename: options.name + '_types.ts',
Expand All @@ -72,6 +74,7 @@ async function generateTypesForApi(options: {
namingConvention: 'keep',
enumsAsTypes: true,
ignoreEnumValuesFromSchema: true,
scalars: options.codegenScalarsConfig,
},
schemaAst: options.schema,
schema: undefined as any, // This is not necessary on codegen. Will be removed later
Expand Down Expand Up @@ -215,6 +218,21 @@ export async function generateTsArtifacts(
}
);
}
const codegenScalarsConfig = {
File: 'File',
Upload: 'File',
};
for (const resolverName in scalarResolvers) {
const scalarResolver = scalarResolvers[resolverName];
codegenScalarsConfig[scalarResolver.name] = scalarResolver.extensions?.codegenScalarType;
}
for (const typeName in unifiedSchema.getTypeMap()) {
const type = unifiedSchema.getType(typeName);
const codegenScalarType = type.extensions.codegenScalarType;
if (codegenScalarType) {
codegenScalarsConfig[typeName] = codegenScalarType;
}
}
const codegenOutput =
'// @ts-nocheck\n' +
(
Expand All @@ -235,6 +253,7 @@ export async function generateTsArtifacts(
noSchemaStitching: mergerType !== 'stitching',
contextType: unifiedContextIdentifier,
federation: mergerType === 'federation',
scalars: codegenScalarsConfig,
...codegenConfig,
},
schemaAst: unifiedSchema,
Expand Down Expand Up @@ -262,6 +281,7 @@ export async function generateTsArtifacts(
schema: sourceSchema,
name: source.name,
contextVariables: source.contextVariables,
codegenScalarsConfig,
});

if (item) {
Expand Down
4 changes: 0 additions & 4 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1342,10 +1342,6 @@
"$ref": "#/definitions/MongooseModel"
},
"additionalItems": false
},
"autoTypeMerging": {
"type": "boolean",
"description": "Enable Automatic Type Merging/Federation support"
}
}
},
Expand Down
4 changes: 0 additions & 4 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,6 @@ export interface MongooseHandler {
connectionString?: string;
models?: MongooseModel[];
discriminators?: MongooseModel[];
/**
* Enable Automatic Type Merging/Federation support
*/
autoTypeMerging?: boolean;
}
export interface MongooseModel {
name: string;
Expand Down
3 changes: 1 addition & 2 deletions website/src/generated-markdown/MongooseHandler.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,5 +1209,4 @@
* `pagination` - One of:
* `Boolean`
* `object`:
* `perPage` (type: `Int`)
* `autoTypeMerging` (type: `Boolean`) - Enable Automatic Type Merging/Federation support
* `perPage` (type: `Int`)
4 changes: 2 additions & 2 deletions website/src/pages/docs/guides/file-uploads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ sources:
transforms:
#
additionalTypeDefs: |
scalar Upload
scalar File
extend type Mutation {
uploadFile(upload: Upload!): File!
uploadFile(upload: File!): FileResult!
}
additionalResolvers:
Expand Down

0 comments on commit a56ebce

Please sign in to comment.