-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72412a6
commit 6a36fb1
Showing
12 changed files
with
221 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
src/mesh/schema/ | ||
src/mesh/sdk.ts | ||
__generated__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { GraphQLSchema } from 'graphql'; | ||
import { ApolloServer } from 'apollo-server'; | ||
import { Logger } from 'winston'; | ||
|
||
export function serveMesh( | ||
logger: Logger, | ||
schema: GraphQLSchema, | ||
contextBuilder: () => Record<string, any> | ||
) { | ||
const server = new ApolloServer({ | ||
schema, | ||
context: () => { | ||
const context = contextBuilder(); | ||
return context; | ||
} | ||
}); | ||
|
||
server.listen().then(({ url }) => { | ||
logger.info(`🕸️ Serving GraphQL Mesh GraphiQL: ${url}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Logger } from 'winston'; | ||
import { RawSourcesOutput } from '@graphql-mesh/runtime'; | ||
import { plugin as tsBasePlugin } from '@graphql-codegen/typescript'; | ||
import { plugin as tsResolversPlugin } from '@graphql-codegen/typescript-resolvers'; | ||
import { Types } from '@graphql-codegen/plugin-helpers'; | ||
import { GraphQLSchema } from 'graphql'; | ||
|
||
export async function generateTsTypes( | ||
logger: Logger, | ||
unifiedSchema: GraphQLSchema, | ||
rawSources: RawSourcesOutput | ||
): Promise<string> { | ||
const unifiedContextIdentifier = 'MeshContext'; | ||
|
||
const tsTypes = (await tsBasePlugin( | ||
unifiedSchema, | ||
[], | ||
{} | ||
)) as Types.ComplexPluginOutput; | ||
const tsResolversTypes = (await tsResolversPlugin(unifiedSchema, [], { | ||
useIndexSignature: true, | ||
noSchemaStitching: true, | ||
contextType: unifiedContextIdentifier, | ||
federation: false | ||
})) as Types.ComplexPluginOutput; | ||
|
||
const results = await Promise.all( | ||
Object.keys(rawSources).map(async apiName => { | ||
const source = rawSources[apiName]; | ||
|
||
if (source.handler.tsSupport) { | ||
return await source.handler.tsSupport({ | ||
schema: source.schema, | ||
getMeshSourcePayload: source.meshSourcePayload, | ||
name: apiName | ||
}); | ||
} else { | ||
logger.warn( | ||
`Mesh source ${apiName} doesn't support TypeScript, ignoring...` | ||
); | ||
} | ||
}) | ||
); | ||
|
||
let sdkItems: string[] = []; | ||
let contextItems: string[] = []; | ||
let models: string[] = []; | ||
|
||
for (const item of results) { | ||
if (item) { | ||
if (item.sdk) { | ||
sdkItems.push(item.sdk.codeAst); | ||
} | ||
if (item.context) { | ||
contextItems.push(item.context.codeAst); | ||
} | ||
if (item.models) { | ||
contextItems.push(item.models); | ||
} | ||
} | ||
} | ||
|
||
const contextType = `export type ${unifiedContextIdentifier} = ${results | ||
.filter(r => r && r.context) | ||
.map(r => r?.context?.identifier) | ||
.join(' & ')};`; | ||
|
||
return [ | ||
...(tsTypes.prepend || []), | ||
...(tsResolversTypes.prepend || []), | ||
tsTypes.content, | ||
tsResolversTypes.content, | ||
...models, | ||
...sdkItems, | ||
...contextItems, | ||
contextType, | ||
...(tsTypes.append || []), | ||
...(tsResolversTypes.append || []) | ||
] | ||
.filter(Boolean) | ||
.join('\n\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.