Does the apollo client work on node.js?

I need a libq graphical application to work with node.js for some testing and some mashup data - not in production capacity. I use apollo everywhere ( react-apollo , apollo graphql-server-express ). My needs are pretty simple.

Is apollo-client viable choice? I cannot find examples or documents about using this in node - if you know about them, share it.

Or maybe I should / use the link graphical client on node?

+13
source share
4 answers

The Apollo client should work fine on Node. You only need to cross-fetch, as it is assumed that fetch exists.

Here is the full implementation of TypeScript Apollo Client running on Node.js.

 import ApolloClient from "apollo-boost"; import gql from "graphql-tag"; import { InsertJob } from "./graphql-types"; import 'cross-fetch/polyfill'; const client = new ApolloClient({ uri: "http://localhost:3000/graphql" }); client.mutate<InsertJob.AddCompany, InsertJob.Variables>({ mutation: gql'mutation insertJob($companyName: String!) { addCompany(input: { displayName: $companyName } ) { id } }', variables: { companyName: "aaa" } }) .then(result => console.log(result)); 
+6
source

Here is a simple implementation of node js.

Customer

'graphiql' is good enough for development.

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

server.js

 var graphql = require ('graphql').graphql var express = require('express') var graphQLHTTP = require('express-graphql') var Schema = require('./schema') // This is just an internal test var query = 'query{starwar{name, gender,gender}}' graphql(Schema, query).then( function(result) { console.log(JSON.stringify(result,null," ")); }); var app = express() .use('/', graphQLHTTP({ schema: Schema, pretty: true, graphiql: true })) .listen(8080, function (err) { console.log('GraphQL Server is now running on localhost:8080'); }); 

schema.js

 //schema.js var graphql = require ('graphql'); var http = require('http'); 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 = http.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.GraphQLSchema({ query: QueryType }); 
+2
source

In response to @YakirNa's comment:

I cannot speak with other needs that I have described, but I have done a lot of testing. I have completed all my tests in the process.

Most of the tests end with testing the resolver, which I do through jig, which calls the graphql library graphql with a test request and then checks the response.

I also have a (almost) end-to-end test layer that works at the HTTP Express processing level. It creates a fake HTTP request and checks the response in the process. This is all part of the server process; nothing goes through the wire. I use this easily, mainly for JWT authentication and other requests at the request level that are independent of the graphql request body.

+1
source

If someone is looking for a version of JavaScript:

 require('dotenv').config(); const gql = require('graphql-tag'); const ApolloClient = require('apollo-boost').ApolloClient; const fetch = require('cross-fetch/polyfill').fetch; const createHttpLink = require('apollo-link-http').createHttpLink; const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; const client = new ApolloClient({ link: createHttpLink({ uri: process.env.API, fetch: fetch }), cache: new InMemoryCache() }); client.mutate({ mutation: gql' mutation popJob { popJob { id type param status progress creation_date expiration_date } } ', }).then(job => { console.log(job); }) 
0
source

Source: https://habr.com/ru/post/1260297/


All Articles