From eb1cd6f72d1bcafb5e24c4aead741e23b5551e46 Mon Sep 17 00:00:00 2001 From: Vinh Le Date: Thu, 4 Oct 2018 22:46:08 +0800 Subject: [PATCH 1/5] Support absolute paths and NODE_PATH --- .../__snapshots__/macro.test.js.snap | 104 ++++++++++++++++++ src/__tests__/fixtures/simpleFragment.graphql | 3 + src/__tests__/macro.test.js | 17 +++ src/macro.js | 9 +- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/fixtures/simpleFragment.graphql diff --git a/src/__tests__/__snapshots__/macro.test.js.snap b/src/__tests__/__snapshots__/macro.test.js.snap index b2cb53e..eecbae9 100644 --- a/src/__tests__/__snapshots__/macro.test.js.snap +++ b/src/__tests__/__snapshots__/macro.test.js.snap @@ -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'; diff --git a/src/__tests__/fixtures/simpleFragment.graphql b/src/__tests__/fixtures/simpleFragment.graphql new file mode 100644 index 0000000..7ade060 --- /dev/null +++ b/src/__tests__/fixtures/simpleFragment.graphql @@ -0,0 +1,3 @@ +fragment UserEntry1 on User { + firstName +} diff --git a/src/__tests__/macro.test.js b/src/__tests__/macro.test.js index e472d56..1c86d7d 100644 --- a/src/__tests__/macro.test.js +++ b/src/__tests__/macro.test.js @@ -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: ` diff --git a/src/macro.js b/src/macro.js index e74946c..e4b06f3 100644 --- a/src/macro.js +++ b/src/macro.js @@ -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'; @@ -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 } } }, @@ -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))); }); From 14d3c40abd806b676c7de7f5818a240dc4d63718 Mon Sep 17 00:00:00 2001 From: Vinh Le Date: Fri, 5 Oct 2018 07:48:01 +0800 Subject: [PATCH 2/5] Make test number fancy --- src/__tests__/index.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/__tests__/index.test.js diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js new file mode 100644 index 0000000..a141a55 --- /dev/null +++ b/src/__tests__/index.test.js @@ -0,0 +1,8 @@ +import macro from '../macro'; +import index from '../index'; + +describe('index', () => { + it('refers to macro correctly', () => { + expect(index).toBe(macro); + }); +}); From 9d0538ad2a5a9d4874dfe141e252dc5fa9ccfea2 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Tue, 23 Oct 2018 15:17:26 -0400 Subject: [PATCH 3/5] Add support for pulling from node modules if relative path does not exist --- src/macro.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/macro.js b/src/macro.js index e4b06f3..f2eb545 100644 --- a/src/macro.js +++ b/src/macro.js @@ -10,8 +10,19 @@ import compileWithFragment from './utils/compileWithFragment'; // console.log(printAST(referencePath.parentPath)) const cwd = fs.realpathSync(process.cwd()); -const resolvePathFromCwd = relativePath => - path.resolve(cwd, process.env.NODE_PATH || '.', relativePath); +const resolvePathFromCwd = relativePath => { + let resolvedPath = path.resolve( + cwd, + process.env.NODE_PATH || '.', + relativePath, + ); + return fs.exists(resolvedPath, function(exists) { + if (exists) { + return resolvedPath; + } + return path.resolve(cwd, 'node_modules', relativePath); + }); +}; function graphqlMacro({ references, From 13d3a8c02726911aef83feac21f545032016e13d Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sat, 27 Oct 2018 05:00:00 -0400 Subject: [PATCH 4/5] lint --- src/macro.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/macro.js b/src/macro.js index f2eb545..2a9a3a5 100644 --- a/src/macro.js +++ b/src/macro.js @@ -11,12 +11,12 @@ import compileWithFragment from './utils/compileWithFragment'; const cwd = fs.realpathSync(process.cwd()); const resolvePathFromCwd = relativePath => { - let resolvedPath = path.resolve( + const resolvedPath = path.resolve( cwd, process.env.NODE_PATH || '.', relativePath, ); - return fs.exists(resolvedPath, function(exists) { + return fs.exists(resolvedPath, (exists) => { if (exists) { return resolvedPath; } From 12b9df6525c3a58561babffa8b40e82e31c58dca Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sat, 27 Oct 2018 05:04:28 -0400 Subject: [PATCH 5/5] prettier --- src/macro.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macro.js b/src/macro.js index 2a9a3a5..6fbcdaf 100644 --- a/src/macro.js +++ b/src/macro.js @@ -16,7 +16,7 @@ const resolvePathFromCwd = relativePath => { process.env.NODE_PATH || '.', relativePath, ); - return fs.exists(resolvedPath, (exists) => { + return fs.exists(resolvedPath, exists => { if (exists) { return resolvedPath; }