Skip to content

Commit

Permalink
Added JSON scalar type (ardatan/graphql-tools#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
oricordeau committed Oct 26, 2016
1 parent a4d111b commit bb4ccf9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"express": "^4.13.4",
"graphql": "^0.7.0",
"graphql-subscriptions": "^0.2.0",
"graphql-type-json": "^0.1.3",
"istanbul": "^0.4.5",
"mocha": "^3.0.1",
"remap-istanbul": "^0.7.0",
Expand Down
5 changes: 5 additions & 0 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals
defaultMockMap.set('String', () => 'Hello World');
defaultMockMap.set('Boolean', () => Math.random() > 0.5);
defaultMockMap.set('ID', () => uuid.v4());
defaultMockMap.set('JSON', () => ({
kind: 'ObjectValue',
value: '{"foo":"bar"}',
name: 'JSON',
}));

function mergeObjects(a: Object, b: Object) {
return Object.assign(a, b);
Expand Down
8 changes: 8 additions & 0 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ function addResolveFunctionsToSchema(schema: GraphQLSchema, resolveFunctions: IR
return;
}

if (typeName === 'JSON') {
const propsToCopy = ['description', 'serialize', 'parseValue', 'parseLiteral', '_scalarConfig'];
propsToCopy.forEach(function(propName) {
type[propName] = resolveFunctions['JSON'][propName];
});
return;
}

const fields = getFieldsForType(type);
if (!fields) {
throw new SchemaError(
Expand Down
41 changes: 41 additions & 0 deletions src/test/testMocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import {
addResolveFunctionsToSchema,
makeExecutableSchema,
} from '../schemaGenerator';
const GraphQLJSON = require('graphql-type-json');
import 'mocha';

describe('Mock', () => {
const shorthand = `
scalar MissingMockType
scalar JSON
interface Flying {
returnInt: Int
}
Expand Down Expand Up @@ -49,6 +52,7 @@ describe('Mock', () => {
returnBoolean: Boolean
returnID: ID
returnEnum: SomeEnum
returnJSON: JSON
returnBirdsAndBees: [BirdsAndBees]
returnFlying: [Flying]
returnMockError: MissingMockType
Expand Down Expand Up @@ -83,6 +87,7 @@ describe('Mock', () => {
return info.schema.getType(data.typename);
},
},
JSON: GraphQLJSON,
};

it('throws an error if you forget to pass schema', () => {
Expand Down Expand Up @@ -123,6 +128,22 @@ describe('Mock', () => {
});
});

it('mocks the JSON type', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
addResolveFunctionsToSchema(jsSchema, resolveFunctions);
const mockMap = {};
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap});
const testQuery = `{
returnJSON
}`;
return graphql(jsSchema, testQuery).then((res) => {
//console.log(res);
expect(res.data.returnJSON).to.be.a('object');
let resultObj = JSON.parse(res.data.returnJSON.value);
expect(resultObj.foo).to.equal('bar');
});
});

it('lets you use mockServer for convenience', () => {
const testQuery = `{
returnInt
Expand Down Expand Up @@ -526,6 +547,26 @@ describe('Mock', () => {
});
});

it('can mock the JSON type', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
addResolveFunctionsToSchema(jsSchema, resolveFunctions);
const mockMap = { JSON: () => ({
kind: 'ObjectValue',
value: '{"hello":"world"}',
name: 'JSON',
})};
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap });
const testQuery = `{
returnJSON
}`;
return graphql(jsSchema, testQuery).then((res) => {
//console.log(res);
expect(res.data.returnJSON).to.be.a('object');
let resultObj = JSON.parse(res.data.returnJSON.value);
expect(resultObj.hello).to.equal('world');
});
});

it('can mock a list of ints', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
const mockMap = { Int: () => 123 };
Expand Down
21 changes: 21 additions & 0 deletions src/test/testSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GraphQLResolveInfo,
} from 'graphql';
// import { printSchema } from 'graphql';
const GraphQLJSON = require('graphql-type-json');
import { Logger } from '../Logger';
import TypeA from './circularSchemaA';
import {
Expand Down Expand Up @@ -282,6 +283,26 @@ describe('generating schema from shorthand', () => {
expect(jsSchema.getQueryType().getFields()).to.have.all.keys('foo', 'bar');
});

it('can generate a schema which includes a JSON scalar type', () => {
const typeDefAry = [`
scalar JSON
`, `
type Query {
foo: JSON
}
`, `
schema {
query: Query
}
`];
const resolvers = {
JSON: GraphQLJSON,
};
const jsSchema = makeExecutableSchema({ typeDefs: typeDefAry, resolvers: resolvers});
//console.log(jsSchema);
expect(jsSchema.getQueryType().name).to.equal('Query');
});

it('properly deduplicates the array of type definitions', () => {
const typeDefAry = [`
type Query {
Expand Down

0 comments on commit bb4ccf9

Please sign in to comment.