Support graphql(schema, `query`) (#2781)

master
Lucas Azzola 2017-09-12 04:18:11 +10:00 committed by Simon Lydell
parent f51929e262
commit 3f74c45b91
5 changed files with 52 additions and 12 deletions

View File

@ -4,9 +4,9 @@ function cleanAST(ast) {
return JSON.stringify(massageAST(ast), null, 2);
}
function massageAST(ast) {
function massageAST(ast, parent) {
if (Array.isArray(ast)) {
return ast.map(e => massageAST(e)).filter(e => e);
return ast.map(e => massageAST(e, parent)).filter(e => e);
}
if (ast && typeof ast === "object") {
// We remove extra `;` and add them when needed
@ -29,7 +29,7 @@ function massageAST(ast) {
const newObj = {};
for (const key in ast) {
if (typeof ast[key] !== "function") {
newObj[key] = massageAST(ast[key]);
newObj[key] = massageAST(ast[key], ast);
}
}
@ -219,6 +219,7 @@ function massageAST(ast) {
quasis.forEach(q => delete q.value);
}
// styled-components and graphql
if (
ast.type === "TaggedTemplateExpression" &&
@ -231,6 +232,13 @@ function massageAST(ast) {
) {
newObj.quasi.quasis.forEach(quasi => delete quasi.value);
}
if (
ast.type === "TemplateLiteral" &&
parent.type === "CallExpression" &&
parent.callee.name === "graphql"
) {
newObj.quasis.forEach(quasi => delete quasi.value);
}
return newObj;
}

View File

@ -68,14 +68,17 @@ function fromBabylonFlowOrTypeScript(path) {
*/
if (
parentParent &&
parentParent.type === "TaggedTemplateExpression" &&
parent.quasis.length === 1 &&
((parentParent.tag.type === "MemberExpression" &&
parentParent.tag.object.name === "graphql" &&
parentParent.tag.property.name === "experimental") ||
(parentParent.tag.type === "Identifier" &&
(parentParent.tag.name === "gql" ||
parentParent.tag.name === "graphql")))
((parentParent.type === "TaggedTemplateExpression" &&
parent.quasis.length === 1 &&
((parentParent.tag.type === "MemberExpression" &&
parentParent.tag.object.name === "graphql" &&
parentParent.tag.property.name === "experimental") ||
(parentParent.tag.type === "Identifier" &&
(parentParent.tag.name === "gql" ||
parentParent.tag.name === "graphql")))) ||
(parentParent.type === "CallExpression" &&
parentParent.callee.type === "Identifier" &&
parentParent.callee.name === "graphql"))
) {
return {
options: { parser: "graphql" },

View File

@ -1,5 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`graphql.js 1`] = `
graphql(schema, \`
mutation MarkReadNotificationMutation(
$input
: MarkReadNotificationData!
)
{ markReadNotification(data: $input ) { notification {seenState} } }\`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
graphql(
schema,
\`
mutation MarkReadNotificationMutation($input: MarkReadNotificationData!) {
markReadNotification(data: $input) {
notification {
seenState
}
}
}
\`
);
`;
exports[`graphql-tag.js 1`] = `
import gql from "graphql-tag";

View File

@ -0,0 +1,6 @@
graphql(schema, `
mutation MarkReadNotificationMutation(
$input
: MarkReadNotificationData!
)
{ markReadNotification(data: $input ) { notification {seenState} } }`)

View File

@ -4,5 +4,5 @@ module.exports = {
test: value =>
typeof value === "string" && value.indexOf(process.cwd()) !== -1,
print: (value, serializer) =>
serializer(value.replace(process.cwd(), "<cwd>"))
serializer(value.replace(process.cwd(), "<cwd>").replace(/\\/g, "/"))
};