chore: Move graphl to a single place (#9303)

* chore: Move graphl to a single place

* add new line
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-09-25 12:04:25 +02:00
1 parent 1d3a60023a
commit 358435d715
30 files changed
+78 -62

No files matched your search

+1 -1
View File
@@ -13,4 +13,4 @@ export * from "./telemetry"
export const MEDUSA_CLI_PATH = require.resolve("@medusajs/medusa-cli")
export { GraphQLSchema, gqlSchemaToTypes, Query } from "@medusajs/modules-sdk"
export { Query } from "@medusajs/modules-sdk"
-1
View File
@@ -44,7 +44,6 @@
"@graphql-tools/schema": "^10.0.0",
"@medusajs/orchestration": "^0.5.7",
"@medusajs/utils": "^1.11.9",
"graphql": "^16.6.0",
"resolve-cwd": "^3.0.0"
},
"peerDependencies": {
+3 -4
View File
@@ -20,6 +20,7 @@ import {
ContainerRegistrationKeys,
createMedusaContainer,
dynamicImport,
GraphQLUtils,
isObject,
isString,
MedusaError,
@@ -29,7 +30,6 @@ import {
} from "@medusajs/utils"
import type { Knex } from "@mikro-orm/knex"
import { asValue } from "awilix"
import { GraphQLSchema } from "graphql/type"
import { MODULE_PACKAGE_NAMES } from "./definitions"
import {
MedusaModule,
@@ -39,7 +39,6 @@ import {
import { RemoteLink } from "./remote-link"
import { createQuery, RemoteQuery } from "./remote-query"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
import { cleanGraphQLSchema } from "./utils"
const LinkModulePackage = MODULE_PACKAGE_NAMES[Modules.LINK]
@@ -204,7 +203,7 @@ function cleanAndMergeSchema(loadedSchema) {
scalar DateTime
scalar JSON
`
const { schema: cleanedSchema, notFound } = cleanGraphQLSchema(
const { schema: cleanedSchema, notFound } = GraphQLUtils.cleanGraphQLSchema(
defaultMedusaSchema + loadedSchema
)
const mergedSchema = mergeTypeDefs(cleanedSchema)
@@ -232,7 +231,7 @@ export type MedusaAppOutput = {
link: RemoteLink | undefined
query: RemoteQueryFunction
entitiesMap?: Record<string, any>
gqlSchema?: GraphQLSchema
gqlSchema?: GraphQLUtils.GraphQLSchema
notFound?: Record<string, Record<string, string>>
runMigrations: RunMigrationFn
revertMigrations: RevertMigrationFn
@@ -1,6 +1,6 @@
import { mergeTypeDefs } from "@graphql-tools/merge"
import { makeExecutableSchema } from "@graphql-tools/schema"
import { cleanGraphQLSchema } from "../../utils/clean-graphql-schema"
import { cleanGraphQLSchema } from "@medusajs/utils"
export function getEntitiesMap(loadedSchema): Map<string, any> {
const defaultMedusaSchema = `
@@ -7,5 +7,3 @@ export enum MODULE_RESOURCE_TYPE {
SHARED = "shared",
ISOLATED = "isolated",
}
export { GraphQLSchema } from "graphql"
+2 -4
View File
@@ -1,4 +1,2 @@
export * from "./clean-graphql-schema"
export * from "./graphql-schema-to-fields"
export * from "./gql-schema-to-types"
export * from "./gql-get-fields-and-relations"
export * from "./linking-error"
export * from "./convert-data-to-link-definition"
+1 -2
View File
@@ -33,8 +33,7 @@
"typescript": "^5.6.2"
},
"dependencies": {
"@medusajs/utils": "^1.11.9",
"graphql": "^16.6.0"
"@medusajs/utils": "^1.11.9"
},
"peerDependencies": {
"awilix": "^8.0.1"
@@ -10,12 +10,11 @@ import {
} from "@medusajs/types"
import {
deduplicate,
extractRelationsFromGQL,
GraphQLUtils,
isDefined,
isString,
MedusaError,
} from "@medusajs/utils"
import GraphQLParser from "./graphql-ast"
const BASE_PATH = "_root"
@@ -140,7 +139,7 @@ export class RemoteJoiner {
graphqlQuery: string,
variables?: Record<string, unknown>
): RemoteJoinerQuery {
const parser = new GraphQLParser(graphqlQuery, variables)
const parser = new GraphQLUtils.GraphQLParser(graphqlQuery, variables)
return parser.parseQuery()
}
@@ -154,7 +153,9 @@ export class RemoteJoiner {
) {
this.options.autoCreateServiceNameAlias ??= true
if (this.options.entitiesMap) {
this.entityMap = extractRelationsFromGQL(this.options.entitiesMap)
this.entityMap = GraphQLUtils.extractRelationsFromGQL(
this.options.entitiesMap
)
}
this.buildReferences(
+2
View File
@@ -36,6 +36,8 @@
"typescript": "^5.6.2"
},
"dependencies": {
"@graphql-tools/merge": "^9.0.7",
"@graphql-tools/schema": "^10.0.6",
"@medusajs/types": "^1.11.16",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
+1
View File
@@ -17,3 +17,4 @@ export * as PromotionUtils from "./promotion"
export * as SearchUtils from "./search"
export * as ShippingProfileUtils from "./shipping"
export * as UserUtils from "./user"
export * as GraphQLUtils from "./graphql"
-1
View File
@@ -26,7 +26,6 @@ export * from "./get-node-version"
export * from "./get-selects-and-relations-from-object-array"
export * from "./get-set-difference"
export * from "./graceful-shutdown-server"
export * from "./graphql-relations-entity-map"
export * from "./group-by"
export * from "./handle-postgres-database-error"
export * from "./is-big-number"
@@ -1,4 +1,4 @@
import { cleanGraphQLSchema } from "../utils/clean-graphql-schema"
import { cleanGraphQLSchema } from "../clean-graphql"
describe("Clean Graphql Schema", function () {
it("Should keep the schema intact if all entities are available", function () {
@@ -1,4 +1,4 @@
import GraphQLParser from "../../joiner/graphql-ast"
import { GraphQLParser } from "../graphql-parser"
describe("RemoteJoiner.parseQuery", () => {
beforeEach(() => {
@@ -1,6 +1,6 @@
import { mergeTypeDefs } from "@graphql-tools/merge"
import { makeExecutableSchema } from "@graphql-tools/schema"
import { gqlGetFieldsAndRelations } from "../../utils"
import { gqlGetFieldsAndRelations } from "../../get-fields-and-relations"
const userModule = `
type User {
@@ -1,6 +1,6 @@
import { mergeTypeDefs } from "@graphql-tools/merge"
import { makeExecutableSchema } from "@graphql-tools/schema"
import { graphqlSchemaToFields } from "../../utils"
import { graphqlSchemaToFields } from "../../graphql-to-fields"
const userModule = `
type User {
@@ -28,7 +28,10 @@ interface Entity {
directives?: { [field: string]: Directive[] }
}
class GraphQLParser {
/**
* Remote joiner graphql parser
*/
export class GraphQLParser {
private ast: DocumentNode
constructor(input: string, private variables: Record<string, unknown> = {}) {
@@ -190,5 +193,3 @@ class GraphQLParser {
return remoteJoinConfig
}
}
export default GraphQLParser
@@ -1,16 +1,19 @@
import { MedusaModule } from "../medusa-module"
import { FileSystem, toCamelCase } from "@medusajs/utils"
import { GraphQLSchema } from "graphql/type"
import { parse, printSchema } from "graphql"
import { codegen } from "@graphql-codegen/core"
import * as typescriptPlugin from "@graphql-codegen/typescript"
import { ModuleJoinerConfig } from "@medusajs/types"
function buildEntryPointsTypeMap(
function buildEntryPointsTypeMap({
schema,
joinerConfigs,
}: {
schema: string
): { entryPoint: string; entityType: any }[] {
joinerConfigs: ModuleJoinerConfig[]
}): { entryPoint: string; entityType: any }[] {
// build map entry point to there type to be merged and used by the remote query
const joinerConfigs = MedusaModule.getAllJoinerConfigs()
return joinerConfigs
.flatMap((config) => {
const aliases = Array.isArray(config.alias)
@@ -41,15 +44,17 @@ async function generateTypes({
outputDir,
filename,
config,
joinerConfigs,
}: {
outputDir: string
filename: string
config: Parameters<typeof codegen>[0]
joinerConfigs: ModuleJoinerConfig[]
}) {
const fileSystem = new FileSystem(outputDir)
let output = await codegen(config)
const entryPoints = buildEntryPointsTypeMap(output)
const entryPoints = buildEntryPointsTypeMap({ schema: output, joinerConfigs })
const interfaceName = toCamelCase(filename)
@@ -81,14 +86,17 @@ ${entryPoints
}
}
// TODO: rename from gqlSchemaToTypes to grapthqlToTsTypes
export async function gqlSchemaToTypes({
schema,
outputDir,
filename,
joinerConfigs,
}: {
schema: GraphQLSchema
outputDir: string
filename: string
joinerConfigs: ModuleJoinerConfig[]
}) {
const config = {
documents: [],
@@ -114,5 +122,5 @@ export async function gqlSchemaToTypes({
},
}
await generateTypes({ outputDir, filename, config })
await generateTypes({ outputDir, filename, config, joinerConfigs })
}
+9
View File
@@ -0,0 +1,9 @@
export * from "./graphql-parser"
export * from "./graphql-to-fields"
export * from "./extract-relations-from-graphql"
export * from "./clean-graphql"
export * from "./graphql-to-ts-types"
export * from "./get-fields-and-relations"
export * from "graphql"
export * from "graphql/type"
+1
View File
@@ -1,3 +1,4 @@
export * from "./graphql"
export * from "./api-key"
export * from "./auth"
export * from "./bundles"