From 3dad22f12a2939c7dbdb3fa4ecea6f1ea148e583 Mon Sep 17 00:00:00 2001 From: Michael Hsu Date: Fri, 25 Jan 2019 08:50:08 +0800 Subject: [PATCH] fix(resolveImportPath): should work with relative path issue #52 (#54) --- .../__snapshots__/macro.test.js.snap | 77 +++++++++++++++++++ src/__tests__/macro.test.js | 7 ++ src/utils/resolveImportPath.js | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/__tests__/__snapshots__/macro.test.js.snap b/src/__tests__/__snapshots__/macro.test.js.snap index 78afaf7..4adb580 100644 --- a/src/__tests__/__snapshots__/macro.test.js.snap +++ b/src/__tests__/__snapshots__/macro.test.js.snap @@ -214,6 +214,83 @@ const query = { `; +exports[`macros [loader] should work with relative path issue#52: [loader] should work with relative path issue#52 1`] = ` + +import { loader } from 'graphql.macro'; +const query = loader('../__tests__/fixtures/query.graphql'); + + ↓ ↓ ↓ ↓ ↓ ↓ + +const query = { + "kind": "Document", + "definitions": [{ + "kind": "OperationDefinition", + "operation": "query", + "name": { + "kind": "Name", + "value": "User" + }, + "variableDefinitions": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [{ + "kind": "Field", + "name": { + "kind": "Name", + "value": "user" + }, + "arguments": [{ + "kind": "Argument", + "name": { + "kind": "Name", + "value": "id" + }, + "value": { + "kind": "IntValue", + "value": "5" + } + }], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [{ + "kind": "Field", + "name": { + "kind": "Name", + "value": "firstName" + }, + "arguments": [], + "directives": [] + }, { + "kind": "Field", + "name": { + "kind": "Name", + "value": "lastName" + }, + "arguments": [], + "directives": [] + }] + } + }] + } + }], + "loc": { + "start": 0, + "end": 62, + "source": { + "body": "query User {\\n user(id: 5) {\\n firstName\\n lastName\\n }\\n}\\n", + "name": "GraphQL request", + "locationOffset": { + "line": 1, + "column": 1 + } + } + } +}; + +`; + exports[`macros [loader] with absolute path and NODE_PATH: [loader] with absolute path and NODE_PATH 1`] = ` import { loader } from 'graphql.macro'; diff --git a/src/__tests__/macro.test.js b/src/__tests__/macro.test.js index 623e16b..a1d30e6 100644 --- a/src/__tests__/macro.test.js +++ b/src/__tests__/macro.test.js @@ -91,6 +91,13 @@ pluginTester({ const query = loader('./fixtures/query3.graphql'); `, }, + '[loader] should work with relative path issue#52': { + error: false, + code: ` + import { loader } from '../macro'; + const query = loader('../__tests__/fixtures/query.graphql'); + `, + }, // '[loader] multiple operations': { // error: false, // code: ` diff --git a/src/utils/resolveImportPath.js b/src/utils/resolveImportPath.js index 7fa6aa8..c9f10b5 100644 --- a/src/utils/resolveImportPath.js +++ b/src/utils/resolveImportPath.js @@ -11,7 +11,7 @@ const resolveImportPath = ({ filename: string, relativePath: string, }): string => { - if (relativePath.startsWith('./')) { + if (relativePath.startsWith('.')) { return path.join(filename, '..', relativePath); }