Skip to content

Commit

Permalink
feat: add fragment support
Browse files Browse the repository at this point in the history
  • Loading branch information
evenchange4 committed Jan 19, 2018
1 parent 3bb0050 commit 7b474f2
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 86 deletions.
4 changes: 2 additions & 2 deletions flow-typed/npm/lint-staged_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// flow-typed signature: 6ffb7649c511c62236652196ec593200
// flow-typed version: <<STUB>>/lint-staged_v6.0.0/flow_v0.63.1
// flow-typed signature: 97e43513f7c63b32384370dca9c72b4e
// flow-typed version: <<STUB>>/lint-staged_v6.0.1/flow_v0.63.1

/**
* This is an autogenerated libdef stub for:
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphql.macro",
"name": "@michaelhsu/graphql.macro",
"version": "0.1.0",
"description": "Inline GraphQL AST at build-time with babel-plugin-macros.",
"description": "Compile GraphQL AST at build-time with babel-plugin-macros.",
"author": "Michael Hsu <evenchange4@gmail.com>",
"license": "MIT",
"main": "lib/index.js",
Expand Down Expand Up @@ -50,13 +50,17 @@
"github-changes": "1.1.2",
"graphql": "0.12.3",
"husky": "0.14.3",
"jest": "22.1.3",
"lint-staged": "6.0.0",
"jest": "22.1.4",
"lint-staged": "6.0.1",
"prettier": "1.10.2"
},
"jest": {
"collectCoverageFrom": ["src/**/*.js", "!src/**/*.test.js"],
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/lib/"],
"testPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/lib/",
"<rootDir>/example/"
],
"resetMocks": true,
"resetModules": true
},
Expand Down
231 changes: 221 additions & 10 deletions src/__tests__/__snapshots__/macro.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`macros [gql]: [gql] 1`] = `
exports[`macros [gql] with fragment: [gql] with fragment 1`] = `
import { gql } from 'graphql-tag.macro';
const userFragment = gql\`
fragment UserEntry1 on User {
firstName
}
\`;
const query = gql\`
{
query User {
user(id: 5) {
firstName
lastName
...UserEntry1
}
}
\${userFragment}
\`;
↓ ↓ ↓ ↓ ↓ ↓
const userFragment = {
"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": 51,
"source": {
"body": "\\n fragment UserEntry1 on User {\\n firstName\\n }\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
"column": 1
}
}
}
};
const query = {
"kind": "Document",
"definitions": [{
"kind": "OperationDefinition",
"operation": "query",
"name": {
"kind": "Name",
"value": "User"
},
"variableDefinitions": [],
"directives": [],
"selectionSet": {
Expand Down Expand Up @@ -47,28 +99,110 @@ const query = {
"kind": "Field",
"name": {
"kind": "Name",
"value": "firstName"
"value": "lastName"
},
"arguments": [],
"directives": []
}, {
"kind": "FragmentSpread",
"name": {
"kind": "Name",
"value": "UserEntry1"
},
"directives": []
}]
}
}]
}
}].concat(userFragment.definitions),
"loc": {
"start": 0,
"end": 82,
"source": {
"body": "\\n query User {\\n user(id: 5) {\\n lastName\\n ...UserEntry1\\n }\\n }\\n \\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
"column": 1
}
}
}
};
`;

exports[`macros [gql] without fragment: [gql] without fragment 1`] = `
import { gql } from 'graphql-tag.macro';
const query = gql\`
query User {
user(id: 5) {
lastName
...UserEntry1
}
}
\`;
↓ ↓ ↓ ↓ ↓ ↓
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": "lastName"
},
"arguments": [],
"directives": []
}, {
"kind": "FragmentSpread",
"name": {
"kind": "Name",
"value": "UserEntry1"
},
"directives": []
}]
}
}]
}
}],
"loc": {
"start": 0,
"end": 64,
"end": 79,
"source": {
"body": "\\n {\\n user(id: 5) {\\n firstName\\n lastName\\n }\\n }\\n",
"body": "\\n query User {\\n user(id: 5) {\\n lastName\\n ...UserEntry1\\n }\\n }\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
Expand All @@ -80,7 +214,7 @@ const query = {
`;

exports[`macros [loader]: [loader] 1`] = `
exports[`macros [loader] with fragment: [loader] with fragment 1`] = `
import { loader } from 'graphql-tag.macro';
const query = loader('./fixtures/query1.graphql');
Expand Down Expand Up @@ -135,7 +269,7 @@ const query = {
"kind": "Field",
"name": {
"kind": "Name",
"value": "firstName"
"value": "id"
},
"arguments": [],
"directives": []
Expand Down Expand Up @@ -219,9 +353,86 @@ const query = {
}],
"loc": {
"start": 0,
"end": 264,
"end": 257,
"source": {
"body": "fragment UserEntry1 on User {\\n firstName\\n}\\n\\nfragment UserEntry3 on User {\\n id\\n}\\nfragment UserEntry2 on User {\\n lastName\\n}\\n#import \\"./fragment1.graphql\\"\\n#import './fragment2.graphql'\\n\\nquery User {\\n user(id: 5) {\\n ...UserEntry1\\n ...UserEntry2\\n }\\n}\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
"column": 1
}
}
}
};
`;

exports[`macros [loader] without fragment: [loader] without fragment 1`] = `
import { loader } from 'graphql-tag.macro';
const query = loader('./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": "fragment UserEntry1 on User {\\n firstName\\n}\\n\\nfragment UserEntry3 on User {\\n firstName\\n}\\nfragment UserEntry2 on User {\\n lastName\\n}\\n#import \\"./fragment1.graphql\\"\\n#import './fragment2.graphql'\\n\\nquery User {\\n user(id: 5) {\\n ...UserEntry1\\n ...UserEntry2\\n }\\n}\\n",
"body": "query User {\\n user(id: 5) {\\n firstName\\n lastName\\n }\\n}\\n",
"name": "GraphQL request",
"locationOffset": {
"line": 1,
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/fragment1.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ fragment UserEntry1 on User {
}

fragment UserEntry3 on User {
firstName
id
}
6 changes: 6 additions & 0 deletions src/__tests__/fixtures/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query User {
user(id: 5) {
firstName
lastName
}
}

0 comments on commit 7b474f2

Please sign in to comment.