diff --git a/CHANGELOG.md b/CHANGELOG.md index 47bf430d2c0..e65364118c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT +- Move out GraphQL query parsing into a new package [`graphql-tag`](https://github.com/apollostack/graphql-tag) with a backcompat shim for `apollo-client/gql`. [Issue #312](https://github.com/apollostack/apollo-client/issues/312) [PR #313](https://github.com/apollostack/apollo-client/pull/313) + ### v0.3.20 - Exported `writeQueryToStore` and `writeFragmentToStore` directly from `apollo-client` to match `readQueryFromStore` and `readFragmentFromStore`. [PR #311](https://github.com/apollostack/apollo-client/pull/311) diff --git a/gql.js b/gql.js index fcc856507a3..b2e2e99fbb2 100644 --- a/gql.js +++ b/gql.js @@ -1,4 +1,2 @@ -/* We are placing this file in the root to enable npm-link development - * Currently, gql resides in a submodule and is not able to be imported when linked - */ -module.exports = require('./lib/src/gql'); +// This is here for backcompat, even though the right way is to use the other package +module.exports = require('graphql-tag'); diff --git a/package.json b/package.json index 4157c204b3f..08ddef1834f 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "dependencies": { "es6-promise": "^3.1.2", "graphql": "^0.4.18 || ^0.5.0 || ^0.6.0", + "graphql-tag": "^0.1.1", "lodash.assign": "^4.0.8", "lodash.clonedeep": "^4.3.2", "lodash.countby": "^4.4.0", @@ -47,12 +48,12 @@ "lodash.includes": "^4.1.2", "lodash.isarray": "^4.0.0", "lodash.isboolean": "^3.0.3", + "lodash.isequal": "^4.2.0", "lodash.isnull": "^3.0.0", "lodash.isnumber": "^3.0.3", "lodash.isobject": "^3.0.2", "lodash.isstring": "^4.0.1", "lodash.isundefined": "^3.0.1", - "lodash.isequal": "^4.2.0", "redux": "^3.3.1", "symbol-observable": "^0.2.4", "whatwg-fetch": "^1.0.0" diff --git a/src/gql.ts b/src/gql.ts deleted file mode 100644 index 1c37f8a1b08..00000000000 --- a/src/gql.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { parse } from 'graphql/language/parser'; - -import { - Document, -} from 'graphql'; - -const cache: {[queryString: string]: Document} = {}; - -function parseDocument(doc: string): Document { - if (cache[doc]) { - return cache[doc]; - } - - const parsed = parse(doc); - - if (!parsed || parsed.kind !== 'Document') { - throw new Error('Not a valid GraphQL document.'); - } - - cache[doc] = parsed; - - return parsed as Document; -} - -// XXX This should eventually disallow arbitrary string interpolation, like Relay does -export default function gql(literals, ...substitutions): Document { - let result = ''; - - // run the loop only for the substitution count - for (let i = 0; i < substitutions.length; i++) { - result += literals[i]; - result += substitutions[i]; - } - - // add the last literal - result += literals[literals.length - 1]; - - return parseDocument(result); -} - -export function registerGqlTag() { - if (typeof window !== 'undefined') { - window['gql'] = gql; - } else if (typeof global !== 'undefined') { - global['gql'] = gql; - } -} diff --git a/test/QueryManager.ts b/test/QueryManager.ts index fdcba499e1c..758652420df 100644 --- a/test/QueryManager.ts +++ b/test/QueryManager.ts @@ -15,7 +15,7 @@ import { addTypenameToSelectionSet, } from '../src/queries/queryTransform'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { assert, diff --git a/test/batching.ts b/test/batching.ts index 67f6e3f6f03..6bc7c8392f1 100644 --- a/test/batching.ts +++ b/test/batching.ts @@ -5,7 +5,7 @@ import { assert } from 'chai'; import mockNetworkInterface, { mockBatchedNetworkInterface, } from './mocks/mockNetworkInterface'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { GraphQLResult } from 'graphql'; const networkInterface = mockNetworkInterface(); diff --git a/test/client.ts b/test/client.ts index 796b451fc41..486e8161c28 100644 --- a/test/client.ts +++ b/test/client.ts @@ -18,7 +18,7 @@ import { Store, } from '../src/store'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { createStore, diff --git a/test/diffAgainstStore.ts b/test/diffAgainstStore.ts index 9323ece5a71..23ba5866492 100644 --- a/test/diffAgainstStore.ts +++ b/test/diffAgainstStore.ts @@ -12,7 +12,7 @@ import { NormalizedCache, } from '../src/data/store'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; describe('diffing queries against the store', () => { it('returns nothing when the store is enough', () => { diff --git a/test/directives.ts b/test/directives.ts index afbc03cc646..54dc3b80176 100644 --- a/test/directives.ts +++ b/test/directives.ts @@ -9,7 +9,7 @@ import { getQueryDefinition, } from '../src/queries/getFromAST'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import cloneDeep = require('lodash.clonedeep'); diff --git a/test/getFromAST.ts b/test/getFromAST.ts index 40a64404541..f9a63b82867 100644 --- a/test/getFromAST.ts +++ b/test/getFromAST.ts @@ -14,7 +14,7 @@ import { OperationDefinition, } from 'graphql'; import { print } from 'graphql/language/printer'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { assert } from 'chai'; describe('AST utility functions', () => { diff --git a/test/gql.ts b/test/gql.ts deleted file mode 100644 index 20b091cc951..00000000000 --- a/test/gql.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { assert } from 'chai'; - -import gql from '../src/gql'; - -describe('gql', () => { - it('parses queries', () => { - assert.equal(gql`{ testQuery }`.kind, 'Document'); - }); - - it('returns the same object for the same query', () => { - assert.isTrue(gql`{ sameQuery }` === gql`{ sameQuery }`); - }); -}); diff --git a/test/networkInterface.ts b/test/networkInterface.ts index e07a8b4d483..0beea7c834d 100644 --- a/test/networkInterface.ts +++ b/test/networkInterface.ts @@ -19,7 +19,7 @@ import { MiddlewareRequest, } from '../src/middleware'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { print } from 'graphql/language/printer'; diff --git a/test/queryMerging.ts b/test/queryMerging.ts index 84f5d518a6b..48eacb5de5f 100644 --- a/test/queryMerging.ts +++ b/test/queryMerging.ts @@ -25,7 +25,7 @@ import { OperationDefinition, } from 'graphql'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { assert } from 'chai'; import cloneDeep = require('lodash.clonedeep'); diff --git a/test/queryTransform.ts b/test/queryTransform.ts index d0efa9978dd..3ee37f7eb43 100644 --- a/test/queryTransform.ts +++ b/test/queryTransform.ts @@ -8,7 +8,7 @@ import { getMutationDefinition, } from '../src/queries/getFromAST'; import { print } from 'graphql/language/printer'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; import { assert } from 'chai'; describe('query transforms', () => { diff --git a/test/readFromStore.ts b/test/readFromStore.ts index 4950ddbe64c..cea553be20f 100644 --- a/test/readFromStore.ts +++ b/test/readFromStore.ts @@ -10,7 +10,7 @@ import { StoreObject, } from '../src/data/store'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; describe('reading from the store', () => { it('rejects malformed queries', () => { diff --git a/test/roundtrip.ts b/test/roundtrip.ts index fa33ff5e9af..1aaf415b93e 100644 --- a/test/roundtrip.ts +++ b/test/roundtrip.ts @@ -7,7 +7,7 @@ import { Document, } from 'graphql'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; describe('roundtrip', () => { it('real graphql result', () => { diff --git a/test/scheduler.ts b/test/scheduler.ts index 646c7fd1abe..4b9bf6b505d 100644 --- a/test/scheduler.ts +++ b/test/scheduler.ts @@ -8,7 +8,7 @@ import { createApolloStore, } from '../src/store'; import mockNetworkInterface from './mocks/mockNetworkInterface'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; describe('QueryScheduler', () => { it('should throw an error if we try to register a non-polling query', () => { diff --git a/test/tests.ts b/test/tests.ts index ebfb50b4be9..b5531714a7d 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -16,7 +16,6 @@ import './networkInterface'; import './QueryManager'; import './client'; import './store'; -import './gql'; import './queryTransform'; import './getFromAST'; import './directives'; diff --git a/test/writeToStore.ts b/test/writeToStore.ts index 4906804c92a..4f70ca674ec 100644 --- a/test/writeToStore.ts +++ b/test/writeToStore.ts @@ -23,7 +23,7 @@ import { Node, } from 'graphql'; -import gql from '../src/gql'; +import gql from 'graphql-tag'; describe('writing to the store', () => { it('properly normalizes a trivial item', () => {