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
2024-09-25 12:04:25 +02:00
committed by GitHub
parent 1d3a60023a
commit 358435d715
30 changed files with 78 additions and 62 deletions

2
.gitignore vendored
View File

@@ -24,3 +24,5 @@ build/**
**/stats
.favorites.json
.vscode
**/tsconfig.tsbuildinfo

View File

@@ -19,7 +19,8 @@
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true
"skipLibCheck": true,
"rootDir": "${configDir}/src"
},
"include": ["${configDir}/src"],
"exclude": ["${configDir}/dist", "${configDir}/node_modules"]

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"

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": {

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

View File

@@ -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 = `

View File

@@ -7,5 +7,3 @@ export enum MODULE_RESOURCE_TYPE {
SHARED = "shared",
ISOLATED = "isolated",
}
export { GraphQLSchema } from "graphql"

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"

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"

View File

@@ -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(

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",

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"

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"

View File

@@ -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 () {

View File

@@ -1,4 +1,4 @@
import GraphQLParser from "../../joiner/graphql-ast"
import { GraphQLParser } from "../graphql-parser"
describe("RemoteJoiner.parseQuery", () => {
beforeEach(() => {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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 })
}

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"

View File

@@ -1,3 +1,4 @@
export * from "./graphql"
export * from "./api-key"
export * from "./auth"
export * from "./bundles"

View File

@@ -3,11 +3,12 @@ import express from "express"
import { track } from "medusa-telemetry"
import { scheduleJob } from "node-schedule"
import { GracefulShutdownServer } from "@medusajs/utils"
import { gqlSchemaToTypes, GracefulShutdownServer } from "@medusajs/utils"
import http, { IncomingMessage, ServerResponse } from "http"
import { gqlSchemaToTypes, logger } from "@medusajs/framework"
import { logger } from "@medusajs/framework"
import loaders from "../loaders"
import { MedusaModule } from "@medusajs/modules-sdk"
const EVERY_SIXTH_HOUR = "0 */6 * * *"
const CRON_SCHEDULE = EVERY_SIXTH_HOUR
@@ -66,6 +67,7 @@ async function start({ port, directory, types }) {
outputDir: outputDirGeneratedTypes,
filename: "remote-query-entry-points",
schema: gqlSchema,
joinerConfigs: MedusaModule.getAllJoinerConfigs(),
})
logger.info("Geneated modules types")
}

View File

@@ -1,5 +1,5 @@
import { ConfigModule, MedusaContainer, PluginDetails } from "@medusajs/types"
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils"
import { ContainerRegistrationKeys, promiseAll, GraphQLSchema } from "@medusajs/utils"
import { asValue } from "awilix"
import { Express, NextFunction, Request, Response } from "express"
import { join } from "path"
@@ -12,7 +12,6 @@ import {
container,
expressLoader,
featureFlagsLoader,
GraphQLSchema,
JobLoader,
LinkLoader,
logger,

View File

@@ -1,18 +1,13 @@
import { makeExecutableSchema } from "@graphql-tools/schema"
import {
cleanGraphQLSchema,
gqlGetFieldsAndRelations,
MedusaModule,
} from "@medusajs/modules-sdk"
import { MedusaModule } from "@medusajs/modules-sdk"
import {
IndexTypes,
JoinerServiceConfigAlias,
ModuleJoinerConfig,
ModuleJoinerRelationship,
} from "@medusajs/types"
import { CommonEvents } from "@medusajs/utils"
import { CommonEvents, GraphQLUtils } from "@medusajs/utils"
import { schemaObjectRepresentationPropertiesToOmit } from "@types"
import { Kind, ObjectTypeDefinitionNode } from "graphql/index"
export const CustomDirectives = {
Listeners: {
@@ -25,7 +20,7 @@ export const CustomDirectives = {
}
export function makeSchemaExecutable(inputSchema: string) {
const { schema: cleanedSchema } = cleanGraphQLSchema(inputSchema)
const { schema: cleanedSchema } = GraphQLUtils.cleanGraphQLSchema(inputSchema)
return makeExecutableSchema({ typeDefs: cleanedSchema })
}
@@ -330,7 +325,7 @@ function processEntity(
)
currentObjectRepresentationRef.fields =
gqlGetFieldsAndRelations(entitiesMap, entityName) ?? []
GraphQLUtils.gqlGetFieldsAndRelations(entitiesMap, entityName) ?? []
/**
* Retrieve the module and alias for the current entity.
@@ -372,14 +367,16 @@ function processEntity(
const schemaParentEntity = Object.values(entitiesMap).filter((value: any) => {
return (
value.astNode &&
(value.astNode as ObjectTypeDefinitionNode).fields?.some((field: any) => {
let currentType = field.type
while (currentType.type) {
currentType = currentType.type
}
(value.astNode as GraphQLUtils.ObjectTypeDefinitionNode).fields?.some(
(field: any) => {
let currentType = field.type
while (currentType.type) {
currentType = currentType.type
}
return currentType.name?.value === entityName
})
return currentType.name?.value === entityName
}
)
)
})
@@ -410,7 +407,7 @@ function processEntity(
})
const isEntityListInParent =
entityFieldInParent.type.kind === Kind.LIST_TYPE
entityFieldInParent.type.kind === GraphQLUtils.Kind.LIST_TYPE
const entityTargetPropertyNameInParent = entityFieldInParent.name.value
/**

View File

@@ -1,10 +1,9 @@
import { join } from "path"
import { CustomDirectives, makeSchemaExecutable } from "./build-config"
import {
gqlSchemaToTypes as ModulesSdkGqlSchemaToTypes,
MedusaModule,
} from "@medusajs/modules-sdk"
import { FileSystem } from "@medusajs/utils"
import { FileSystem, gqlSchemaToTypes as ModulesSdkGqlSchemaToTypes, } from "@medusajs/utils"
import * as process from "process"
export async function gqlSchemaToTypes(schema: string) {
@@ -18,6 +17,7 @@ export async function gqlSchemaToTypes(schema: string) {
schema: executableSchema,
filename,
outputDir: dir,
joinerConfigs: MedusaModule.getAllJoinerConfigs(),
})
const fileSystem = new FileSystem(dir)

View File

@@ -4535,7 +4535,7 @@ __metadata:
languageName: node
linkType: hard
"@graphql-tools/merge@npm:^9.0.6":
"@graphql-tools/merge@npm:^9.0.6, @graphql-tools/merge@npm:^9.0.7":
version: 9.0.7
resolution: "@graphql-tools/merge@npm:9.0.7"
dependencies:
@@ -4611,7 +4611,7 @@ __metadata:
languageName: node
linkType: hard
"@graphql-tools/schema@npm:^10.0.3, @graphql-tools/schema@npm:^10.0.4":
"@graphql-tools/schema@npm:^10.0.3, @graphql-tools/schema@npm:^10.0.4, @graphql-tools/schema@npm:^10.0.6":
version: 10.0.6
resolution: "@graphql-tools/schema@npm:10.0.6"
dependencies:
@@ -6249,7 +6249,6 @@ __metadata:
"@medusajs/types": ^1.11.16
"@medusajs/utils": ^1.11.9
cross-env: ^5.2.1
graphql: ^16.6.0
jest: ^29.7.0
resolve-cwd: ^3.0.0
rimraf: ^5.0.1
@@ -6326,7 +6325,6 @@ __metadata:
"@medusajs/types": ^1.11.16
"@medusajs/utils": ^1.11.9
cross-env: ^5.2.1
graphql: ^16.6.0
jest: ^29.7.0
rimraf: ^5.0.1
typescript: ^5.6.2
@@ -6741,6 +6739,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@medusajs/utils@workspace:packages/core/utils"
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
@@ -21410,7 +21410,7 @@ __metadata:
languageName: node
linkType: hard
"graphql@npm:^16.6.0, graphql@npm:^16.8.1":
"graphql@npm:^16.8.1":
version: 16.8.1
resolution: "graphql@npm:16.8.1"
checksum: 129c318156b466f440914de80dbf7bc67d17f776f2a088a40cb0da611d19a97c224b1c6d2b13cbcbc6e5776e45ed7468b8432f9c3536724e079b44f1a3d57a8a