是否可以使用 Apollo Server 在服务器端本地执行突变或查询
Is it possible to execute a mutation or query locally at server side with Apollo Server
我想知道是否可以使用 Apollo Server 在服务器端本地执行突变或查询。
示例:
1- 到达端点 GET /createNew
2- 我 运行 我定义的突变:
apolloserver.mutation(mutation_query).then((response)=>{res.status(200)})
3- 一个新条目被添加到数据库并且一个 JSON 字符串返回给客户端
有这样的东西吗? apolloserver
对象在 运行 时可用吗?
我正在使用 NodeJS + Express
我成功了
首先我们启动对象:
import express from 'express';
import { graphql } from 'graphql';
const context = require('./context'); //this is the object to be passed to each resolver function
const schema = require('./schema'); //this is the object returned by makeExecutableSchema({typeDefs, resolvers})
const app = express();
然后,例如,如果我们想要 运行 在端点 GET /execute
上进行查询
app.get('/execute', function(req, res){
var query = `
query myQueryTitle{
queryName{
_id,
...
}
}
`;
graphql(schema, query, null, context)
.then((result) => {
res.json(result.data.queryName);
});
})
所以任何时候我们想要 运行 一个查询或一个突变,我们调用 graphql
传递 schema
, requestString
/query
和 context
对象
这可能与 graphqlExpress
的实例共存
我想知道是否可以使用 Apollo Server 在服务器端本地执行突变或查询。
示例:
1- 到达端点 GET /createNew
2- 我 运行 我定义的突变:
apolloserver.mutation(mutation_query).then((response)=>{res.status(200)})
3- 一个新条目被添加到数据库并且一个 JSON 字符串返回给客户端
有这样的东西吗? apolloserver
对象在 运行 时可用吗?
我正在使用 NodeJS + Express
我成功了
首先我们启动对象:
import express from 'express';
import { graphql } from 'graphql';
const context = require('./context'); //this is the object to be passed to each resolver function
const schema = require('./schema'); //this is the object returned by makeExecutableSchema({typeDefs, resolvers})
const app = express();
然后,例如,如果我们想要 运行 在端点 GET /execute
app.get('/execute', function(req, res){
var query = `
query myQueryTitle{
queryName{
_id,
...
}
}
`;
graphql(schema, query, null, context)
.then((result) => {
res.json(result.data.queryName);
});
})
所以任何时候我们想要 运行 一个查询或一个突变,我们调用 graphql
传递 schema
, requestString
/query
和 context
对象
这可能与 graphqlExpress