Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了graphql – apollo-client是否可以在node.js上运行?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个graphql客户端lib来运行node.js进行一些测试和一些数据混搭 – 而不是生产能力.我在其他地方使用apollo(react-apollo,apollo的graphql-server-express).我的需求非常简单.

apollo-client是一个可行的选择吗?我找不到关于在节点上使用它的示例或文档 – 如果您知道任何,请分享.

或者我应该/可以在节点上使用参graphql客户端?

解决方法

这是简单的节点js实现.

‘graphiql’客户端足以满足开发活动的需要.

1. run npm install
2. start server with "node server.js"
3. hit "@R_874_10107@://localhost:8080/graphiql"  for graphiql client

server.js

var graphql = require ('graphql').graphql  
var express = require('express')  
var graphQL@R_874_10107@ = require('express-graphql')  

var scheR_105_11845@a = require('./scheR_105_11845@a')  

// This is just an internal test
var query = 'query{starwar{name,gender,gender}}'  
graphql(scheR_105_11845@a,query).then( function(result) {  
  console.log(JSON.Stringify(result,null," "));
});

var app = express()  
  .use('/',graphQL@R_874_10107@({ scheR_105_11845@a: scheR_105_11845@a,pretty: true,graphiql: true }))
  .listen(8080,function (err) {
    console.log('GraphQL Server is Now running on localhost:8080');
  });

scheR_105_11845@a.js

//scheR_105_11845@a.js
var graphql = require ('graphql');  
var @R_874_10107@ = require('@R_874_10107@');

var StarWar = [  
  { 
    "name": "default","gender": "default","mass": "default"
  }
];

var TodoType = new graphql.GraphQLObjectType({  
  name: 'starwar',fields: function () {
    return {
      name: {
        type: graphql.GraphQLString
      },gender: {
        type: graphql.GraphQLString
      },mass: {
        type: graphql.GraphQLString
      }
    }
  }
});



var QueryType = new graphql.GraphQLObjectType({  
  name: 'Query',fields: function () {
    return {
      starwar: {
        type: new graphql.GraphQLList(TodoTypE),resolve: function () {
          return new Promise(function (resolve,reject) {
            var request = @R_874_10107@.get({
              hostname: 'swapi.co',path: '/api/people/1/',method: 'GET'
            },function(res){
                    res.setEncoding('utf8');
                    res.on('data',function(responsE){
                    StarWar = [JSON.parse(responsE)];
                    resolve(StarWar)

                    console.log('On response success:',StarWar);
                });
            });

            request.on('error',function(responsE){
                    console.log('On error',response.messagE);
                });

            request.end();                      
          });
        }
      }
    }
  }
});

module.exports = new graphql.GraphQLscheR_105_11845@a({  
  query: QueryType
});

大佬总结

以上是大佬教程为你收集整理的graphql – apollo-client是否可以在node.js上运行?全部内容,希望文章能够帮你解决graphql – apollo-client是否可以在node.js上运行?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。