-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow GraphQLSchema instances to be passed to handlers (#49)
* Allow GraphQLSchema instances to be passed to handlers * never mind tags in test
- Loading branch information
Showing
3 changed files
with
95 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,92 @@ | ||
import { composeCreateGraphQLHandler } from 'ember-cli-mirage-graphql/handler'; | ||
import { makeExecutableSchema, mergeSchemas } from 'graphql-tools'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | handler', function() { | ||
test('it parses the request, creates mocks and resolvers for schema and returns graphQL', function(assert) { | ||
assert.expect(8); | ||
|
||
let db = {}; | ||
let options = {}; | ||
let query = {}; | ||
let rawSchema = {}; | ||
let schema = {}; | ||
let variables = {}; | ||
let addResolvers = (_schema) => | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
let parseRequest = () => ({ query, variables }); | ||
|
||
function addMocks(_schema, _db, _options) { | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
assert.equal(_db, db, 'It received the db'); | ||
assert.equal(_options, options, 'It received the options'); | ||
} | ||
|
||
function createSchema(_rawSchema) { | ||
assert.equal(_rawSchema, rawSchema, 'It received the raw schema'); | ||
return schema; | ||
} | ||
|
||
function graphQL(_schema, _query, _, __, vars) { | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
assert.equal(_query, query, 'It received the query'); | ||
assert.equal(vars, variables, 'It received the vars'); | ||
} | ||
|
||
let createHandler = composeCreateGraphQLHandler(parseRequest, createSchema, | ||
addMocks, addResolvers, graphQL); | ||
let handler = createHandler(rawSchema, options); | ||
|
||
handler({ db }, { request: {} }); | ||
const db = {}; | ||
const options = {}; | ||
const query = {}; | ||
const variables = {}; | ||
|
||
const composeAddResolvers = (schema, assert) => (_schema) => | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
|
||
const parseRequest = () => ({ query, variables }); | ||
|
||
const composeAddMocks = (schema, assert) => (_schema, _db, _options) => { | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
assert.equal(_db, db, 'It received the db'); | ||
assert.equal(_options, options, 'It received the options'); | ||
}; | ||
|
||
const composeGraphQL = (schema, assert) => (_schema, _query, _, __, vars) => { | ||
assert.equal(_schema, schema, 'It received the schema'); | ||
assert.equal(_query, query, 'It received the query'); | ||
assert.equal(vars, variables, 'It received the vars'); | ||
} | ||
|
||
const composeAdditionalCallbacks = (schema, assert) => [ | ||
composeAddMocks(schema, assert), | ||
composeAddResolvers(schema, assert), | ||
composeGraphQL(schema, assert) | ||
]; | ||
|
||
const composeCreateHandler = (createSchema, schema, assert) => | ||
composeCreateGraphQLHandler( | ||
parseRequest, | ||
createSchema, | ||
...composeAdditionalCallbacks(schema, assert)); | ||
|
||
module('raw schema', function() { | ||
test('it parses the request, creates mocks and resolvers for schema and returns GraphQL', function(assert) { | ||
assert.expect(8); | ||
|
||
const schema = {}; | ||
|
||
function createSchema(_schema) { | ||
assert.equal(_schema, schema, 'It received the raw schema'); | ||
return schema; | ||
} | ||
|
||
const createHandler = composeCreateHandler(createSchema, schema, assert); | ||
const handler = createHandler(schema, options); | ||
|
||
handler({ db }, { request: {} }); | ||
}); | ||
}); | ||
|
||
module('merged schema', function() { | ||
test('it parses the request, creates mocks and resolvers for schema and returns GraphQL', function(assert) { | ||
assert.expect(7); | ||
|
||
const fooSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type Foo { | ||
name: String | ||
} | ||
type Query { | ||
foos: [Foo] | ||
} | ||
` | ||
}); | ||
const barSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type Bar { | ||
name: String | ||
} | ||
type Query { | ||
bars: [Bar] | ||
} | ||
` | ||
}); | ||
const createSchema = () => assert.equal(0, 1); // This should not be called | ||
const schema = mergeSchemas({ schemas: [fooSchema, barSchema] }); | ||
const createHandler = composeCreateHandler(createSchema, schema, assert); | ||
const handler = createHandler(schema, options); | ||
|
||
handler({ db }, { request: {} }); | ||
}); | ||
}) | ||
}); |