Vic*_*ira 2 node.js express graphql apollo-server
我想知道是否有可能使用Apollo Server在服务器端本地执行变异或查询。
例:
1-达到端点GET / createNew
2-我定义了一个突变,例如:
apolloserver.mutation(mutation_query).then((response)=>{res.status(200)})
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
然后,例如,如果我们要在端点上运行查询 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);
});
})
Run Code Online (Sandbox Code Playgroud)
因此,无论何时我们想运行查询或变异,我们都将调用graphqlpass schema,requestString/ query和context对象
这可能与以下情况共存 graphqlExpress
| 归档时间: |
|
| 查看次数: |
1301 次 |
| 最近记录: |