-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract serializeToJSON and include type information in typesUsed
- Loading branch information
1 parent
cf75a2c
commit a90cf59
Showing
6 changed files
with
71 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters