npm install think-apollo-graphql
Set the extend in src/config/extend.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:
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.
{
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.
// 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);
}
}
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}
}
{
"data": {
"books": {
"title": "Harry Potter and the Sorcerer's stone"
}
}
}