https://github.com/thinkjs/think-apollo-graphql
Graphql extend for ThinkJS
https://github.com/thinkjs/think-apollo-graphql
Last synced: about 1 year ago
JSON representation
Graphql extend for ThinkJS
- Host: GitHub
- URL: https://github.com/thinkjs/think-apollo-graphql
- Owner: thinkjs
- License: mit
- Created: 2018-02-27T07:53:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-01T02:30:54.000Z (over 8 years ago)
- Last Synced: 2024-04-26T01:01:32.512Z (about 2 years ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 3
- Watchers: 13
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# think-apollo-graphql
[](https://www.npmjs.com/package/think-apollo-graphql)
## Install
```
npm install think-apollo-graphql
```
## How to use
Set the extend in `src/config/extend.js`
```js
const graphql = require('think-apollo-graphql');
module.exports = [
graphql
];
```
And then the `think`, `controller` will have the method `thinkGraphql`. For
example in `controller` you can use like this:
```js
module.exports = class extends think.Controller {
constructor(...props) {
super(...props);
}
async indexAction() {
const graphqlResult = await this.thinkGraphql(graphqlOptions);
return this.json(graphqlResult);
}
}
```
Note: `think.thinkGraphql(graphqlOptions, ctx)`, think.thinkGraphql expects exactly ctx.
#### graphqlOptions like:
```js
{
schema: the GraphQLSchema to be used
context: the context value passed to resolvers during GraphQL execution
rootValue: the value passed to the first resolve function
formatError: a function to apply to every error before sending the response to clients
validationRules: additional GraphQL validation rules to be applied to client-specified queries
formatParams: a function applied for each query in a batch to format parameters before execution
formatResponse: a function applied to each response after execution
tracing: when set to true, collect and expose trace data in the Apollo Tracing format
}
```
More doc at [apollo-server](https://github.com/apollographql/apollo-server).
## Demo
### Server
```js
// src/controller/graphql.js
const { makeExecutableSchema } = require('graphql-tools');
module.exports = class extends think.Controller {
constructor(...props) {
super(...props);
}
async indexAction() {
// Some fake data
const books = [
{
id: 1,
title: `Harry Potter and the Sorcerer's stone`,
author: 'J.K. Rowling',
},
{
id: 2,
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
// The GraphQL schema in string form
const typeDefs = `
type Query {
books(id: Int): Book
}
type Book {
id: Int,
title: String,
author: String
}
`;
// The resolvers
const resolvers = {
Query: {
books: (_, arg) => {
return books.filter((ele) => {
return ele.id === +arg.id;
})[0];
}
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
const graphqlResult = await this.thinkGraphql({
schema
});
return this.json(graphqlResult);
}
}
```
### Client
```
POST /graphql2 HTTP/1.1
Host: 127.0.0.1:8360
Content-Type: application/json
{
"query": "query getBookById($id: Int){books(id: $id) {title}}",
"variables": {"id": 1}
}
```
### Result
```
{
"data": {
"books": {
"title": "Harry Potter and the Sorcerer's stone"
}
}
}
```