Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #997 from mattleff/sort-schema
feat(): Add sortSchema option
- Loading branch information
Showing
8 changed files
with
269 additions
and
18 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
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,48 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { printSchema, GraphQLSchema } from 'graphql'; | ||
import { FileSystemHelper } from '../../lib/schema-builder/helpers/file-system.helper'; | ||
import { sortedPrintedSchemaSnapshot } from '../utils/printed-schema.snapshot'; | ||
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants'; | ||
import { GraphQLSchemaHost } from '../../lib'; | ||
import { SortAutoSchemaModule } from '../graphql/sort-auto-schema.module'; | ||
|
||
describe('GraphQL sort autoSchemaFile schema', () => { | ||
let app: INestApplication; | ||
let schema: GraphQLSchema; | ||
let writeFileMock: jest.Mock; | ||
|
||
beforeEach(async () => { | ||
writeFileMock = jest.fn().mockImplementation(() => Promise.resolve()); | ||
const module = await Test.createTestingModule({ | ||
imports: [SortAutoSchemaModule], | ||
}) | ||
.overrideProvider(FileSystemHelper) | ||
.useValue({ | ||
writeFile: writeFileMock, | ||
}) | ||
.compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
|
||
const graphQLSchemaHost = app.get<GraphQLSchemaHost>(GraphQLSchemaHost); | ||
schema = graphQLSchemaHost.schema; | ||
}); | ||
|
||
it('should match schema snapshot', () => { | ||
expect(GRAPHQL_SDL_FILE_HEADER + printSchema(schema)).toEqual( | ||
sortedPrintedSchemaSnapshot, | ||
); | ||
|
||
expect(writeFileMock).toHaveBeenCalledTimes(1); | ||
expect(writeFileMock).toHaveBeenCalledWith( | ||
'schema.graphql', | ||
sortedPrintedSchemaSnapshot, | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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,59 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { printSchema, GraphQLSchema } from 'graphql'; | ||
import { SortSchemaModule } from '../graphql/sort-schema.module'; | ||
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants'; | ||
import { GraphQLSchemaHost } from '../../lib'; | ||
|
||
describe('GraphQL sort schema', () => { | ||
let app: INestApplication; | ||
let schema: GraphQLSchema; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [SortSchemaModule], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
|
||
const graphQLSchemaHost = app.get<GraphQLSchemaHost>(GraphQLSchemaHost); | ||
schema = graphQLSchemaHost.schema; | ||
}); | ||
|
||
it('should match schema snapshot', () => { | ||
expect(GRAPHQL_SDL_FILE_HEADER + printSchema(schema)).toEqual( | ||
expectedSchema, | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); | ||
|
||
const expectedSchema = `# ------------------------------------------------------ | ||
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY) | ||
# ------------------------------------------------------ | ||
type Cat { | ||
age: Int | ||
color: String | ||
id: Int | ||
name: String | ||
weight: Int | ||
} | ||
type Mutation { | ||
createCat(name: String): Cat | ||
} | ||
type Query { | ||
cat(id: ID!): Cat | ||
getCats: [Cat] | ||
} | ||
type Subscription { | ||
catCreated: Cat | ||
} | ||
`; |
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,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '../../lib'; | ||
import { RecipesModule } from '../code-first/recipes/recipes.module'; | ||
import { DirectionsModule } from '../code-first/directions/directions.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
RecipesModule, | ||
DirectionsModule, | ||
GraphQLModule.forRoot({ | ||
autoSchemaFile: 'schema.graphql', | ||
sortSchema: true, | ||
}), | ||
], | ||
}) | ||
export class SortAutoSchemaModule {} |
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,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { join } from 'path'; | ||
import { GraphQLModule } from '../../lib'; | ||
import { CatsModule } from './cats/cats.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
CatsModule, | ||
GraphQLModule.forRoot({ | ||
typePaths: [join(__dirname, '**', '*.graphql')], | ||
sortSchema: true, | ||
}), | ||
], | ||
}) | ||
export class SortSchemaModule {} |
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