Skip to content

Commit

Permalink
feat(typedefs-resolvers): add sync function version (#803)
Browse files Browse the repository at this point in the history
Co-authored-by: Michał Lytek <[email protected]>
  • Loading branch information
Carassale and MichalLytek authored Aug 20, 2021
1 parent 776e458 commit 094654a
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 455 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- allow deprecating input fields and args (#794)
- support disabling inferring default values (#793)
- support readonly arrays for roles of `@Authorized` decorator (#935)
- add sync version of `buildTypeDefsAndResolvers` function (#803)
### Fixes
- **Breaking Change**: properly emit types nullability when `defaultValue` is provided and remove `ConflictingDefaultWithNullableError` error (#751)
- allow defining extension on field resolver level for fields also defined as a property of the class (#776)
Expand Down
10 changes: 9 additions & 1 deletion docs/bootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ const stateLink = withClientState({
// ...the rest of `ApolloClient` initialization code
```

Be aware that some of the TypeGraphQL features (i.a. [query complexity](complexity.md)) might not work with the `buildTypeDefsAndResolvers` approach because they use some low-level `graphql-js` features.
There's also a sync version of it - `buildTypeDefsAndResolversSync`:

```typescript
const { typeDefs, resolvers } = buildTypeDefsAndResolvers({
resolvers: [FirstResolver, SecondResolver],
});
```

However, be aware that some of the TypeGraphQL features (i.a. [query complexity](complexity.md)) might not work with the `buildTypeDefsAndResolvers` approach because they use some low-level `graphql-js` features.
13 changes: 11 additions & 2 deletions src/utils/buildTypeDefsAndResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { printSchema } from "graphql";
import { GraphQLSchema, printSchema } from "graphql";

import { BuildSchemaOptions, buildSchema } from "./buildSchema";
import { BuildSchemaOptions, buildSchema, buildSchemaSync } from "./buildSchema";
import { createResolversMap } from "./createResolversMap";

export async function buildTypeDefsAndResolvers(options: BuildSchemaOptions) {
const schema = await buildSchema(options);
return createTypeDefsAndResolversMap(schema);
}

export function buildTypeDefsAndResolversSync(options: BuildSchemaOptions) {
const schema = buildSchemaSync(options);
return createTypeDefsAndResolversMap(schema);
}

function createTypeDefsAndResolversMap(schema: GraphQLSchema) {
const typeDefs = printSchema(schema);
const resolvers = createResolversMap(schema);
return { typeDefs, resolvers };
Expand Down
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { buildSchema, buildSchemaSync, BuildSchemaOptions } from "./buildSchema";
export { buildTypeDefsAndResolvers } from "./buildTypeDefsAndResolvers";
export {
buildTypeDefsAndResolvers,
buildTypeDefsAndResolversSync,
} from "./buildTypeDefsAndResolvers";
export { createResolversMap } from "./createResolversMap";
export {
emitSchemaDefinitionFile,
Expand Down
Loading

0 comments on commit 094654a

Please sign in to comment.