Skip to content

Commit

Permalink
Allow GraphQLSchema instances to be passed to handlers (#49)
Browse files Browse the repository at this point in the history
* Allow GraphQLSchema instances to be passed to handlers

* never mind  tags in test
  • Loading branch information
jneurock authored Jun 2, 2020
1 parent a3add58 commit cc29865
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 39 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import schema from 'app-name/path-to-your-schema';
export default createGraphQLHandler(schema, /* options = {} */);
```

Note: We use a simple command line tool to download our schema: [get-graphql-schema](https://www.npmjs.com/package/get-graphql-schema).
Notes:
* We use a simple command line tool to download our schema: [get-graphql-schema](https://www.npmjs.com/package/get-graphql-schema).
* You can import and pass in your raw schema directly, as shown above, or you can pass in an instance of a `GraphQLSchema`, e.g., a case where you might merge multiple schemas.

---

Expand Down
9 changes: 6 additions & 3 deletions addon/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import {
createSchema
} from './schema';
import { deprecate } from '@ember/debug';
import { graphql } from 'graphql';
import { GraphQLSchema, graphql } from 'graphql';

export const composeCreateGraphQLHandler =
(parseRequest, createSchema, addMocksToSchema, addInterfaceTypeResolversToSchema, graphql) =>
(rawSchema, options) =>
(schema, options) =>
({ db }, request) => {
let { query, variables } = parseRequest(request.requestBody);
let schema = createSchema(rawSchema);

if (!(schema instanceof GraphQLSchema)) {
schema = createSchema(schema);
}

if (options && options.varsMap) {
deprecate('ember-cli-mirage-graphql varsMap is deprecated, please use argsMap instead', false, {
Expand Down
121 changes: 86 additions & 35 deletions tests/unit/handler-test.js
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: {} });
});
})
});

0 comments on commit cc29865

Please sign in to comment.