Skip to content

Commit

Permalink
feat: add schemaComposer option to `composeWithJson(typeName, json,…
Browse files Browse the repository at this point in the history
… options)`
  • Loading branch information
nodkz committed Mar 9, 2020
1 parent f19e08e commit f28fae3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ const schema = new GraphQLSchema({
Or do the same via `graphql-compose`:

```js
import { schemaComposer } from 'graphql-compose';
import { SchemaComposer } from 'graphql-compose';

const schemaComposer = new SchemaComposer();
const PersonTC = composeWithJson('CustomPerson', restApiResponse, { schemaComposer });

schemaComposer.Query.addFields({
person: {
Expand Down
12 changes: 10 additions & 2 deletions src/ObjectParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ObjectTypeComposer,
upperFirst,
ObjectTypeComposerFieldConfigDefinition,
schemaComposer,
SchemaComposer,
} from 'graphql-compose';

type GetValueOpts = {
Expand All @@ -10,12 +12,18 @@ type GetValueOpts = {
};

export default class ObjectParser {
static createTC(name: string, json: Record<string, any>): ObjectTypeComposer<any, any> {
static createTC(
name: string,
json: Record<string, any>,
opts?: { schemaComposer: SchemaComposer<any> }
): ObjectTypeComposer<any, any> {
if (!json || typeof json !== 'object') {
throw new Error('You provide empty object in second arg for `createTC` method.');
}

const tc = ObjectTypeComposer.createTemp(name);
const sc = opts?.schemaComposer || schemaComposer;

const tc = sc.createObjectTC(name);
Object.keys(json).forEach((fieldName) => {
const fieldConfig = this.getFieldConfig(json[fieldName], { typeName: name, fieldName });
tc.setField(fieldName, fieldConfig);
Expand Down

0 comments on commit f28fae3

Please sign in to comment.