Skip to content

Commit

Permalink
feat: add new option omitScalars for toSDL() methods. Eg. `schemaCo…
Browse files Browse the repository at this point in the history
…mposer.toSDL({ omitScalars: true })` will print schema SDL without Scalar types. Closes #239
  • Loading branch information
nodkz committed Mar 5, 2020
1 parent 08e1354 commit d431244
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/InputTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ export class InputTypeComposer<TContext> {
nestedTypes = nestedTypes.sort((a, b) => a.getTypeName().localeCompare(b.getTypeName()));
nestedTypes.forEach(t => {
if (t !== this && !exclude.includes(t.getTypeName())) {
r += `\n\n${t.toSDL(innerOpts)}`;
const sdl = t.toSDL(innerOpts);
if (sdl) r += `\n\n${sdl}`;
}
});
return r;
Expand Down
3 changes: 2 additions & 1 deletion src/InterfaceTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,8 @@ export class InterfaceTypeComposer<TSource, TContext> {
nestedTypes = nestedTypes.sort((a, b) => a.getTypeName().localeCompare(b.getTypeName()));
nestedTypes.forEach(t => {
if (t !== this && !exclude.includes(t.getTypeName())) {
r += `\n\n${t.toSDL(innerOpts)}`;
const sdl = t.toSDL(innerOpts);
if (sdl) r += `\n\n${sdl}`;
}
});
return r;
Expand Down
3 changes: 2 additions & 1 deletion src/ObjectTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,8 @@ export class ObjectTypeComposer<TSource, TContext> {
nestedTypes = nestedTypes.sort((a, b) => a.getTypeName().localeCompare(b.getTypeName()));
nestedTypes.forEach(t => {
if (t !== this && !exclude.includes(t.getTypeName())) {
r += `\n\n${t.toSDL(innerOpts)}`;
const sdl = t.toSDL(innerOpts);
if (sdl) r += `\n\n${sdl}`;
}
});
return r;
Expand Down
1 change: 1 addition & 0 deletions src/SchemaComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ export class SchemaComposer<TContext> extends TypeStorage<any, NamedTypeComposer
* @param {Object} options
* @param {String[]} options.include - add to SDL only provided types
* @param {String[]} options.exclude - do not add provided types to SDL
* @param {Boolean} options.omitScalars - do not add Scalar types to SDL
* @param {Boolean} options.omitDescriptions - do not add descriptions to SDL
* @param {Boolean} options.omitDirectiveDefinitions - do not add directives definitions to SDL
* @param {Boolean} options.commentDescriptions - print descriptions like comments, starting with #
Expand Down
3 changes: 2 additions & 1 deletion src/UnionTypeComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ export class UnionTypeComposer<TSource, TContext> {
nestedTypes = nestedTypes.sort((a, b) => a.getTypeName().localeCompare(b.getTypeName()));
nestedTypes.forEach(t => {
if (t !== this && !exclude.includes(t.getTypeName())) {
r += `\n\n${t.toSDL(innerOpts)}`;
const sdl = t.toSDL(innerOpts);
if (sdl) r += `\n\n${sdl}`;
}
});
return r;
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/ObjectTypeComposer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,33 @@ describe('ObjectTypeComposer', () => {
scalar String
`);

expect(
sc1.getOTC('User').toSDL({
deep: true,
omitScalars: true,
})
).toBe(dedent`
type User implements I1 & I2 {
f1: String
f2: Int
f3: User
f4(f: Filter): Int
}
input Filter {
a: Boolean
b: Filter
}
interface I1 {
f1: String
}
interface I2 {
f2: Int
}
`);

expect(
sc1.getOTC('User').toSDL({
deep: true,
Expand Down
57 changes: 38 additions & 19 deletions src/utils/__tests__/schemaPrinter-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SchemaComposer } from '../../SchemaComposer';
import { dedent } from '../dedent';

describe('schemaPrinter', () => {
describe('printSchemaComposer()', () => {
Expand All @@ -21,8 +22,8 @@ describe('schemaPrinter', () => {
sc.toSDL({
omitDescriptions: true,
})
).toMatchInlineSnapshot(`
"directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
).toBe(dedent`
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
scalar _FieldSet
Expand All @@ -32,9 +33,9 @@ describe('schemaPrinter', () => {
me: User
}
type User @key(fields: \\"id\\") {
type User @key(fields: "id") {
id: ID!
}"
}
`);
});

Expand All @@ -44,16 +45,16 @@ describe('schemaPrinter', () => {
omitDescriptions: true,
omitDirectiveDefinitions: true,
})
).toMatchInlineSnapshot(`
"scalar ID
).toBe(dedent`
scalar ID
type Query {
me: User
}
type User @key(fields: \\"id\\") {
type User @key(fields: "id") {
id: ID!
}"
}
`);
});

Expand All @@ -63,14 +64,14 @@ describe('schemaPrinter', () => {
omitDescriptions: true,
exclude: ['User'],
})
).toMatchInlineSnapshot(`
"directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
).toBe(dedent`
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
scalar _FieldSet
type Query {
me: User
}"
}
`);
});

Expand All @@ -80,16 +81,16 @@ describe('schemaPrinter', () => {
omitDescriptions: true,
include: ['User'],
})
).toMatchInlineSnapshot(`
"directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
).toBe(dedent`
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
scalar _FieldSet
scalar ID
type User @key(fields: \\"id\\") {
type User @key(fields: "id") {
id: ID!
}"
}
`);
});

Expand All @@ -100,14 +101,32 @@ describe('schemaPrinter', () => {
include: ['User'],
exclude: ['User', 'ID'],
})
).toMatchInlineSnapshot(`
"directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
).toBe(dedent`
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
scalar _FieldSet
type User @key(fields: \\"id\\") {
type User @key(fields: "id") {
id: ID!
}"
}
`);
});

it('should print schema in SDL without scalars', () => {
expect(
sc.toSDL({
omitScalars: true,
})
).toBe(dedent`
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
}
`);
});
});
Expand Down
7 changes: 7 additions & 0 deletions src/utils/schemaPrinter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ type Options = {
*/
omitDescriptions?: boolean;

/**
* Do not print Scalars for types
*
* Default: false
*/
omitScalars?: boolean | null;

/**
* Sort fields, args and interfaces.
* Useful for snapshot testing.
Expand Down
8 changes: 8 additions & 0 deletions src/utils/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ type Options = {
*/
omitDescriptions?: ?boolean,

/**
* Do not print Scalars for types
*
* Default: false
*/
omitScalars?: boolean | null,

/**
* Sort fields, args and interfaces.
* Useful for snapshot testing.
Expand Down Expand Up @@ -258,6 +265,7 @@ export function printType(type: GraphQLNamedType, options?: Options): string {
}

export function printScalar(type: GraphQLScalarType, options?: Options): string {
if (options?.omitScalars) return '';
return `${printDescription(type, options)}scalar ${type.name}${printNodeDirectives(
type.astNode
)}`;
Expand Down

0 comments on commit d431244

Please sign in to comment.