Skip to content

Commit 25a8294

Browse files
committed
add graphql-tag support
1 parent 4897077 commit 25a8294

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ bower install graphql.js --save
5353
#### Using NPM
5454
```bash
5555
npm install graphql.js --save
56+
57+
# or
58+
59+
yarn add graphql.js
5660
```
5761

5862
#### Using with Rails Asset Pipeline
@@ -75,6 +79,21 @@ GraphQL.js is **isomorphic**. You can use it in both **browser and Node.js**.
7579
var graphql = require('graphql.js')
7680
```
7781

82+
##### Using with `graphql-tag`
83+
84+
`[graphql-tag](https://github.com/apollographql/graphql-tag)` converts GraphQL query strings to AST. You can use `graphql-tag` with GraphQL.js
85+
86+
```js
87+
var gql = require('graphql-tag')
88+
var graphql = require('graphql.js')
89+
90+
// setup...
91+
92+
graph.query(gql`query { name }`)
93+
```
94+
95+
> Using `graphql-tag` will not allow you to use _auto declaration_ and _nested fragments_ syntaxes since these are not valid query syntax for GraphQL but only for this library.
96+
7897
## Connection
7998

8099
Create a simple connection to your GraphQL endpoint.

graphql.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@
167167
}
168168

169169
GraphQLClient.prototype.processQuery = function (query, fragments) {
170+
if (typeof query == 'object' && query.hasOwnProperty('kind') && query.hasOwnProperty('definitions')) {
171+
// This looks like a graphql AST.
172+
try {
173+
var graphql = require('graphql')
174+
query = graphql.print(query)
175+
} catch (e) {
176+
throw new Error("You tried to pass a graphql syntax tree but it cannot be compiled to string.")
177+
}
178+
}
170179
var fragmentRegexp = GraphQLClient.FRAGMENT_PATTERN
171180
var collectedFragments = this.collectFragments(query, fragments)
172181
query = query.replace(fragmentRegexp, function (_, $m) {
@@ -226,7 +235,7 @@
226235
}
227236

228237
GraphQLClient.prototype.buildQuery = function (query, variables) {
229-
return this.autoDeclare(this.processQuery(query, this._fragments, variables), variables)
238+
return this.autoDeclare(this.processQuery(query, this._fragments), variables)
230239
}
231240

232241
GraphQLClient.prototype.createSenderFunction = function (url) {

0 commit comments

Comments
 (0)