forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
1 changed file
with
70 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { expectJSON } from '../../__testUtils__/expectJSON.js'; | ||
|
||
import type { GraphQLNamedType } from '../../type/index.js'; | ||
import type { GraphQLSchemaNormalizedConfig } from '../../type/schema.js'; | ||
|
||
import { buildSchema } from '../buildASTSchema.js'; | ||
import { mapSchemaConfig, SchemaElementKind } from '../mapSchemaConfig.js'; | ||
|
||
function toTypeMap(types: ReadonlyArray<GraphQLNamedType>) { | ||
const typeMap = Object.create(null); | ||
for (const type of types) { | ||
typeMap[type.name] = type; | ||
} | ||
return typeMap; | ||
} | ||
|
||
describe('mapSchemaConfig', () => { | ||
it('returns the original config when no mappers are included', () => { | ||
const schema = buildSchema('type Query'); | ||
const schemaConfig = schema.toConfig(); | ||
const newSchemaConfig = mapSchemaConfig(schemaConfig, () => ({})); | ||
expectJSON(newSchemaConfig).toDeepEqual(schemaConfig); | ||
}); | ||
|
||
it('returns a new schema config when using a schema mapper', () => { | ||
const schema = buildSchema('type Query'); | ||
const schemaConfig = schema.toConfig(); | ||
const schemaConfigWithDescription = { | ||
...schemaConfig, | ||
description: 'New description', | ||
}; | ||
|
||
const newSchemaConfig = mapSchemaConfig(schema.toConfig(), () => ({ | ||
[SchemaElementKind.SCHEMA]: () => schemaConfigWithDescription, | ||
})); | ||
|
||
expect(schemaConfigWithDescription).to.equal(newSchemaConfig); | ||
}); | ||
|
||
it('executes schema mappers last such that they receive mapped types', () => { | ||
const schema = buildSchema('type Query'); | ||
const schemaTypeMap = schema.getTypeMap(); | ||
const schemaConfig = schema.toConfig(); | ||
const schemaConfigWithDescription = { | ||
...schemaConfig, | ||
description: 'New description', | ||
}; | ||
|
||
function schemaMapper(config: GraphQLSchemaNormalizedConfig) { | ||
expect(config).not.to.equal(schemaConfig); | ||
|
||
const mappedSchemaTypeMap = toTypeMap(config.types); | ||
for (const [typeName, type] of Object.entries(schemaTypeMap)) { | ||
if (typeName === 'Query') { | ||
expect(mappedSchemaTypeMap[typeName]).not.to.equal(type); | ||
} else { | ||
expect(mappedSchemaTypeMap[typeName]).to.equal(type); | ||
} | ||
} | ||
return schemaConfigWithDescription; | ||
} | ||
|
||
mapSchemaConfig(schema.toConfig(), () => ({ | ||
[SchemaElementKind.SCHEMA]: schemaMapper, | ||
})); | ||
}); | ||
}); |