Skip to content

Commit

Permalink
Extract serializeToJSON and include type information in typesUsed
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnwalraven committed Dec 6, 2016
1 parent cf75a2c commit a90cf59
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 26 deletions.
12 changes: 0 additions & 12 deletions src/compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,6 @@ function sourceAt(location) {
return location.source.body.slice(location.start, location.end);
}

export function stringifyIR(ast, space) {
return JSON.stringify(ast, function(key, value) {
if (value === undefined) {
return null;
} else if (isType(value)) {
return String(value);
} else {
return value;
}
}, space);
}

export function printIR({ fields, inlineFragments, fragmentSpreads }) {
return fields && wrap('<', join(fragmentSpreads, ', '), '> ')
+ block(fields.map(field =>
Expand Down
13 changes: 3 additions & 10 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import fs from 'fs'
import { ToolError, logError } from './errors'
import { loadSchema, loadAndMergeQueryDocuments } from './loading'
import { validateQueryDocument } from './validation'
import { compileToIR, stringifyIR } from './compilation'
import { compileToIR } from './compilation'
import serializeToJSON from './serializeToJSON'
import { generateSource as generateSwiftSource } from './swift'
import { generateSource as generateTypescriptSource } from './typescript'

Expand All @@ -20,7 +21,7 @@ export default function generate(inputPaths, schemaPath, outputPath, target, opt
let output;
switch (target) {
case 'json':
output = generateIR(context);
output = serializeToJSON(context);
break;
case 'ts':
case 'typescript':
Expand All @@ -38,11 +39,3 @@ export default function generate(inputPaths, schemaPath, outputPath, target, opt
console.log(output);
}
}

function generateIR(context) {
return stringifyIR({
operations: Object.values(context.operations),
fragments: Object.values(context.fragments),
typesUsed: context.typesUsed
}, '\t');
}
63 changes: 63 additions & 0 deletions src/serializeToJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
isType,
GraphQLEnumType,
GraphQLInputObjectType,
} from 'graphql';

export default function serializeToJSON(context) {
return serializeAST({
operations: Object.values(context.operations),
fragments: Object.values(context.fragments),
typesUsed: context.typesUsed.map(serializeType),
}, '\t');
}

export function serializeAST(ast, space) {
return JSON.stringify(ast, function(key, value) {
if (value === undefined) {
return null;
} else if (isType(value)) {
if (expandTypes) {
return serializeType(value);
} else {
return String(value);
}
} else {
return value;
}
}, space);
}

function serializeType(type) {
if (type instanceof GraphQLEnumType) {
return serializeEnumType(type);
} else if (type instanceof GraphQLInputObjectType) {
return serializeInputObjectType(type);
}
}

function serializeEnumType(type) {
const { name, description } = type;
const values = type.getValues();

return {
kind: "EnumType",
name,
description,
values: values.map(value => (
{ name: value.name, description: value.description }
))
}
}

function serializeInputObjectType(type) {
const { name, description } = type;
const fields = Object.values(type.getFields());

return {
kind: "InputObjectType",
name,
description,
fields
}
}
5 changes: 3 additions & 2 deletions test/compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {

import { loadSchema } from '../src/loading'

import { compileToIR, stringifyIR, printIR } from '../src/compilation'
import { compileToIR } from '../src/compilation'
import { serializeAST } from '../src/serializeToJSON'

const schema = loadSchema(require.resolve('./starwars/schema.json'));

Expand Down Expand Up @@ -1141,7 +1142,7 @@ describe('Compiling query documents', () => {
});

function filteredIR(ir) {
return JSON.parse(stringifyIR(ir), function(key, value) {
return JSON.parse(serializeAST(ir), function(key, value) {
if (key === 'source') {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion test/swift/codeGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const schema = loadSchema(require.resolve('../starwars/schema.json'));

import CodeGenerator from '../../src/utilities/CodeGenerator';

import { compileToIR, printIR } from '../../src/compilation';
import { compileToIR } from '../../src/compilation';

describe('Swift code generation', function() {
beforeEach(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/codeGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const schema = loadSchema(require.resolve('../starwars/schema.json'));

import CodeGenerator from '../../src/utilities/CodeGenerator';

import { compileToIR, printIR } from '../../src/compilation';
import { compileToIR } from '../../src/compilation';

describe('TypeScript code generation', function() {
beforeEach(function() {
Expand Down

0 comments on commit a90cf59

Please sign in to comment.