Skip to content

Commit

Permalink
refactor: due changes in graphql-compose@6.0.1
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Using API from graphql-compose@6.0.0
  • Loading branch information
nodkz committed Mar 15, 2019
1 parent 8a1fec2 commit 4741378
Show file tree
Hide file tree
Showing 14 changed files with 1,981 additions and 1,797 deletions.
10 changes: 10 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"line-length": false,
"no-trailing-punctuation": {
"punctuation": ",;"
},
"no-inline-html": false,
"ol-prefix": false,
"first-line-h1": false,
"first-heading-h1": false
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"prettier.eslintIntegration": true,
"eslint.validate": [
"javascript",
],
"javascript.validate.enable": false
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ Modules `graphql` and `graphql-compose` are in `peerDependencies`, so should be

Example
=======
`TypeComposer` is a [graphql-compose](https://github.com/graphql-compose/graphql-compose) utility, that wraps GraphQL types and provide bunch of useful methods for type manipulation.
`ObjectTypeComposer` is a [graphql-compose](https://github.com/graphql-compose/graphql-compose) utility, that wraps GraphQL types and provide bunch of useful methods for type manipulation.
```js
import composeWithRelay from 'graphql-compose-relay';
import { TypeComposer } from 'graphql-compose';
import { ObjectTypeComposer } from 'graphql-compose';
import { RootQueryType, UserType } from './my-graphq-object-types';

const rootQueryTypeComposer = new TypeComposer(RootQueryType);
const userTypeComposer = new TypeComposer(UserType);
const queryTC = new ObjectTypeComposer(RootQueryType);
const userTC = new ObjectTypeComposer(UserType);

// If passed RootQuery, then will be added only `node` field to this type.
// Via RootQuery.node you may find objects by globally unique ID among all types.
composeWithRelay(rootQueryTypeComposer);
composeWithRelay(queryTC);

// Other types, like User, will be wrapped with middlewares that:
// - add relay's id field. Field will be added or wrapped to return Relay's globally unique ID.
// - for mutations will be added clientMutationId to input and output objects types
// - this type will be added to NodeInterface for resolving via RootQuery.node
composeWithRelay(userTypeComposer);
composeWithRelay(userTC);
```
That's all!

Expand All @@ -55,7 +55,7 @@ All this annoying operations is too fatigue to do by hands. So this middleware d

Requirements
============
Method `composeWithRelay` accept `TypeComposer` as input argument. So `TypeComposer` should meet following requirements:
Method `composeWithRelay` accept `ObjectTypeComposer` as input argument. So `ObjectTypeComposer` should meet following requirements:
- has defined `recordIdFn` (function that from object of this type, returns you id for the globalId construction)
- should have `findById` resolver (that will be used by `RootQuery.node`)

Expand Down
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@
},
"homepage": "https://github.com/graphql-compose/graphql-compose-relay",
"peerDependencies": {
"graphql-compose": ">=5.0.1 || >=4.0.0"
"graphql-compose": ">=6.0.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
"@babel/plugin-transform-flow-strip-types": "^7.3.4",
"@babel/plugin-transform-runtime": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-flow": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.4.2",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.5.0",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^5.5.0",
"eslint": "^5.15.2",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-flowtype": "^2.50.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-prettier": "^2.6.2",
"flow-bin": "^0.80.0",
"graphql": "14.0.0",
"graphql-compose": "^5.0.1",
"jest": "^23.5.0",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"semantic-release": "^15.9.12"
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-flowtype": "^3.4.2",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"flow-bin": "^0.95.1",
"graphql": "14.1.1",
"graphql-compose": "^6.0.1",
"jest": "^24.5.0",
"prettier": "^1.16.4",
"rimraf": "^2.6.3",
"semantic-release": "^15.13.3"
},
"config": {
"commitizen": {
Expand All @@ -74,7 +74,7 @@
"flow": "./node_modules/.bin/flow",
"test": "npm run coverage && npm run lint && npm run flow",
"link": "yarn build && yarn link graphql-compose && yarn link",
"unlink": "yarn unlink graphql-compose && yarn add graphql-compose",
"unlink": "rimraf node_modules && yarn install",
"semantic-release": "semantic-release"
}
}
10 changes: 0 additions & 10 deletions src/__mocks__/rootMutationTypeComposer.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/__mocks__/rootQueryTypeComposer.js

This file was deleted.

22 changes: 11 additions & 11 deletions src/__mocks__/userTypeComposer.js → src/__mocks__/userTC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import { TypeComposer, Resolver } from 'graphql-compose';
import { schemaComposer } from 'graphql-compose';
import {
GraphQLString,
GraphQLObjectType,
Expand All @@ -24,10 +24,10 @@ export const UserType = new GraphQLObjectType({
},
});

export const userTypeComposer = new TypeComposer(UserType);
userTypeComposer.setRecordIdFn(obj => obj.id);
export const userTC = schemaComposer.createObjectTC(UserType);
userTC.setRecordIdFn(obj => obj.id);

export const findByIdResolver = new Resolver({
export const findByIdResolver = schemaComposer.createResolver({
name: 'findById',
kind: 'query',
type: UserType,
Expand Down Expand Up @@ -56,9 +56,9 @@ export const findByIdResolver = new Resolver({
return Promise.resolve(null);
},
});
userTypeComposer.setResolver('findById', findByIdResolver);
userTC.setResolver('findById', findByIdResolver);

export const createOneResolver = new Resolver({
export const createOneResolver = schemaComposer.createResolver({
name: 'createOne',
kind: 'mutation',
type: new GraphQLObjectType({
Expand Down Expand Up @@ -89,9 +89,9 @@ export const createOneResolver = new Resolver({
});
},
});
userTypeComposer.setResolver('createOne', createOneResolver);
userTC.setResolver('createOne', createOneResolver);

export const manyArgsWithInputResolver = new Resolver({
export const manyArgsWithInputResolver = schemaComposer.createResolver({
name: 'manyArgsWithInput',
kind: 'mutation',
type: new GraphQLObjectType({
Expand Down Expand Up @@ -130,9 +130,9 @@ export const manyArgsWithInputResolver = new Resolver({
});
},
});
userTypeComposer.setResolver('manyArgsWithInput', manyArgsWithInputResolver);
userTC.setResolver('manyArgsWithInput', manyArgsWithInputResolver);

export const manyArgsWithoutInputResolver = new Resolver({
export const manyArgsWithoutInputResolver = schemaComposer.createResolver({
name: 'manyArgsWithoutInput',
kind: 'mutation',
type: new GraphQLObjectType({
Expand Down Expand Up @@ -160,4 +160,4 @@ export const manyArgsWithoutInputResolver = new Resolver({
});
},
});
userTypeComposer.setResolver('manyArgsWithoutInput', manyArgsWithoutInputResolver);
userTC.setResolver('manyArgsWithoutInput', manyArgsWithoutInputResolver);
42 changes: 20 additions & 22 deletions src/__tests__/composeWithRelay-test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
/* @flow */

import { TypeComposer } from 'graphql-compose';
import { ObjectTypeComposer, schemaComposer } from 'graphql-compose';
import {
GraphQLInterfaceType,
GraphQLSchema,
GraphQLNonNull,
graphql,
} from 'graphql-compose/lib/graphql';
import { composeWithRelay } from '../composeWithRelay';
import { userTypeComposer } from '../__mocks__/userTypeComposer';
import { rootQueryTypeComposer } from '../__mocks__/rootQueryTypeComposer';
import { rootMutationTypeComposer } from '../__mocks__/rootMutationTypeComposer';
import { userTC } from '../__mocks__/userTC';
import { toGlobalId } from '../globalId';

describe('composeWithRelay', () => {
const userComposer = composeWithRelay(userTypeComposer);
const rootQueryComposer = composeWithRelay(rootQueryTypeComposer);
const rootMutationComposer = composeWithRelay(rootMutationTypeComposer);
const userComposer = composeWithRelay(userTC);
const queryTC = composeWithRelay(schemaComposer.Query);
const mutationTC = composeWithRelay(schemaComposer.Mutation);

describe('basic checks', () => {
it('should return TypeComposer', () => {
expect(userComposer).toBeInstanceOf(TypeComposer);
it('should return ObjectTypeComposer', () => {
expect(userComposer).toBeInstanceOf(ObjectTypeComposer);
});

it('should throw error if got a not TypeComposer', () => {
it('should throw error if got a not ObjectTypeComposer', () => {
expect(() => composeWithRelay((123: any))).toThrowError(
'should provide TypeComposer instance'
'should provide ObjectTypeComposer instance'
);
});

it('should throw error if TypeComposer without recordIdFn', () => {
const tc = userTypeComposer.clone('AnotherUserType2');
it('should throw error if ObjectTypeComposer without recordIdFn', () => {
const tc = userTC.clone('AnotherUserType2');
delete tc.gqType._gqcGetRecordIdFn;
expect(() => composeWithRelay(tc)).toThrowError('should have recordIdFn');
});

it('should thow error if typeComposer does not have findById resolver', () => {
const tc = userTypeComposer.clone('AnotherUserType');
const tc = userTC.clone('AnotherUserType');
tc.removeResolver('findById');
expect(() => composeWithRelay(tc)).toThrowError(
"does not have resolver with name 'findById'"
Expand All @@ -46,7 +44,7 @@ describe('composeWithRelay', () => {

describe('when pass RootQuery type composer', () => {
it('should add `node` field to RootQuery', () => {
const nodeField: any = rootQueryComposer.getField('node');
const nodeField: any = queryTC.getField('node');
expect(nodeField.type).toBeInstanceOf(GraphQLInterfaceType);
expect(nodeField.type.name).toBe('Node');
});
Expand All @@ -64,9 +62,9 @@ describe('composeWithRelay', () => {
});

it('should resolve globalId in `user.id` field', async () => {
rootQueryTypeComposer.setField('user', userTypeComposer.getResolver('findById'));
queryTC.setField('user', userTC.getResolver('findById'));
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
query: queryTC.getType(),
});
const query = `{
user(_id: 1) {
Expand All @@ -80,9 +78,9 @@ describe('composeWithRelay', () => {
});

it('should resolve globalId in `node.id` field', async () => {
rootQueryTypeComposer.setField('user', userTypeComposer.getResolver('findById'));
queryTC.setField('user', userTC.getResolver('findById'));
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
query: queryTC.getType(),
});
const query = `{
node(id: "${toGlobalId('User', 1)}") {
Expand All @@ -99,10 +97,10 @@ describe('composeWithRelay', () => {
});

it('should passthru clientMutationId in mutations', async () => {
rootMutationComposer.setField('createUser', userTypeComposer.getResolver('createOne'));
mutationTC.setField('createUser', userTC.getResolver('createOne'));
const schema = new GraphQLSchema({
query: rootQueryTypeComposer.getType(),
mutation: rootMutationComposer.getType(),
query: queryTC.getType(),
mutation: mutationTC.getType(),
});
const query = `mutation {
createUser(input: { name: "Ok", clientMutationId: "123" }) {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/nodeFieldConfig-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* @flow */

import { GraphQLInterfaceType, GraphQLNonNull } from 'graphql-compose/lib/graphql';
import { findByIdResolver, userTypeComposer } from '../__mocks__/userTypeComposer';
import { findByIdResolver, userTC } from '../__mocks__/userTC';
import { toGlobalId } from '../globalId';
import { getNodeFieldConfig } from '../nodeFieldConfig';

describe('nodeFieldConfig', () => {
const typeToFindByIdMap = {
User: {
resolver: findByIdResolver,
tc: userTypeComposer,
tc: userTC,
},
};
const config = getNodeFieldConfig(typeToFindByIdMap);
Expand Down
22 changes: 11 additions & 11 deletions src/__tests__/wrapMutationResolver.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/* @flow */

import { TypeComposer, InputTypeComposer } from 'graphql-compose';
import { schemaComposer } from 'graphql-compose';
import {
GraphQLString,
GraphQLID,
GraphQLNonNull,
getNamedType,
} from 'graphql-compose/lib/graphql';
import { composeWithRelay } from '../composeWithRelay';
import { userTypeComposer } from '../__mocks__/userTypeComposer';
import { userTC } from '../__mocks__/userTC';
import { toGlobalId } from '../globalId';

describe('wrapMutationResolver', () => {
composeWithRelay(userTypeComposer);
const fieldConfig: any = userTypeComposer.getResolver('createOne').getFieldConfig();
const fieldConfigManyArgsWithInput: any = userTypeComposer
composeWithRelay(userTC);
const fieldConfig: any = userTC.getResolver('createOne').getFieldConfig();
const fieldConfigManyArgsWithInput: any = userTC
.getResolver('manyArgsWithInput')
.getFieldConfig();
const fieldConfigManyArgsWithoutInput: any = userTypeComposer
const fieldConfigManyArgsWithoutInput: any = userTC
.getResolver('manyArgsWithoutInput')
.getFieldConfig();

describe('args', () => {
it('should add `clientMutationId` field to args.input', () => {
const itc = new InputTypeComposer(fieldConfig.args.input.type);
const itc = schemaComposer.createInputTC(fieldConfig.args.input.type);
expect(itc.hasField('clientMutationId')).toBe(true);
expect(itc.getFieldType('clientMutationId')).toBe(GraphQLString);
});
Expand All @@ -40,7 +40,7 @@ describe('wrapMutationResolver', () => {
);

const type: any = getNamedType(fieldConfigManyArgsWithoutInput.args.input.type);
const itc = new InputTypeComposer(type);
const itc = schemaComposer.createInputTC(type);
expect(itc.hasField('sort')).toBe(true);
expect(itc.hasField('limit')).toBe(true);
expect(itc.hasField('clientMutationId')).toBe(true);
Expand All @@ -52,7 +52,7 @@ describe('wrapMutationResolver', () => {
);

const type: any = getNamedType(fieldConfigManyArgsWithInput.args.input.type);
const itc = new InputTypeComposer(type);
const itc = schemaComposer.createInputTC(type);
expect(itc.hasField('sort')).toBe(false);
expect(itc.hasField('limit')).toBe(false);
expect(itc.hasField('clientMutationId')).toBe(true);
Expand All @@ -61,13 +61,13 @@ describe('wrapMutationResolver', () => {

describe('outputType', () => {
it('should add `clientMutationId` field to payload', () => {
const tc = new TypeComposer(fieldConfig.type);
const tc = schemaComposer.createObjectTC(fieldConfig.type);
expect(tc.hasField('clientMutationId')).toBe(true);
expect(tc.getFieldType('clientMutationId')).toBe(GraphQLString);
});

it('should add `nodeId` field to payload', () => {
const tc = new TypeComposer(fieldConfig.type);
const tc = schemaComposer.createObjectTC(fieldConfig.type);
expect(tc.hasField('nodeId')).toBe(true);
expect(tc.getFieldType('nodeId')).toBe(GraphQLID);
});
Expand Down
Loading

0 comments on commit 4741378

Please sign in to comment.