Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update buildTypeDefsAndResolvers.ts #803

Merged
merged 6 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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