Skip to content

Commit f28fae3

Browse files
committed
feat: add schemaComposer option to composeWithJson(typeName, json, options)
1 parent f19e08e commit f28fae3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ const schema = new GraphQLSchema({
136136
Or do the same via `graphql-compose`:
137137

138138
```js
139-
import { schemaComposer } from 'graphql-compose';
139+
import { SchemaComposer } from 'graphql-compose';
140+
141+
const schemaComposer = new SchemaComposer();
142+
const PersonTC = composeWithJson('CustomPerson', restApiResponse, { schemaComposer });
140143

141144
schemaComposer.Query.addFields({
142145
person: {

src/ObjectParser.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
ObjectTypeComposer,
33
upperFirst,
44
ObjectTypeComposerFieldConfigDefinition,
5+
schemaComposer,
6+
SchemaComposer,
57
} from 'graphql-compose';
68

79
type GetValueOpts = {
@@ -10,12 +12,18 @@ type GetValueOpts = {
1012
};
1113

1214
export default class ObjectParser {
13-
static createTC(name: string, json: Record<string, any>): ObjectTypeComposer<any, any> {
15+
static createTC(
16+
name: string,
17+
json: Record<string, any>,
18+
opts?: { schemaComposer: SchemaComposer<any> }
19+
): ObjectTypeComposer<any, any> {
1420
if (!json || typeof json !== 'object') {
1521
throw new Error('You provide empty object in second arg for `createTC` method.');
1622
}
1723

18-
const tc = ObjectTypeComposer.createTemp(name);
24+
const sc = opts?.schemaComposer || schemaComposer;
25+
26+
const tc = sc.createObjectTC(name);
1927
Object.keys(json).forEach((fieldName) => {
2028
const fieldConfig = this.getFieldConfig(json[fieldName], { typeName: name, fieldName });
2129
tc.setField(fieldName, fieldConfig);

0 commit comments

Comments
 (0)