Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ install:
script:
- npm run standard
- npm test
after_success:
- npm run coveralls
124 changes: 44 additions & 80 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,89 @@
<br>
</h1>

Easy GraphQL mock is a node library used to create mocks of the schema. It will create
mocks of all the types that are on the GraphQL Schema, nested types are supported.
[![Coverage Status](https://coveralls.io/repos/github/EasyGraphQL/easygraphql-mock/badge.svg?branch=master)](https://coveralls.io/github/EasyGraphQL/easygraphql-mock?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/EasyGraphQL/easygraphql-mock.svg)](https://greenkeeper.io/)

## Installation
`easygraphql-mock` is a node library used to create mocks of a schema.
It will create mocks of all the types that are on the GraphQL Schema, including the nested types.

[![Greenkeeper badge](https://badges.greenkeeper.io/EasyGraphQL/easygraphql-mock.svg)](https://greenkeeper.io/)
## Installation

```bash
To install the package on your project just run on the root of your project
```shell
$ npm install easygraphql-mock --save
```

## Usage
To get started with the mocks, you might need to follow the next steps:

### Basic
```js
const fs = require('fs')
const path = require('path')
const mocker = require('easygraphql-mock')
// Read the `graphql` file with the schema
const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')

const mock = mocker(schemaCode)
## How to use it?

// All the types that are on the file are going to be created with a mock
console.log(mock.Me)
```
+ Import [`easygraphql-mock`](https://github.com/EasyGraphQL/easygraphql-mock) package.
+ Read the schema.
+ Initialize the mock, and pass the schema as the first argument.
+ If there are multiples schemas pass an array with the schemas an argument.
+ **Note**: In order to use multiples schema files, the queries and mutations must be extended.
+ The second argument is optional and it is going to be your custom schema, in case you want to pass it.

### With definitions
You can set some values to the fields that you want on the schema. To do that, you might pass
an object to `easygqlmock` as a second argument. It should have the name of the type and the
field that you want to set.
*In case you have a custom scalar, set it on the second argument, if it's not set it will be {}*

### One schema file
```js
'use strict'

const easygraphqlMock = require('easygraphql-mock')
const fs = require('fs')
const path = require('path')
const mocker = require('easygraphql-mock')
// Read the `graphql` file with the schema
const schemaCode = fs.readFileSync(path.join(__dirname, 'schema', 'schema.gql'), 'utf8')

const mock = mocker(schemaCode, {
Family: {
name: 'Super test 1',
ages: [10],
familyRelation: 'Mother',
familyRelationArr: ['Mother', 'Brother']
}
})
const userSchema = fs.readFileSync(path.join(__dirname, 'schema', 'user.gql'), 'utf8')

// All the types that are on the file are going to be created with a mock
console.log(mock.Family)
console.log(mock.Family.name) // 'Super test 1'
const mockedSchema = easygraphqlMock(userSchema)
```

## Use multiple Schema files
You can pass multiple schema files, just be shure that you extend the queries and mutations and
when you're creating the mock pass an array with the files:


### Multiples schemas files
```js
'use strict'

const easygraphqlMock = require('easygraphql-mock')
const fs = require('fs')
const path = require('path')
const mocker = require('easygraphql-mock')
// Read the `graphql` file with the schema
const studentSchema = fs.readFileSync(path.join(__dirname, 'schema', 'student.gql'), 'utf8')
const schoolSchema = fs.readFileSync(path.join(__dirname, 'schema', 'school.gql'), 'utf8')

const mock = mocker([studentSchema, schoolSchema])
const userSchema = fs.readFileSync(path.join(__dirname, 'schema', 'user.gql'), 'utf8')
const familySchema = fs.readFileSync(path.join(__dirname, 'schema', 'family.gql'), 'utf8')

// All the types that are on the file are going to be created with a mock
console.log(mock.Student)
console.log(mock.School)
const mockedSchema = easygraphqlMock([userSchema, familySchema])
```

## Result
### Custom schema
You can set some values to the fields that you want on the schema. To do that, you might pass an object as a second argument.
It must have the same name of the type and the field that you want to set.

If the `gql` schema has:
```graphql
enum FamilyRelation {
Father
Mother
Brother
}
```js
'use strict'

type Family {
name: String!
ages: [Int]!
user: User!
familyRelation: FamilyRelation!
familyRelationArr: [FamilyRelation]!
}
const easygraphqlMock = require('easygraphql-mock')
const fs = require('fs')
const path = require('path')

type User {
email: String!
username: String!
fullName: String!
phone: String!
family: Family!
}
```
const userSchema = fs.readFileSync(path.join(__dirname, 'schema', 'user.gql'), 'utf8')
const familySchema = fs.readFileSync(path.join(__dirname, 'schema', 'family.gql'), 'utf8')

Run `mocker`
```js
const mock = mocker(schemaCode, {
const mockedSchema = easygraphqlMock([userSchema, familySchema], {
CustomScalarDate: '2018-10-10',
Family: {
name: 'Super test 1',
ages: [10],
familyRelation: 'Mother',
familyRelationArr: ['Mother', 'Brother']
}
})
console.log(mock.Family)
```

And something like this will be the result for `mock.Family`
### Result
Here is the result of `mockedSchema.Family`

```js
{
name: 'Super test 1',
ages: [ 10 ],
createdAt: '2018-10-10',
user: {
email: 'ulalilid@herem.gl',
username: 'tNfwN',
Expand All @@ -138,7 +102,7 @@ And something like this will be the result for `mock.Family`
username: tNfwN',
fullName: 'Nathan Lewis',
phone: '(231) 616-1744',
family: '...'
family: ...
},
familyRelation: 'Mother',
familyRelationArr: [ 'Mother', 'Brother' ]
Expand Down
Loading