mirror of
https://github.com/vale981/apollo-server
synced 2025-03-05 17:51:40 -05:00
add tests, fix broken validation
This commit is contained in:
parent
d21fcd3ca6
commit
e1a5f261d4
2 changed files with 114 additions and 7 deletions
|
@ -8,6 +8,9 @@ import {
|
|||
GraphQLSchema,
|
||||
GraphQLObjectType,
|
||||
GraphQLString,
|
||||
GraphQLInt,
|
||||
GraphQLNonNull,
|
||||
parse,
|
||||
} from 'graphql';
|
||||
|
||||
import { runQuery } from './runQuery';
|
||||
|
@ -21,22 +24,125 @@ const QueryType = new GraphQLObjectType({
|
|||
return 'it works';
|
||||
},
|
||||
},
|
||||
testRootValue: {
|
||||
type: GraphQLString,
|
||||
resolve(root) {
|
||||
return root + ' works';
|
||||
},
|
||||
},
|
||||
testContextValue: {
|
||||
type: GraphQLString,
|
||||
resolve(root, args, context) {
|
||||
return context + ' works';
|
||||
},
|
||||
},
|
||||
testArgumentValue: {
|
||||
type: GraphQLInt,
|
||||
resolve(root, args, context) {
|
||||
return args['base'] + 5;
|
||||
},
|
||||
args: {
|
||||
base: { type: new GraphQLNonNull(GraphQLInt) },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const Schema = new GraphQLSchema({
|
||||
query: QueryType,
|
||||
});
|
||||
// XXX can be removed after tests are actually writen
|
||||
/* tslint:enable:no-unused-variable */
|
||||
|
||||
describe('runQuery', () => {
|
||||
it('returns a response', () => {
|
||||
// XXX can be removed after tests are actually writen
|
||||
// tslint:disable-next-line:no-unused-variable
|
||||
it('returns the right result when query is a string', () => {
|
||||
const query = `{ testString }`;
|
||||
const expected = { testString: 'it works' };
|
||||
return runQuery({ schema: Schema, query: query }).then((res) => {
|
||||
return runQuery({ schema: Schema, query: query })
|
||||
.then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns the right result when query is a document', () => {
|
||||
const query = parse(`{ testString }`);
|
||||
const expected = { testString: 'it works' };
|
||||
return runQuery({ schema: Schema, query: query })
|
||||
.then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a validation error if the query string does not pass validation', () => {
|
||||
const query = `query TestVar($base: String){ testArgumentValue(base: $base) }`;
|
||||
const expected = 'Variable "$base" of type "String" used in position expecting type "Int!".';
|
||||
return runQuery({
|
||||
schema: Schema,
|
||||
query: query,
|
||||
variables: { base: 1 },
|
||||
}).then((res) => {
|
||||
expect(res.data).to.be.undefined;
|
||||
expect(res.errors.length).to.equal(1);
|
||||
return expect(res.errors[0].message).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('does not run validation if the query is a document', () => {
|
||||
// this would not pass validation, because $base ought to be Int!, not String
|
||||
// what effecively happens is string concatentation, but it's returned as Int
|
||||
const query = parse(`query TestVar($base: String){ testArgumentValue(base: $base) }`);
|
||||
const expected = { testArgumentValue: 15 };
|
||||
return runQuery({
|
||||
schema: Schema,
|
||||
query: query,
|
||||
variables: { base: 1 },
|
||||
}).then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly passes in the rootValue', () => {
|
||||
const query = `{ testRootValue }`;
|
||||
const expected = { testRootValue: 'it also works' };
|
||||
return runQuery({ schema: Schema, query: query, rootValue: 'it also' })
|
||||
.then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly passes in the context', () => {
|
||||
const query = `{ testContextValue }`;
|
||||
const expected = { testContextValue: 'it still works' };
|
||||
return runQuery({ schema: Schema, query: query, context: 'it still' })
|
||||
.then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly passes in variables (and arguments)', () => {
|
||||
// XXX can be removed after tests are actually writen
|
||||
const query = `query TestVar($base: Int!){ testArgumentValue(base: $base) }`;
|
||||
const expected = { testArgumentValue: 6 };
|
||||
return runQuery({
|
||||
schema: Schema,
|
||||
query: query,
|
||||
variables: { base: 1 },
|
||||
}).then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('runs the correct operation when operationName is specified', () => {
|
||||
const query = `
|
||||
query Q1 {
|
||||
testString
|
||||
}
|
||||
query Q2 {
|
||||
testRootValue
|
||||
}`;
|
||||
const expected = {
|
||||
testString: 'it works',
|
||||
};
|
||||
return runQuery({ schema: Schema, query: query, operationName: 'Q1' })
|
||||
.then((res) => {
|
||||
return expect(res.data).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -27,6 +27,7 @@ function runQuery({
|
|||
variables?: { [key: string]: any },
|
||||
operationName?: string,
|
||||
//logFunction?: function => void
|
||||
//validationRules?: No, too risky. If you want extra validation rules, then parse it yourself.
|
||||
}): Promise<GraphQLResult> {
|
||||
let documentAST: Document;
|
||||
|
||||
|
@ -40,7 +41,7 @@ function runQuery({
|
|||
}
|
||||
|
||||
// validate
|
||||
const validationErrors = validate(schema, documentAST, []);
|
||||
const validationErrors = validate(schema, documentAST);
|
||||
if (validationErrors.length) {
|
||||
return Promise.resolve({ errors: validationErrors });
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue