From a56ebcec503402fbdb3d4e3561fd2e38e4dd5c43 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Wed, 7 Sep 2022 01:08:32 +0300 Subject: [PATCH] fix(cli): use correct TS types for scalars in the artifacts (#4451) * 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] --- .../@graphql-mesh_cli-4451-dependencies.md | 7 +++++++ .changeset/chatty-melons-sneeze.md | 14 +++++++++++++ .../graphql-file-upload-example/.meshrc.yml | 2 +- .../frontend/src/queries.js | 2 +- .../test/graphql-file-upload-example.test.js | 2 +- .../upload-files/server.js | 8 ++++---- .../json-schema-covid.test.js.snap | 4 ++-- .../additional-resolvers.ts | 5 ++++- packages/cli/package.json | 1 + packages/cli/src/commands/ts-artifacts.ts | 20 +++++++++++++++++++ packages/types/src/config-schema.json | 4 ---- packages/types/src/config.ts | 4 ---- .../MongooseHandler.generated.md | 3 +-- .../src/pages/docs/guides/file-uploads.mdx | 4 ++-- 14 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 .changeset/@graphql-mesh_cli-4451-dependencies.md create mode 100644 .changeset/chatty-melons-sneeze.md diff --git a/.changeset/@graphql-mesh_cli-4451-dependencies.md b/.changeset/@graphql-mesh_cli-4451-dependencies.md new file mode 100644 index 0000000000000..92b68f940913f --- /dev/null +++ b/.changeset/@graphql-mesh_cli-4451-dependencies.md @@ -0,0 +1,7 @@ +--- +"@graphql-mesh/cli": patch +--- + +dependencies updates: + +- Added dependency [`graphql-scalars@1.18.0` ↗︎](https://www.npmjs.com/package/graphql-scalars/v/1.18.0) (to `dependencies`) diff --git a/.changeset/chatty-melons-sneeze.md b/.changeset/chatty-melons-sneeze.md new file mode 100644 index 0000000000000..5b3d942309eb2 --- /dev/null +++ b/.changeset/chatty-melons-sneeze.md @@ -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` diff --git a/examples/graphql-file-upload-example/.meshrc.yml b/examples/graphql-file-upload-example/.meshrc.yml index 5e7e03c7da996..5615557b68b7a 100644 --- a/examples/graphql-file-upload-example/.meshrc.yml +++ b/examples/graphql-file-upload-example/.meshrc.yml @@ -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", diff --git a/examples/graphql-file-upload-example/frontend/src/queries.js b/examples/graphql-file-upload-example/frontend/src/queries.js index 427ddabd2622a..94bc78a111452 100644 --- a/examples/graphql-file-upload-example/frontend/src/queries.js +++ b/examples/graphql-file-upload-example/frontend/src/queries.js @@ -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) diff --git a/examples/graphql-file-upload-example/test/graphql-file-upload-example.test.js b/examples/graphql-file-upload-example/test/graphql-file-upload-example.test.js index 80dfd196258ce..6aa4665245fd5 100644 --- a/examples/graphql-file-upload-example/test/graphql-file-upload-example.test.js +++ b/examples/graphql-file-upload-example/test/graphql-file-upload-example.test.js @@ -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 } diff --git a/examples/graphql-file-upload-example/upload-files/server.js b/examples/graphql-file-upload-example/upload-files/server.js index 9cc6cbb9eaa80..dd2607015068d 100644 --- a/examples/graphql-file-upload-example/upload-files/server.js +++ b/examples/graphql-file-upload-example/upload-files/server.js @@ -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 } diff --git a/examples/json-schema-covid/tests/__snapshots__/json-schema-covid.test.js.snap b/examples/json-schema-covid/tests/__snapshots__/json-schema-covid.test.js.snap index 910d6e518f241..c753b34ce74b0 100644 --- a/examples/json-schema-covid/tests/__snapshots__/json-schema-covid.test.js.snap +++ b/examples/json-schema-covid/tests/__snapshots__/json-schema-covid.test.js.snap @@ -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 diff --git a/examples/openapi-javascript-wiki/additional-resolvers.ts b/examples/openapi-javascript-wiki/additional-resolvers.ts index aaf1bf641c0b7..2f9a8d9dc5f1b 100644 --- a/examples/openapi-javascript-wiki/additional-resolvers.ts +++ b/examples/openapi-javascript-wiki/additional-resolvers.ts @@ -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; }, }, }; diff --git a/packages/cli/package.json b/packages/cli/package.json index c00ba60a3cdf2..8c7580f4b8f51 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/packages/cli/src/commands/ts-artifacts.ts b/packages/cli/src/commands/ts-artifacts.ts index 6469a13d024ac..d94d66426521f 100644 --- a/packages/cli/src/commands/ts-artifacts.ts +++ b/packages/cli/src/commands/ts-artifacts.ts @@ -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'; @@ -63,6 +64,7 @@ async function generateTypesForApi(options: { schema: GraphQLSchema; name: string; contextVariables: Record; + codegenScalarsConfig: Record; }) { const baseTypes = await codegen({ filename: options.name + '_types.ts', @@ -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 @@ -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' + ( @@ -235,6 +253,7 @@ export async function generateTsArtifacts( noSchemaStitching: mergerType !== 'stitching', contextType: unifiedContextIdentifier, federation: mergerType === 'federation', + scalars: codegenScalarsConfig, ...codegenConfig, }, schemaAst: unifiedSchema, @@ -262,6 +281,7 @@ export async function generateTsArtifacts( schema: sourceSchema, name: source.name, contextVariables: source.contextVariables, + codegenScalarsConfig, }); if (item) { diff --git a/packages/types/src/config-schema.json b/packages/types/src/config-schema.json index d4a7616d3dfa9..214084fa15eb9 100644 --- a/packages/types/src/config-schema.json +++ b/packages/types/src/config-schema.json @@ -1342,10 +1342,6 @@ "$ref": "#/definitions/MongooseModel" }, "additionalItems": false - }, - "autoTypeMerging": { - "type": "boolean", - "description": "Enable Automatic Type Merging/Federation support" } } }, diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index f3bd79f947b8d..a1ef17b81eae6 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -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; diff --git a/website/src/generated-markdown/MongooseHandler.generated.md b/website/src/generated-markdown/MongooseHandler.generated.md index eb916ad31b925..4ce9f9735b8ae 100644 --- a/website/src/generated-markdown/MongooseHandler.generated.md +++ b/website/src/generated-markdown/MongooseHandler.generated.md @@ -1209,5 +1209,4 @@ * `pagination` - One of: * `Boolean` * `object`: - * `perPage` (type: `Int`) -* `autoTypeMerging` (type: `Boolean`) - Enable Automatic Type Merging/Federation support \ No newline at end of file + * `perPage` (type: `Int`) \ No newline at end of file diff --git a/website/src/pages/docs/guides/file-uploads.mdx b/website/src/pages/docs/guides/file-uploads.mdx index 6a39d1ec3c790..4eedab47c6731 100644 --- a/website/src/pages/docs/guides/file-uploads.mdx +++ b/website/src/pages/docs/guides/file-uploads.mdx @@ -33,10 +33,10 @@ sources: transforms: # … additionalTypeDefs: | - scalar Upload + scalar File extend type Mutation { - uploadFile(upload: Upload!): File! + uploadFile(upload: File!): FileResult! } additionalResolvers: