f / graphql.js

A Simple and Isomorphic GraphQL Client for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`client.query().then` is not a function in 0.6.2 version

ilyazub opened this issue · comments

commented

This code doesn't work in v0.6.2 but works in v0.4.0:

const client = graphql('/api')
client.query(`query { me }`, { var1: '42' }).then(/* ... */ )

It seems like once number of arguments to returning function of GraphQLClient.prototype.createSenderFunction had changed from one to three, it became not working.

I had managed to fix it in three ways:

  1. Add two extra arguments to query
- client.query(`query { me }`, { var1: '42' }).then(/* ... */ )
+ client.query(`query { me }`, '', '', { var1: '42' }).then(/* ... */ )
  1. Remove query type from GraphQL query string
- client.query(`query { me }`, { var1: '42' }).then(/* ... */ )
+ client.query(`{ me }`, { var1: '42' }).then(/* ... */ )
  1. Add caller call to graphql.js itself to behave like 0.4.20 version before changes in GraphQLClient.prototype.createSenderFunction
if (arguments.length > 3) {
    return caller.apply(null, Array.prototype.slice.call(arguments, 3))
}
+if (arguments.length > 1) {
+    return caller.apply(null, Array.prototype.slice.call(arguments, 1))
+}
return caller

@f what is the correct way to fix this problem?

Hi ilyazub,

client.query returns a function, not a promise. Try using this

const client = graphql('/api')

 //see the extra function call `()` before `.then`
client.query(`{ me }`)( { var1: '42' }).then(/* ... */ )

or better to read the code

const client = graphql('/api')
const myQuery = client.query(`{ me }`);
myQuery({ var1: '42' }).then(/* ... */ )

Also have a look at the execution demo
See: https://github.com/f/graphql.js#direct-execution-with-run-and-es6-template-tag

I hope that helps