Skip to content

Commit

Permalink
feat(import path): Support absolute paths and NODE_PATH (#37)
Browse files Browse the repository at this point in the history
Resolves #36.

About NODE_PATH, we use to have this setting with projects created via create-react-app.

cc @evenchange4
  • Loading branch information
vinhlh authored and evenchange4 committed Jan 24, 2019
1 parent 3e9a16b commit ba5dba8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
104 changes: 104 additions & 0 deletions src/__tests__/__snapshots__/macro.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,110 @@ const query = {
`;

exports[`macros [loader] with absolute path and NODE_PATH: [loader] with absolute path and NODE_PATH 1`] = `
import { loader } from 'graphql.macro';
const query = loader('__tests__/fixtures/simpleFragment.graphql');
↓ ↓ ↓ ↓ ↓ ↓
const query = {
"kind": "Document",
"definitions": [{
"kind": "FragmentDefinition",
"name": {
"kind": "Name",
"value": "UserEntry1"
},
"typeCondition": {
"kind": "NamedType",
"name": {
"kind": "Name",
"value": "User"
}
},
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [{
"kind": "Field",
"name": {
"kind": "Name",
"value": "firstName"
},
"arguments": [],
"directives": []
}]
}
}],
"loc": {
"start": 0,
"end": 44,
"source": {
"body": "fragment UserEntry1 on User {\\n firstName\\n}\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
"column": 1
}
}
}
};
`;

exports[`macros [loader] with absolute path: [loader] with absolute path 1`] = `
import { loader } from 'graphql.macro';
const query = loader('src/__tests__/fixtures/simpleFragment.graphql');
↓ ↓ ↓ ↓ ↓ ↓
const query = {
"kind": "Document",
"definitions": [{
"kind": "FragmentDefinition",
"name": {
"kind": "Name",
"value": "UserEntry1"
},
"typeCondition": {
"kind": "NamedType",
"name": {
"kind": "Name",
"value": "User"
}
},
"directives": [],
"selectionSet": {
"kind": "SelectionSet",
"selections": [{
"kind": "Field",
"name": {
"kind": "Name",
"value": "firstName"
},
"arguments": [],
"directives": []
}]
}
}],
"loc": {
"start": 0,
"end": 44,
"source": {
"body": "fragment UserEntry1 on User {\\n firstName\\n}\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
"column": 1
}
}
}
};
`;

exports[`macros [loader] with fragment: [loader] with fragment 1`] = `
import { loader } from 'graphql.macro';
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/simpleFragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment UserEntry1 on User {
firstName
}
17 changes: 17 additions & 0 deletions src/__tests__/macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ pluginTester({
const query = loader('./fixtures/query1.graphql');
`,
},
'[loader] with absolute path': {
error: false,
code: `
import { loader } from '../macro';
const query = loader('src/__tests__/fixtures/simpleFragment.graphql');
`,
},
'[loader] with absolute path and NODE_PATH': {
error: false,
code: `
import { loader } from '../macro';
const query = loader('__tests__/fixtures/simpleFragment.graphql');
`,
setup: () => {
process.env.NODE_PATH = 'src/';
},
},
'[loader] with nested circular fragments': {
error: false,
code: `
Expand Down
9 changes: 8 additions & 1 deletion src/macro.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import path from 'path';
import fs from 'fs';
import { createMacro } from 'babel-plugin-macros';
import gqlTag from 'graphql-tag';
import serialize from 'babel-literal-to-ast';
Expand All @@ -8,6 +9,10 @@ import compileWithFragment from './utils/compileWithFragment';
// import printAST from 'ast-pretty-print';
// console.log(printAST(referencePath.parentPath))

const cwd = fs.realpathSync(process.cwd());
const resolvePathFromCwd = relativePath =>
path.resolve(cwd, process.env.NODE_PATH || '.', relativePath);

function graphqlMacro({
references,
state: { file: { opts: { filename } } },
Expand All @@ -28,7 +33,9 @@ function graphqlMacro({
// Case 2: import { loader } from 'graphql.macro'
loader.forEach(referencePath => {
referencePath.parentPath.node.arguments.forEach(({ value }) => {
const queryPath = path.join(filename, '..', value);
const queryPath = value.startsWith('./')
? path.join(filename, '..', value)
: resolvePathFromCwd(value);
const expanded = expandImports(queryPath); // Note: #import feature
referencePath.parentPath.replaceWith(serialize(gqlTag(expanded)));
});
Expand Down

0 comments on commit ba5dba8

Please sign in to comment.