diff --git a/package.json b/package.json index 44b543f4..6f0da454 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "widgetizer-widgetizer", - "version": "0.3.4", + "version": "0.3.5", "description": "Production-ready Node.js GraphQL server for Express, HAPI, Koa", "main": "dist/index.js", "directories": { diff --git a/src/core/runQuery.test.ts b/src/core/runQuery.test.ts index f87e619e..85638df1 100644 --- a/src/core/runQuery.test.ts +++ b/src/core/runQuery.test.ts @@ -141,20 +141,32 @@ describe('runQuery', () => { }); }); + it('throws a validation error if there are missing variables', () => { + const query = `query TestVar($base: Int!){ testArgumentValue(base: $base) }`; + const expected = 'Variable "$base" of required type "Int!" was not provided.'; + return runQuery({ + schema: Schema, + query: query, + }).then((res) => { + return expect(res.errors[0].message).to.deep.equal(expected); + }); + }); + + it('runs the correct operation when operationName is specified', () => { - const query = ` + const query = ` query Q1 { testString } query Q2 { testRootValue }`; - const expected = { - testString: 'it works', + const expected = { + testString: 'it works', }; - return runQuery({ schema: Schema, query: query, operationName: 'Q1' }) - .then((res) => { - return expect(res.data).to.deep.equal(expected); - }); - }); + return runQuery({ schema: Schema, query: query, operationName: 'Q1' }) + .then((res) => { + return expect(res.data).to.deep.equal(expected); + }); + }); }); diff --git a/src/core/runQuery.ts b/src/core/runQuery.ts index 9f710ede..d229a737 100644 --- a/src/core/runQuery.ts +++ b/src/core/runQuery.ts @@ -19,6 +19,8 @@ export interface GqlResponse { errors?: Array; } +import { Promise } from 'es6-promise'; + export interface QueryOptions { schema: GraphQLSchema; query: string | Document; diff --git a/src/integrations/expressApollo.test.ts b/src/integrations/expressApollo.test.ts index 10ba39e0..f02b9f3d 100644 --- a/src/integrations/expressApollo.test.ts +++ b/src/integrations/expressApollo.test.ts @@ -104,6 +104,25 @@ describe('expressApollo', () => { }); }); + it('can handle a request with variables as string', () => { + const app = express(); + app.use('/graphql', bodyParser.json()); + app.use('/graphql', graphqlHTTP({ schema: Schema })); + const expected = { + testArgument: 'hello world', + }; + const req = request(app) + .post('/graphql') + .send({ + query: 'query test($echo: String!){ testArgument(echo: $echo) }', + variables: '{ "echo": "world" }', + }); + req.then((res) => { + expect(res.status).to.equal(200); + return expect(res.body.data).to.deep.equal(expected); + }); + }); + it('can handle a request with operationName', () => { const app = express(); app.use('/graphql', bodyParser.json()); diff --git a/src/integrations/expressApollo.ts b/src/integrations/expressApollo.ts index e3a2999e..bfa2962a 100644 --- a/src/integrations/expressApollo.ts +++ b/src/integrations/expressApollo.ts @@ -55,7 +55,12 @@ export function graphqlHTTP(options: ExpressApolloOptions | ExpressApolloOptions } // TODO: some sanity checks here. - const { query, variables, operationName } = req.body; + let { query, variables, operationName } = req.body; + + if (typeof variables === 'string') { + // TODO: catch errors + variables = JSON.parse(variables); + } // either query or operationName must be present. Return 400 otherwise // if only operationName is present, check if it's in store. Return 400 otherwise