Skip to content

Commit 6d747bc

Browse files
squirlyDeMoorJaspermischnic
authored
support named imports in graphql transformer (#4510)
* support named imports in graphql transformer fixes #4472 * apply pr feedback * fixup use PackageManager#require to load dependencies Co-authored-by: Jasper De Moor <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
1 parent e95fbae commit 6d747bc

File tree

12 files changed

+156
-53
lines changed

12 files changed

+156
-53
lines changed

β€Ž.flowconfigβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<PROJECT_ROOT>/packages/core/integration-tests/test/input/**
1111
<PROJECT_ROOT>/packages/core/integration-tests/test/integration/**
1212

13+
[untyped]
14+
.*/node_modules/graphql/error/GraphQLError.js.flow
15+
1316
[include]
1417

1518
[libs]

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"format": "prettier --write \"./packages/*/*/{src,bin,test}/**/*.{js,json,md}\"",
1818
"link-all": "node scripts/link-all.js packages",
1919
"unlink-all": "node scripts/unlink-all.js packages",
20+
"check": "flow check",
2021
"lint": "eslint . && prettier \"./packages/*/*/{src,bin,test}/**/*.{js,json,md}\" --list-different",
2122
"lint:readme": "node scripts/validate-readme-toc.js",
2223
"precommit": "lint-staged",

β€Žpackages/core/integration-tests/package.jsonβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"core-js": "^3.2.1",
2525
"cross-env": "^7.0.0",
2626
"get-port": "^5.0.0",
27-
"graphql": "^0.11.7",
28-
"graphql-tag": "^2.6.0",
27+
"graphql": "^15.0.0",
2928
"http-proxy-middleware": "^0.19.1",
3029
"jsdom": "^15.2.1",
3130
"json5": "^1.0.1",

β€Žpackages/core/integration-tests/test/graphql.jsβ€Ž

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'assert';
22
import path from 'path';
3-
import gql from 'graphql-tag';
43
import {bundle, run} from '@parcel/test-utils';
4+
import {parse, print} from 'graphql/language';
55

66
describe('graphql', function() {
77
it('should support requiring graphql files', async function() {
@@ -10,8 +10,8 @@ describe('graphql', function() {
1010
let output = await run(b);
1111
assert.equal(typeof output, 'function');
1212
assert.deepEqual(
13-
output().definitions,
14-
gql`
13+
normalize(output()),
14+
normalize(`
1515
{
1616
user(id: 5) {
1717
...UserFragment
@@ -22,7 +22,7 @@ describe('graphql', function() {
2222
firstName
2323
lastName
2424
}
25-
`.definitions,
25+
`),
2626
);
2727
});
2828

@@ -34,8 +34,8 @@ describe('graphql', function() {
3434
let output = await run(b);
3535
assert.equal(typeof output, 'function');
3636
assert.deepEqual(
37-
output().definitions,
38-
gql`
37+
normalize(output()),
38+
normalize(`
3939
{
4040
user(id: 6) {
4141
...UserFragment
@@ -52,7 +52,60 @@ describe('graphql', function() {
5252
address
5353
email
5454
}
55-
`.definitions,
55+
`),
56+
);
57+
});
58+
59+
it('should support importing fragments in other graphql files by name', async function() {
60+
let b = await bundle(
61+
path.join(__dirname, '/integration/graphql-named-import/index.js'),
62+
);
63+
64+
let output = await run(b);
65+
assert.equal(typeof output, 'function');
66+
assert.deepEqual(
67+
normalize(output()),
68+
normalize(`
69+
query MyQuery {
70+
user(id: 6) {
71+
...UserFragment
72+
address {
73+
...Address
74+
}
75+
}
76+
}
77+
78+
fragment UserFragment on User {
79+
firstName
80+
lastName
81+
...AnotherUserFragment
82+
}
83+
84+
fragment Address on Address {
85+
line1
86+
county
87+
postalCode
88+
}
89+
90+
fragment AnotherUserFragment on User {
91+
address
92+
email
93+
}
94+
95+
fragment otherUserFragment on User {
96+
friends {
97+
edges {
98+
nodes {
99+
name
100+
}
101+
}
102+
}
103+
}
104+
`),
56105
);
57106
});
58107
});
108+
109+
function normalize(body) {
110+
return print(parse(body));
111+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# import UserFragment, Address from './fragments.graphql'
2+
# import * from "./otherFragments.graphql"
3+
4+
query MyQuery {
5+
user(id: 6) {
6+
...UserFragment
7+
address {
8+
...Address
9+
}
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import './moreFragments.graphql'
2+
3+
fragment UserFragment on User {
4+
firstName
5+
lastName
6+
...AnotherUserFragment
7+
}
8+
9+
fragment Address on Address {
10+
line1
11+
county
12+
postalCode
13+
}
14+
15+
fragment Unused on User {
16+
email
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var MyQuery = require('./MyQuery.graphql');
2+
3+
module.exports = function () {
4+
return MyQuery;
5+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fragment AnotherUserFragment on User {
2+
address
3+
email
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fragment otherUserFragment on User {
2+
friends {
3+
edges {
4+
nodes {
5+
name
6+
}
7+
}
8+
}
9+
}

β€Žpackages/transformers/graphql/package.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@parcel/plugin": "^2.0.0-alpha.3.1"
2020
},
2121
"devDependencies": {
22-
"graphql-tag": "^2.10.1"
22+
"graphql": "^15.0.0",
23+
"graphql-import-macro": "^1.0.0"
2324
}
2425
}

0 commit comments

Comments
Β (0)