Skip to content

Commit

Permalink
Make schema generation consistent (#6951)
Browse files Browse the repository at this point in the history
* Make schema generation consistent

* Add tests for schema generation consistency

* Add changeset

* Update packages/legacy/runtime/test/getMesh.test.ts

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>

* Update snapshot

---------

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
masiulis and ardatan committed May 8, 2024
1 parent 234d2f9 commit d9b5137
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-teachers-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/runtime": patch
---

Make schema generation consistent
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ scalar JSON
scalar ObjMap
""""""
type Query {
""""""
case(countryRegion: String): Case
Expand Down
6 changes: 3 additions & 3 deletions packages/legacy/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
];
const wrappedFetchFn: MeshFetch = wrapFetchWithPlugins(initialPluginList);
await Promise.allSettled(
sources.map(async apiSource => {
sources.map(async (apiSource, index) => {
const apiName = apiSource.name;
const sourceLogger = logger.child(apiName);
sourceLogger.debug(`Generating the schema`);
Expand Down Expand Up @@ -199,7 +199,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
}

const rootTypeMap = getRootTypeMap(apiSchema);
rawSources.push({
rawSources[index] = {
name: apiName,
schema: apiSchema,
executor: source.executor,
Expand All @@ -209,7 +209,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
batch: 'batch' in source ? source.batch : true,
merge: source.merge,
createProxyingResolver: createProxyingResolverFactory(apiName, rootTypeMap),
});
};
} catch (e: any) {
sourceLogger.error(
`Failed to generate the schema for the source "${apiName}"\n ${e.message}`,
Expand Down
69 changes: 69 additions & 0 deletions packages/legacy/runtime/test/getMesh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('getMesh', () => {
suffixRootTypeNames: boolean;
suffixFieldNames: boolean;
suffixResponses: boolean;
delayImport?: boolean;
}

function createGraphQLSchema(config: CreateSchemaConfiguration) {
Expand Down Expand Up @@ -96,6 +97,9 @@ describe('getMesh', () => {
store,
logger,
async importFn(moduleId) {
if (config.delayImport) {
await new Promise(r => setTimeout(r, 1));
}
if (moduleId.endsWith(`schema${config.suffix}.ts`)) {
return createGraphQLSchema(config);
}
Expand Down Expand Up @@ -323,4 +327,69 @@ describe('getMesh', () => {
expect(serializedOriginalError?.message).toContain('This is an error');
expect(serializedOriginalError?.stack).toContain('at Object.throwMe (');
});

it('generated consistent schema', async () => {
const sources = [
createGraphQLSource({
suffix: 'Large',
suffixRootTypeNames: true,
suffixFieldNames: true,
suffixResponses: true,
delayImport: true,
}),
...new Array(2).fill(0).map((_, i) =>
createGraphQLSource({
suffix: i.toString(),
suffixRootTypeNames: true,
suffixFieldNames: true,
suffixResponses: true,
}),
),
];
const mesh1 = await getMesh({
cache,
pubsub,
logger,
sources,
merger,
});

const mesh2 = await getMesh({
cache,
pubsub,
logger,
sources,
merger,
});

expect(printSchemaWithDirectives(mesh1.schema)).toEqual(
printSchemaWithDirectives(mesh2.schema),
);

expect(printSchemaWithDirectives(mesh1.schema)).toMatchInlineSnapshot(`
"schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Query {
helloLarge: String
hello0: String
hello1: String
}
type Mutation {
byeLarge: String
bye0: String
bye1: String
}
type Subscription {
waveLarge: String
wave0: String
wave1: String
}"
`);
});
});

0 comments on commit d9b5137

Please sign in to comment.