allow variables to be passed as string

This commit is contained in:
Jonas Helfer 2016-06-26 21:06:37 -04:00
parent dcb02ed94c
commit cc4fa17c50
5 changed files with 48 additions and 10 deletions

View file

@ -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": {

View file

@ -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);
});
});
});

View file

@ -19,6 +19,8 @@ export interface GqlResponse {
errors?: Array<string>;
}
import { Promise } from 'es6-promise';
export interface QueryOptions {
schema: GraphQLSchema;
query: string | Document;

View file

@ -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());

View file

@ -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