Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support absolute paths and NODE_PATH #37

Merged
merged 2 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] multiple operations': {
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition does not cover the case when relative path starts with ../. I would rather use path.isAbsolute function to check whether path is absolute.

const queryPath = path.isAbsolute(value)
    ? resolvePathFromCwd(value)
    : path.join(filename, '..', value);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further testing, I found out that isAbsolute is not what we need here because it returns false for foo/bar. value.startsWith('./') || value.startsWith('../') suits better.

const expanded = expandImports(queryPath); // Note: #import feature
referencePath.parentPath.replaceWith(serialize(gqlTag(expanded)));
});
Expand Down