Tag: graphql

如何实现Rest API,它支持使用nodejs和mongodb进行部分响应和嵌套查询

我一直想知道如何实现一个如Facebookgraphics,谷歌和LinkedIn部分响应如这里所述的api系统 Facebook的 GET /clients/007?fields=firstname,name 200 OK { "id":"007", "firstname":"James", "name":"Bond" } 谷歌 GET /clients/007?fields=firstname,name,address(street) <— nested query 200 OK { "id":"007", "firstname":"James", "name":"Bond", "address":{"street":"Horsen Ferry Road"} } 我看了一下facebook GraphQL库似乎提供了部分响应的解决scheme,但它似乎更复杂,难以实现 有没有任何nodejs库可以提供这样的界面权限pipe理来控制哪些用户可以访问哪些数据

Graphql:必须提供查询string

我有一个简单的快递graphql服务器: const schema = require('./schema'); const express = require('express'); const graphqlHTTP = require('express-graphql'); const cors = require('cors') const bodyParser = require('body-parser'); const app = express(); app.use(cors()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use('/graphql', graphqlHTTP(req => { return ({ schema, pretty: true, }) })); const server = app.listen(9000, err => { if (err) { return err; } […]

发送一个http请求到一个graphql api时“parsingJSON的问题”

我试图发送一个HTTP请求到Githubs graphql API(V4)。 我的猜测是我的查询格式是错误的。 下面的代码用于向api发送POST请求。 app.get('/fetch-data', function(req, res, next) { const access_token = settings.dev.TOKEN; const query = {query: { viewer: { 'login': 'muckbuck' } }}; const options = { uri: 'https://api.github.com/graphql', method: 'POST', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + access_token, 'User-Agent': 'request', 'contentType': "application/graphql", }, data: query }; function callback(error, response, body) { […]

npm开始在Windows机器上失败,但在Ubuntu的工作

我正在学习本教程https://edgecoders.com/graphql-learn-by-doing-part-1-of-3-9b04cadeacfa了解GraphQL的基础知识。 当我试图运行这个命令后,下面的整个过程npm start它在Windows上失败,但相同的命令正在我的Linux机器上工作。 任何人都可以在这里指出我在这里失去的东西吗? 错误是 npm ERR! Windows_NT 10.0.14393 npm ERR! argv "C:\\Program Files (x86)\\nodejs\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules \\npm\\bin\\npm-cli.js" "start" npm ERR! node v6.11.1 npm ERR! npm v3.10.10 npm ERR! code ELIFECYCLE npm ERR! github-graphql-server@1.0.0 start: `babel-node –presets es2015 ./server.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the github-graphql-server@1.0.0 start script […]

GraphiQL返回nil变异

我使用Seqelize GraphQL,我有一个返回null的变异。 我试过用不同的几种方法来改变这个变种的parsing器,但是当我用GraphiQL来testing的时候它总是返回null。 下面是用几种不同的方式写的parsing器: 1 – 最初,我有想法添加包装function的承诺。 resolve(root, args) { return new Promise((resolve, reject) => { db.record.findOne({ where: { UID: args.UID } }) .then(record => { let newArr = undefined if (record.watched == null) { newArr = [args.value] } else { let old = record.watched newArr = old.concat([args.value]) } db.record.update({ watched: newArr }, { where: […]

GraphQL – 嵌套方法

我对GraphQL很陌生,想知道是否可以为types成员input单独的方法 码 var express = require('express'); var graphqlHTTP = require('express-graphql'); var { buildSchema } = require('graphql'); var schema = buildSchema(` type Query { beitraege: Beitrag } type Beitrag { beitr_name: String erst_name: String } `); var root = { // This is the part that does not work Beitrag: () => { beitr_name: () => […]

如何编写graphql的中间件,这将在每个parsing器之前调用

在每一个请求我发送令牌,并检查它在快递中间件 app.use(async (req, res, next) => { const authorization = req.headers.authorization; let token = null; let user; if (authorization) { try { token = jwt.verify(authorization, config.secret); } catch (e) { // dont work throw new GraphQLError({ message: 'token damaged' }); } if (token) { const { _id } = token; user = await User.findOne({ _id […]

Npm运行并行脚本无法正常工作

脚本的目标是保持重试连接到服务器端点,并在可能时生成graphql模式。 如果我在另一个命令提示符下运行脚本,脚本工作正常。 这只是我想在一个命令提示符下运行一个命令。 相反,我得到的错误: (节点:4212)MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。 添加了11个结束监听器。 使用emitter.setMaxListeners()来增加限制 这对我来说没有任何意义,因为听众应该只设置一次,而不是11次。 dotnet-run只运行asp.net服务器。 显然,这需要一些时间来启动,这就是为什么我写这个脚本,以保持重试到服务器。 "start": "npm-run-all –parallel dotnet-run get-schema:dev", "get-schema:dev": "node scripts/getSchema.js –url http://localhost:8080/graphql" 我正在使用npm-run-all npm软件包 getSchema脚本: require('isomorphic-fetch'); const getArgs = require('get-args'); const FormData = require('form-data'); const fs = require('fs'); const { buildClientSchema, introspectionQuery, printSchema, } = require('graphql/utilities'); const path = require('path'); const schemaPath = path.join(__dirname, '../schema/schema'); const formData […]

在Nodejs中以GraphQLtypes分配JSON

我打电话给一个报告API与NodeJs,这给了我一个JSON响应。 可以使用不同的ID来调用API(在这种情况下,不同的报告ID)。 例如: https://report.domain.io/report/1 https://report.domain.io/report/2 等等。 由于id在不同的时间是不同的,它会给我不同的回应。 属性/名称将会不同。 所以我无法将其映射到GraphQLObjectType中。 例如,如果JSON响应如下: [ { "modelbrand": "Sony", "modelcode": "F3111", "avg_free_storage": 225401514 }, { "modelbrand": "Sony", "modelcode": "F3111", "avg_free_storage": 224547840 } ] 然后我可以用GraphQL来映射它,如下所示: Report() { return new graphql.GraphQLObjectType({ name: "Report", description: "This represents report", fields: () => ({ "modelbrand": {type: graphql.GraphQLString}, "modelcode": {type: graphql.GraphQLString}, "avg_free_storage": {type: graphql.GraphQLString} }) }); […]

Apollo Graphql架构拼接冲突

我有一个关于GraphQL架构拼接的问题。 我有两个Graphql模式: type Name { firstname: String! lastname: String! } type Address { street: String! number: Int! } type User { name: Name! address: Address! } type Query { user(userId: String!): User } 和 type User { age: String! } type Query { user(userId: String!): User } 我现在尝试使用graphql-tools的mergeSchemas函数来合并模式: const schema = mergeSchemas({ schemas: [schema1, schema2] […]