Skip to content

Commit

Permalink
bump versions and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Dec 6, 2019
1 parent 2bf265a commit 7e09ddb
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 142 deletions.
63 changes: 59 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ const UPDATE_USER = `
}
`;

const mutationResult = await mutate(
UPDATE_USER,
{ variables: { id: 1, email: 'nancy@foo.co' } }
);
const mutationResult = await mutate(UPDATE_USER, {
variables: { id: 1, email: 'nancy@foo.co' }
});

expect(mutationResult).toEqual({
data: {
Expand Down Expand Up @@ -85,6 +84,62 @@ This is useful when your apollo server `context` option is a callback that opera

As mentioned above, if you don't pass an `extendMockRequest` to `createTestClient`, we provide a default request mock object for you. See https://github.com/howardabrams/node-mocks-http#createrequest for all the default values that are included in that mock.

### setOptions

You can also set the `request` and `response` mocking options **after** the creation of the `test client`, which is a **cleaner** and **faster** way due not needing to create a new instance for **any** change you might want to do the `request` or `response`.

```js
const { query, setOptions } = createTestClient({
apolloServer
});

setOptions({
// If "request" or "response" is not specified, it's not modified
request: {
headers: {
cookie: 'csrf=blablabla',
referer: ''
}
},
response: {
locals: {
user: {
isAuthenticated: false
}
}
}
});
```

### Type Safety

If you are using [TypeScript](https://www.typescriptlang.org/) you can specify the types you expect from the **queries** / **mutations** `data` and / or `variables` you need to specify (_if any_).

> You can also use [**graphql-tag-ts**](https://www.npmjs.com/package/graphql-tag-ts) for even better type safety
```typescript
import gql from 'graphql-tag';

const { query } = createTestClient({
apolloServer
});

const {
data: { helloWorld }
} = query<{ data: { helloWorld: string } }, { hello: string }>(
gql`
query($hello: String!) {
helloWorld(hello: $hello)
}
`,
{
hello: 'world' // type checked
}
);

helloWorld; // string
```

## Why not use `apollo-server-testing`?

You can't really write _real_ integration tests with `apollo-server-testing`, because it doesn't support servers which rely on the `context` option being a function that uses the `req` object ([see this issue for more information](https://github.com/apollographql/apollo-server/issues/2277)).
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"dev": "tsc --watch"
},
"dependencies": {
"apollo-server-core": "^2.8.1",
"apollo-server-express": "^2.8.1",
"apollo-server-core": "^2.9.13",
"apollo-server-express": "^2.9.13",
"express": "^4.17.1",
"node-mocks-http": "^1.7.6",
"typescript": "^3.5.3"
"node-mocks-http": "^1.8.0"
},
"devDependencies": {
"typescript": "^3.7.3"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
Expand Down
Loading

0 comments on commit 7e09ddb

Please sign in to comment.